|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "path" |
| 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 CephHealth string |
| 14 | + |
| 15 | +const ( |
| 16 | + CephHealthOK CephHealth = "HEALTH_OK" |
| 17 | + CephHealthWarn CephHealth = "HEALTH_WARN" |
| 18 | + CephHealthErr CephHealth = "HEALTH_ERR" |
| 19 | +) |
| 20 | + |
| 21 | +func (a CephHealth) Compare(b CephHealth) int { |
| 22 | + if a == b { |
| 23 | + return 0 |
| 24 | + } |
| 25 | + switch a { |
| 26 | + case CephHealthOK: |
| 27 | + return 1 |
| 28 | + case CephHealthWarn: |
| 29 | + switch b { |
| 30 | + case CephHealthOK: |
| 31 | + return -1 |
| 32 | + case CephHealthErr: |
| 33 | + return 1 |
| 34 | + } |
| 35 | + return 1 |
| 36 | + case CephHealthErr: |
| 37 | + switch b { |
| 38 | + case CephHealthOK, CephHealthWarn: |
| 39 | + return -1 |
| 40 | + } |
| 41 | + return 1 |
| 42 | + default: |
| 43 | + return -1 |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +var CephStatusDefaultOutcomes = []*troubleshootv1beta2.Outcome{ |
| 48 | + { |
| 49 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 50 | + Message: "Ceph is healthy", |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + Warn: &troubleshootv1beta2.SingleOutcome{ |
| 55 | + Message: "Ceph status is HEALTH_WARN", |
| 56 | + URI: "https://rook.io/docs/rook/v1.4/ceph-common-issues.html", |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 61 | + Message: "Ceph status is HEALTH_ERR", |
| 62 | + URI: "https://rook.io/docs/rook/v1.4/ceph-common-issues.html", |
| 63 | + }, |
| 64 | + }, |
| 65 | +} |
| 66 | + |
| 67 | +func cephStatus(analyzer *troubleshootv1beta2.CephStatusAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { |
| 68 | + fileName := path.Join(collect.GetCephCollectorFilepath(analyzer.CollectorName, analyzer.Namespace), "status.json") |
| 69 | + collected, err := getCollectedFileContents(fileName) |
| 70 | + if err != nil { |
| 71 | + return nil, errors.Wrap(err, "failed to read collected ceph status") |
| 72 | + } |
| 73 | + |
| 74 | + title := analyzer.CheckName |
| 75 | + if title == "" { |
| 76 | + title = "Ceph Status" |
| 77 | + } |
| 78 | + |
| 79 | + analyzeResult := &AnalyzeResult{ |
| 80 | + Title: title, |
| 81 | + IconKey: "rook", // maybe this should be ceph? |
| 82 | + IconURI: "https://troubleshoot.sh/images/analyzer-icons/rook.svg?w=11&h=16", |
| 83 | + } |
| 84 | + |
| 85 | + status := struct { |
| 86 | + Health struct { |
| 87 | + Status string `json:"status"` |
| 88 | + } `json:"health"` |
| 89 | + }{} |
| 90 | + if err := json.Unmarshal(collected, &status); err != nil { |
| 91 | + return nil, errors.Wrap(err, "failed to unmarshal status.json") |
| 92 | + } |
| 93 | + |
| 94 | + if len(analyzer.Outcomes) == 0 { |
| 95 | + analyzer.Outcomes = CephStatusDefaultOutcomes |
| 96 | + } |
| 97 | + |
| 98 | + for _, outcome := range analyzer.Outcomes { |
| 99 | + if outcome.Fail != nil { |
| 100 | + if outcome.Fail.When == "" { |
| 101 | + outcome.Fail.When = string(CephHealthErr) |
| 102 | + } |
| 103 | + match, err := compareCephStatus(status.Health.Status, outcome.Fail.When) |
| 104 | + if err != nil { |
| 105 | + return nil, errors.Wrap(err, "failed to compare ceph status") |
| 106 | + } else if match { |
| 107 | + analyzeResult.IsFail = true |
| 108 | + analyzeResult.Message = outcome.Fail.Message |
| 109 | + analyzeResult.URI = outcome.Fail.URI |
| 110 | + return analyzeResult, nil |
| 111 | + } |
| 112 | + } else if outcome.Warn != nil { |
| 113 | + if outcome.Warn.When == "" { |
| 114 | + outcome.Warn.When = string(CephHealthWarn) |
| 115 | + } |
| 116 | + match, err := compareCephStatus(status.Health.Status, outcome.Warn.When) |
| 117 | + if err != nil { |
| 118 | + return nil, errors.Wrap(err, "failed to compare ceph status") |
| 119 | + } else if match { |
| 120 | + analyzeResult.IsWarn = true |
| 121 | + analyzeResult.Message = outcome.Warn.Message |
| 122 | + analyzeResult.URI = outcome.Warn.URI |
| 123 | + return analyzeResult, nil |
| 124 | + } |
| 125 | + } else if outcome.Pass != nil { |
| 126 | + if outcome.Pass.When == "" { |
| 127 | + outcome.Pass.When = string(CephHealthOK) |
| 128 | + } |
| 129 | + match, err := compareCephStatus(status.Health.Status, outcome.Pass.When) |
| 130 | + if err != nil { |
| 131 | + return nil, errors.Wrap(err, "failed to compare ceph status") |
| 132 | + } else if match { |
| 133 | + analyzeResult.IsPass = true |
| 134 | + analyzeResult.Message = outcome.Pass.Message |
| 135 | + analyzeResult.URI = outcome.Pass.URI |
| 136 | + return analyzeResult, nil |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return analyzeResult, nil |
| 142 | +} |
| 143 | + |
| 144 | +func compareCephStatus(actual, when string) (bool, error) { |
| 145 | + parts := strings.Split(strings.TrimSpace(when), " ") |
| 146 | + |
| 147 | + if len(parts) == 1 { |
| 148 | + value := strings.TrimSpace(parts[0]) |
| 149 | + return value == actual, nil |
| 150 | + } |
| 151 | + |
| 152 | + if len(parts) != 2 { |
| 153 | + return false, errors.New("unable to parse when range") |
| 154 | + } |
| 155 | + |
| 156 | + operator := strings.TrimSpace(parts[0]) |
| 157 | + value := strings.TrimSpace(parts[1]) |
| 158 | + |
| 159 | + compareResult := CephHealth(actual).Compare(CephHealth(value)) |
| 160 | + |
| 161 | + switch operator { |
| 162 | + case "=", "==", "===": |
| 163 | + return compareResult == 0, nil |
| 164 | + case "<": |
| 165 | + return compareResult == -1, nil |
| 166 | + case ">": |
| 167 | + return compareResult == 1, nil |
| 168 | + case "<=": |
| 169 | + return compareResult <= 0, nil |
| 170 | + case ">=": |
| 171 | + return compareResult >= 0, nil |
| 172 | + default: |
| 173 | + return false, errors.New("unknown operator") |
| 174 | + } |
| 175 | +} |
0 commit comments