|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 12 | +) |
| 13 | + |
| 14 | +func analyzeRegex(analyzer *troubleshootv1beta1.RegEx, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 15 | + contents, err := getCollectedFileContents(path.Join(analyzer.CollectorName, analyzer.CollectorName+".txt")) |
| 16 | + if err != nil { |
| 17 | + return nil, errors.Wrap(err, "failed to get file contents for regex") |
| 18 | + } |
| 19 | + |
| 20 | + expression := regexp.MustCompile(analyzer.Expression) |
| 21 | + match := expression.FindStringSubmatch(string(contents)) |
| 22 | + |
| 23 | + result := &AnalyzeResult{ |
| 24 | + Title: analyzer.CheckName, |
| 25 | + } |
| 26 | + // to avoid empty strings in the UI..,. |
| 27 | + if result.Title == "" { |
| 28 | + result.Title = analyzer.CollectorName |
| 29 | + } |
| 30 | + |
| 31 | + foundMatches := map[string]string{} |
| 32 | + for i, name := range expression.SubexpNames() { |
| 33 | + if i != 0 && name != "" { |
| 34 | + foundMatches[name] = match[i] |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // allow fallthrough |
| 39 | + for _, outcome := range analyzer.Outcomes { |
| 40 | + if outcome.Fail != nil { |
| 41 | + if outcome.Fail.When == "" { |
| 42 | + result.IsFail = true |
| 43 | + result.Message = outcome.Fail.Message |
| 44 | + result.URI = outcome.Fail.URI |
| 45 | + |
| 46 | + return result, nil |
| 47 | + } |
| 48 | + |
| 49 | + isMatch, err := compareRegex(outcome.Fail.When, foundMatches) |
| 50 | + if err != nil { |
| 51 | + return result, errors.Wrap(err, "failed to compare regex fail conditional") |
| 52 | + } |
| 53 | + |
| 54 | + if isMatch { |
| 55 | + result.IsFail = true |
| 56 | + result.Message = outcome.Fail.Message |
| 57 | + result.URI = outcome.Fail.URI |
| 58 | + |
| 59 | + return result, nil |
| 60 | + } |
| 61 | + } else if outcome.Warn != nil { |
| 62 | + if outcome.Warn.When == "" { |
| 63 | + result.IsWarn = true |
| 64 | + result.Message = outcome.Warn.Message |
| 65 | + result.URI = outcome.Warn.URI |
| 66 | + |
| 67 | + return result, nil |
| 68 | + } |
| 69 | + |
| 70 | + isMatch, err := compareRegex(outcome.Warn.When, foundMatches) |
| 71 | + if err != nil { |
| 72 | + return result, errors.Wrap(err, "failed to compare regex warn conditional") |
| 73 | + } |
| 74 | + |
| 75 | + if isMatch { |
| 76 | + result.IsWarn = true |
| 77 | + result.Message = outcome.Warn.Message |
| 78 | + result.URI = outcome.Warn.URI |
| 79 | + |
| 80 | + return result, nil |
| 81 | + } |
| 82 | + } else if outcome.Pass != nil { |
| 83 | + if outcome.Pass.When == "" { |
| 84 | + result.IsPass = true |
| 85 | + result.Message = outcome.Pass.Message |
| 86 | + result.URI = outcome.Pass.URI |
| 87 | + |
| 88 | + return result, nil |
| 89 | + } |
| 90 | + |
| 91 | + isMatch, err := compareRegex(outcome.Pass.When, foundMatches) |
| 92 | + if err != nil { |
| 93 | + return result, errors.Wrap(err, "failed to compare regex pass conditional") |
| 94 | + } |
| 95 | + |
| 96 | + if isMatch { |
| 97 | + result.IsPass = true |
| 98 | + result.Message = outcome.Pass.Message |
| 99 | + result.URI = outcome.Pass.URI |
| 100 | + |
| 101 | + return result, nil |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return result, nil |
| 107 | +} |
| 108 | + |
| 109 | +func compareRegex(conditional string, foundMatches map[string]string) (bool, error) { |
| 110 | + parts := strings.Split(strings.TrimSpace(conditional), " ") |
| 111 | + |
| 112 | + if len(parts) != 3 { |
| 113 | + return false, errors.New("unable to parse regex conditional") |
| 114 | + } |
| 115 | + |
| 116 | + lookForMatchName := parts[0] |
| 117 | + operator := parts[1] |
| 118 | + lookForValue := parts[2] |
| 119 | + |
| 120 | + foundValue, ok := foundMatches[lookForMatchName] |
| 121 | + if !ok { |
| 122 | + // not an error, just wasn't matched |
| 123 | + return false, nil |
| 124 | + } |
| 125 | + |
| 126 | + // if the value side of the conditional is an int, we assume it's an int |
| 127 | + lookForValueInt, err := strconv.Atoi(lookForValue) |
| 128 | + if err == nil { |
| 129 | + fmt.Printf("look for = %s, found = %s\n", lookForValue, foundValue) |
| 130 | + foundValueInt, err := strconv.Atoi(foundValue) |
| 131 | + if err != nil { |
| 132 | + // not an error but maybe it should be... |
| 133 | + return false, nil |
| 134 | + } |
| 135 | + |
| 136 | + switch operator { |
| 137 | + case "=": |
| 138 | + fallthrough |
| 139 | + case "==": |
| 140 | + fallthrough |
| 141 | + case "===": |
| 142 | + return lookForValueInt == foundValueInt, nil |
| 143 | + |
| 144 | + case "<": |
| 145 | + return lookForValueInt < foundValueInt, nil |
| 146 | + |
| 147 | + case ">": |
| 148 | + return lookForValueInt > foundValueInt, nil |
| 149 | + |
| 150 | + case "<=": |
| 151 | + return lookForValueInt <= foundValueInt, nil |
| 152 | + |
| 153 | + case ">=": |
| 154 | + return lookForValueInt >= foundValueInt, nil |
| 155 | + } |
| 156 | + } else { |
| 157 | + // all we can support is "=" and "==" and "===" for now |
| 158 | + if operator != "=" && operator != "==" && operator != "===" { |
| 159 | + return false, errors.New("unexpected operator in regex comparator") |
| 160 | + } |
| 161 | + |
| 162 | + return foundValue == lookForValue, nil |
| 163 | + } |
| 164 | + |
| 165 | + return false, nil |
| 166 | +} |
0 commit comments