-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoverall.go
344 lines (279 loc) · 7.91 KB
/
overall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// result tries to
package result
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/NETWAYS/go-check"
"github.com/NETWAYS/go-check/perfdata"
)
// The "width" of the indentation which is added on every level
const indentationOffset = 2
// Overall is a singleton for a monitoring plugin that has several partial results (or sub-results)
//
// Design decisions: A check plugin has a single Overall (singleton),
// each partial thing which is tested, gets its own subcheck.
//
// The results of these may be relevant to the overall status in the end
// or not, e.g. if a plugin tries two different methods for something and
// one suffices, but one fails, the whole check might be OK and only the subcheck
// Warning or Critical.
type Overall struct {
oks int
warnings int
criticals int
unknowns int
Summary string
stateSetExplicitly bool
Outputs []string // Deprecate this in a future version
PartialResults []PartialResult
}
// PartialResult represents a sub-result for an Overall struct
type PartialResult struct {
Perfdata perfdata.PerfdataList
PartialResults []PartialResult
Output string
state int // Result state, either set explicitly or derived from partialResults
defaultState int // Default result state, if no partial results are available and no state is set explicitly
stateSetExplicitly bool // nolint: unused
defaultStateSet bool // nolint: unused
}
// Initializer for a PartialResult with "sane" defaults
// Notable default compared to the nil object: the default state is set to Unknown
func NewPartialResult() PartialResult {
return PartialResult{
stateSetExplicitly: false,
defaultState: check.Unknown,
}
}
// String returns the status and output of the PartialResult
func (s *PartialResult) String() string {
return fmt.Sprintf("[%s] %s", check.StatusText(s.GetStatus()), s.Output)
}
// Add adds a return state explicitly
//
// Hint: This will set stateSetExplicitly to true
func (o *Overall) Add(state int, output string) {
switch state {
case check.OK:
o.oks++
case check.Warning:
o.warnings++
case check.Critical:
o.criticals++
default:
o.unknowns++
}
// TODO: Might be a bit obscure that the Add method also sets stateSetExplicitly
o.stateSetExplicitly = true
o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", check.StatusText(state), output))
}
// AddSubcheck adds a PartialResult to the Overall
func (o *Overall) AddSubcheck(subcheck PartialResult) {
o.PartialResults = append(o.PartialResults, subcheck)
}
// AddSubcheck adds a PartialResult to the PartialResult
func (s *PartialResult) AddSubcheck(subcheck PartialResult) {
s.PartialResults = append(s.PartialResults, subcheck)
}
// GetStatus returns the current state (ok, warning, critical, unknown) of the Overall
func (o *Overall) GetStatus() int {
if o.stateSetExplicitly {
// nolint: gocritic
if o.criticals > 0 {
return check.Critical
} else if o.unknowns > 0 {
return check.Unknown
} else if o.warnings > 0 {
return check.Warning
} else if o.oks > 0 {
return check.OK
}
return check.Unknown
}
// state not set explicitly!
if len(o.PartialResults) == 0 {
return check.Unknown
}
var (
criticals int
warnings int
oks int
unknowns int
)
for _, sc := range o.PartialResults {
switch sc.GetStatus() {
case check.Critical:
criticals++
case check.Warning:
warnings++
case check.Unknown:
unknowns++
case check.OK:
oks++
}
}
if criticals > 0 {
return check.Critical
}
if unknowns > 0 {
return check.Unknown
}
if warnings > 0 {
return check.Warning
}
if oks > 0 {
return check.OK
}
return check.Unknown
}
// GetSummary returns a text representation of the current state of the Overall
// nolint: funlen
func (o *Overall) GetSummary() string {
if o.Summary != "" {
return o.Summary
}
// Was the state set explicitly?
if o.stateSetExplicitly {
// Yes, so lets generate it from the sum of the overall states
if o.criticals > 0 {
o.Summary += fmt.Sprintf("critical=%d ", o.criticals)
}
if o.unknowns > 0 {
o.Summary += fmt.Sprintf("unknown=%d ", o.unknowns)
}
if o.warnings > 0 {
o.Summary += fmt.Sprintf("warning=%d ", o.warnings)
}
if o.oks > 0 {
o.Summary += fmt.Sprintf("ok=%d ", o.oks)
}
if o.Summary == "" {
o.Summary = "No status information"
return o.Summary
}
}
if !o.stateSetExplicitly {
// No, so lets combine the partial ones
if len(o.PartialResults) == 0 {
// Oh, we actually don't have those either
o.Summary = "No status information"
return o.Summary
}
var (
criticals int
warnings int
oks int
unknowns int
)
for _, sc := range o.PartialResults {
switch sc.GetStatus() {
case check.Critical:
criticals++
case check.Warning:
warnings++
case check.Unknown:
unknowns++
case check.OK:
oks++
}
}
if criticals > 0 {
o.Summary += fmt.Sprintf("critical=%d ", criticals)
}
if unknowns > 0 {
o.Summary += fmt.Sprintf("unknowns=%d ", unknowns)
}
if warnings > 0 {
o.Summary += fmt.Sprintf("warning=%d ", warnings)
}
if oks > 0 {
o.Summary += fmt.Sprintf("ok=%d ", oks)
}
}
o.Summary = "states: " + strings.TrimSpace(o.Summary)
return o.Summary
}
// GetOutput returns a text representation of the current outputs of the Overall
func (o *Overall) GetOutput() string {
var output strings.Builder
output.WriteString(o.GetSummary() + "\n")
for _, extra := range o.Outputs {
output.WriteString(extra + "\n")
}
if o.PartialResults != nil {
var pdata strings.Builder
// Generate indeted output and perfdata for all partialResults
for i := range o.PartialResults {
output.WriteString(o.PartialResults[i].getOutput(0))
pdata.WriteString(" " + o.PartialResults[i].getPerfdata())
}
pdataString := strings.Trim(pdata.String(), " ")
if len(pdataString) > 0 {
output.WriteString("|" + pdataString + "\n")
}
}
return output.String()
}
// getPerfdata returns all subsequent perfdata as a concatenated string
func (s *PartialResult) getPerfdata() string {
var output strings.Builder
if len(s.Perfdata) > 0 {
output.WriteString(s.Perfdata.String())
}
if s.PartialResults != nil {
for _, ss := range s.PartialResults {
output.WriteString(" " + ss.getPerfdata())
}
}
return strings.TrimSpace(output.String())
}
// getOutput generates indented output for all subsequent PartialResults
func (s *PartialResult) getOutput(indentLevel int) string {
var output strings.Builder
prefix := strings.Repeat(" ", indentLevel)
output.WriteString(prefix + "\\_ " + s.String() + "\n")
if s.PartialResults != nil {
for _, ss := range s.PartialResults {
output.WriteString(ss.getOutput(indentLevel + indentationOffset))
}
}
return output.String()
}
// SetDefaultState sets a new default state for a PartialResult
func (s *PartialResult) SetDefaultState(state int) error {
if state < check.OK || state > check.Unknown {
return errors.New("Default State is not a valid result state. Got " + strconv.Itoa(state) + " which is not valid")
}
s.defaultState = state
s.defaultStateSet = true
return nil
}
// SetState sets a state for a PartialResult
func (s *PartialResult) SetState(state int) error {
if state < check.OK || state > check.Unknown {
return errors.New("Default State is not a valid result state. Got " + strconv.Itoa(state) + " which is not valid")
}
s.state = state
s.stateSetExplicitly = true
return nil
}
// GetStatus returns the current state (ok, warning, critical, unknown) of the PartialResult
// nolint: unused
func (s *PartialResult) GetStatus() int {
if s.stateSetExplicitly {
return s.state
}
if len(s.PartialResults) == 0 {
if s.defaultStateSet {
return s.defaultState
}
return check.Unknown
}
states := make([]int, len(s.PartialResults))
for i := range s.PartialResults {
states[i] = s.PartialResults[i].GetStatus()
}
return WorstState(states...)
}