|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 10 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 11 | +) |
| 12 | + |
| 13 | +// Ensure `AnalyzeHostSysctl` implements `HostAnalyzer` interface at compile time. |
| 14 | +var _ HostAnalyzer = (*AnalyzeHostSysctl)(nil) |
| 15 | + |
| 16 | +type AnalyzeHostSysctl struct { |
| 17 | + hostAnalyzer *troubleshootv1beta2.HostSysctlAnalyze |
| 18 | +} |
| 19 | + |
| 20 | +func (a *AnalyzeHostSysctl) Title() string { |
| 21 | + return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Sysctl") |
| 22 | +} |
| 23 | + |
| 24 | +func (a *AnalyzeHostSysctl) IsExcluded() (bool, error) { |
| 25 | + return isExcluded(a.hostAnalyzer.Exclude) |
| 26 | +} |
| 27 | + |
| 28 | +func (a *AnalyzeHostSysctl) Analyze( |
| 29 | + getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents, |
| 30 | +) ([]*AnalyzeResult, error) { |
| 31 | + result := AnalyzeResult{Title: a.Title()} |
| 32 | + |
| 33 | + // Use the generic function to collect both local and remote data |
| 34 | + collectedContents, err := retrieveCollectedContents( |
| 35 | + getCollectedFileContents, |
| 36 | + collect.HostSysctlPath, // Local path |
| 37 | + collect.NodeInfoBaseDir, // Remote base directory |
| 38 | + collect.HostSysctlFileName, // Remote file name |
| 39 | + ) |
| 40 | + if err != nil { |
| 41 | + return []*AnalyzeResult{&result}, err |
| 42 | + } |
| 43 | + |
| 44 | + results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title()) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Wrap(err, "failed to analyze sysctl output") |
| 47 | + } |
| 48 | + |
| 49 | + return results, nil |
| 50 | +} |
| 51 | + |
| 52 | +// checkCondition checks the condition of the when clause |
| 53 | +func (a *AnalyzeHostSysctl) CheckCondition(when string, data []byte) (bool, error) { |
| 54 | + |
| 55 | + sysctl := map[string]string{} |
| 56 | + if err := json.Unmarshal(data, &sysctl); err != nil { |
| 57 | + return false, errors.Wrap(err, "failed to unmarshal data") |
| 58 | + } |
| 59 | + |
| 60 | + // <1:key> <2:operator> <3:value> |
| 61 | + matches := sysctlWhenRX.FindStringSubmatch(when) |
| 62 | + if len(matches) < 4 { |
| 63 | + return false, fmt.Errorf("expected 3 parts in when %q", when) |
| 64 | + } |
| 65 | + |
| 66 | + param := matches[1] |
| 67 | + expected := matches[3] |
| 68 | + opString := matches[2] |
| 69 | + operator, err := ParseComparisonOperator(opString) |
| 70 | + if err != nil { |
| 71 | + return false, errors.Wrap(err, fmt.Sprintf("failed to parse comparison operator %q", opString)) |
| 72 | + } |
| 73 | + |
| 74 | + if _, ok := sysctl[param]; !ok { |
| 75 | + return false, fmt.Errorf("kernel parameter %q does not exist on collected sysctl output", param) |
| 76 | + } |
| 77 | + |
| 78 | + switch operator { |
| 79 | + case Equal: |
| 80 | + return expected == sysctl[param], nil |
| 81 | + } |
| 82 | + |
| 83 | + // operator used is an inequality operator, the only valid inputs should be ints, if not we'll error out |
| 84 | + value, err := strconv.Atoi(sysctl[param]) |
| 85 | + if err != nil { |
| 86 | + return false, fmt.Errorf("collected sysctl param %q has value %q, cannot be used with provided operator %q", param, sysctl[param], opString) |
| 87 | + } |
| 88 | + expectedInt, err := strconv.Atoi(expected) |
| 89 | + if err != nil { |
| 90 | + return false, fmt.Errorf("expected value for sysctl param %q has value %q, cannot be used with provided operator %q", param, expected, opString) |
| 91 | + } |
| 92 | + |
| 93 | + switch operator { |
| 94 | + case LessThan: |
| 95 | + return value < expectedInt, nil |
| 96 | + case LessThanOrEqual: |
| 97 | + return value <= expectedInt, nil |
| 98 | + case GreaterThan: |
| 99 | + return value > expectedInt, nil |
| 100 | + case GreaterThanOrEqual: |
| 101 | + return value >= expectedInt, nil |
| 102 | + default: |
| 103 | + return false, fmt.Errorf("unsupported operator %q", opString) |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments