|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 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 | +type AnalyzeHostServices struct { |
| 14 | + hostAnalyzer *troubleshootv1beta2.HostServicesAnalyze |
| 15 | +} |
| 16 | + |
| 17 | +func (a *AnalyzeHostServices) Title() string { |
| 18 | + return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Host Services") |
| 19 | +} |
| 20 | + |
| 21 | +func (a *AnalyzeHostServices) IsExcluded() (bool, error) { |
| 22 | + return isExcluded(a.hostAnalyzer.Exclude) |
| 23 | +} |
| 24 | + |
| 25 | +func (a *AnalyzeHostServices) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 26 | + hostAnalyzer := a.hostAnalyzer |
| 27 | + |
| 28 | + contents, err := getCollectedFileContents(collect.HostServicesPath) |
| 29 | + if err != nil { |
| 30 | + return nil, errors.Wrap(err, "failed to get collected file") |
| 31 | + } |
| 32 | + |
| 33 | + var services []collect.ServiceInfo |
| 34 | + if err := json.Unmarshal(contents, &services); err != nil { |
| 35 | + return nil, errors.Wrap(err, "failed to unmarshal systemctl service info") |
| 36 | + } |
| 37 | + |
| 38 | + result := AnalyzeResult{} |
| 39 | + |
| 40 | + result.Title = a.Title() |
| 41 | + |
| 42 | + for _, outcome := range hostAnalyzer.Outcomes { |
| 43 | + if outcome.Fail != nil { |
| 44 | + if outcome.Fail.When == "" { |
| 45 | + result.IsFail = true |
| 46 | + result.Message = outcome.Fail.Message |
| 47 | + result.URI = outcome.Fail.URI |
| 48 | + |
| 49 | + return &result, nil |
| 50 | + } |
| 51 | + |
| 52 | + isMatch, err := compareHostServicesConditionalToActual(outcome.Fail.When, services) |
| 53 | + if err != nil { |
| 54 | + return nil, errors.Wrapf(err, "failed to compare %s", outcome.Fail.When) |
| 55 | + } |
| 56 | + |
| 57 | + if isMatch { |
| 58 | + result.IsFail = true |
| 59 | + result.Message = outcome.Fail.Message |
| 60 | + result.URI = outcome.Fail.URI |
| 61 | + |
| 62 | + return &result, nil |
| 63 | + } |
| 64 | + } else if outcome.Warn != nil { |
| 65 | + if outcome.Warn.When == "" { |
| 66 | + result.IsWarn = true |
| 67 | + result.Message = outcome.Warn.Message |
| 68 | + result.URI = outcome.Warn.URI |
| 69 | + |
| 70 | + return &result, nil |
| 71 | + } |
| 72 | + |
| 73 | + isMatch, err := compareHostServicesConditionalToActual(outcome.Warn.When, services) |
| 74 | + if err != nil { |
| 75 | + return nil, errors.Wrapf(err, "failed to compare %s", outcome.Warn.When) |
| 76 | + } |
| 77 | + |
| 78 | + if isMatch { |
| 79 | + result.IsWarn = true |
| 80 | + result.Message = outcome.Warn.Message |
| 81 | + result.URI = outcome.Warn.URI |
| 82 | + |
| 83 | + return &result, nil |
| 84 | + } |
| 85 | + } else if outcome.Pass != nil { |
| 86 | + if outcome.Pass.When == "" { |
| 87 | + result.IsPass = true |
| 88 | + result.Message = outcome.Pass.Message |
| 89 | + result.URI = outcome.Pass.URI |
| 90 | + |
| 91 | + return &result, nil |
| 92 | + } |
| 93 | + |
| 94 | + isMatch, err := compareHostServicesConditionalToActual(outcome.Pass.When, services) |
| 95 | + if err != nil { |
| 96 | + return nil, errors.Wrapf(err, "failed to compare %s", outcome.Pass.When) |
| 97 | + } |
| 98 | + |
| 99 | + if isMatch { |
| 100 | + result.IsPass = true |
| 101 | + result.Message = outcome.Pass.Message |
| 102 | + result.URI = outcome.Pass.URI |
| 103 | + |
| 104 | + return &result, nil |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return &result, nil |
| 110 | +} |
| 111 | + |
| 112 | +// <service> <op> <state> |
| 113 | +// example: ufw.service = active |
| 114 | +func compareHostServicesConditionalToActual(conditional string, services []collect.ServiceInfo) (res bool, err error) { |
| 115 | + parts := strings.Split(conditional, " ") |
| 116 | + if len(parts) != 3 { |
| 117 | + return false, fmt.Errorf("expected exactly 3 parts, got %d", len(parts)) |
| 118 | + } |
| 119 | + |
| 120 | + switch parts[1] { |
| 121 | + case "=", "==": |
| 122 | + for _, service := range services { |
| 123 | + if isServiceMatch(service.Unit, parts[0]) { |
| 124 | + return service.Active == parts[2], nil |
| 125 | + } |
| 126 | + } |
| 127 | + return false, nil |
| 128 | + case "!=", "<>": |
| 129 | + for _, service := range services { |
| 130 | + if isServiceMatch(service.Unit, parts[0]) { |
| 131 | + return service.Active != parts[2], nil |
| 132 | + } |
| 133 | + } |
| 134 | + return false, nil |
| 135 | + } |
| 136 | + |
| 137 | + return false, fmt.Errorf("unexpected operator %q", parts[1]) |
| 138 | +} |
| 139 | + |
| 140 | +func isServiceMatch(serviceName string, matchName string) bool { |
| 141 | + if serviceName == matchName { |
| 142 | + return true |
| 143 | + } |
| 144 | + |
| 145 | + if strings.HasPrefix(serviceName, matchName) { |
| 146 | + return true |
| 147 | + } |
| 148 | + |
| 149 | + return false |
| 150 | +} |
0 commit comments