-
Notifications
You must be signed in to change notification settings - Fork 3
/
sections.go
481 lines (392 loc) · 14 KB
/
sections.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/go-nagios
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package nagios
import (
"fmt"
"io"
"sort"
"strings"
)
// handleServiceOutputSection is a wrapper around the logic used to process
// the Service Output or "one-line summary" content.
func (p Plugin) handleServiceOutputSection(w io.Writer) {
if p.LongServiceOutput == "" {
// If Long Service Output was not specified, explicitly trim any
// formatted trailing spacing so that performance data output will be
// emitted immediately following the Service Output on the same line.
// NOTE: We explicitly include a space character in the cut set just
// on the off chance that a future update to the CheckOutputEOL
// constant removes the explicitly leading whitespace character.
cutSet := fmt.Sprintf(" \t%s", CheckOutputEOL)
p.ServiceOutput = strings.TrimRight(p.ServiceOutput, cutSet)
}
// Aside from (potentially) trimming trailing whitespace, we apply no
// formatting changes to this content, simply emit it as-is. This helps
// avoid potential issues with literal characters being interpreted as
// formatting verbs.
written, err := fmt.Fprint(w, p.ServiceOutput)
if err != nil {
// Very unlikely to occur, but we should still account for it.
panic("Failed to write ServiceOutput to given output sink")
}
p.logPluginOutputSize(fmt.Sprintf("%d bytes plugin ServiceOutput content written to given output sink", written))
}
// handleErrorsSection is a wrapper around the logic used to handle/process
// the Errors section header and listing.
func (p Plugin) handleErrorsSection(w io.Writer) {
if p.isErrorsHidden() {
p.logAction("Skipping processing of errors section; option to hide errors enabled")
return
}
// If one or more errors were recorded and client code has not opted to
// hide the section ...
var totalWritten int
writeErrorToOutputSink := func(err error) {
written, writeErr := fmt.Fprintf(w, "* %v%s", err, CheckOutputEOL)
if writeErr != nil {
panic("Failed to write LastError field content to given output sink")
}
totalWritten += written
}
written, writeErr := fmt.Fprintf(w,
"%s%s**%s**%s%s",
CheckOutputEOL,
CheckOutputEOL,
p.getErrorsLabelText(),
CheckOutputEOL,
CheckOutputEOL,
)
if writeErr != nil {
panic("Failed to write errors section label to given output sink")
}
totalWritten += written
if p.LastError != nil {
writeErrorToOutputSink(p.LastError)
}
// Process any non-nil errors in the collection.
for _, err := range p.Errors {
if err != nil {
writeErrorToOutputSink(err)
}
}
p.logPluginOutputSize(fmt.Sprintf("%d bytes total plugin errors content written to given output sink", totalWritten))
}
// handleThresholdsSection is a wrapper around the logic used to
// handle/process the Thresholds section header and listing.
func (p Plugin) handleThresholdsSection(w io.Writer) {
switch {
case p.LongServiceOutput == "":
p.logAction("Skipping emission of thresholds section; LongServiceOutput is empty")
return
case p.isThresholdsSectionHidden():
p.logAction("Skipping emission of thresholds section; option to hide errors enabled")
return
}
// If one or more threshold values were recorded and client code has
// not opted to hide the section ...
var totalWritten int
written, err := fmt.Fprintf(w, "%s**%s**%s%s",
CheckOutputEOL,
p.getThresholdsLabelText(),
CheckOutputEOL,
CheckOutputEOL,
)
if err != nil {
panic("Failed to write thresholds section label to given output sink")
}
totalWritten += written
if p.CriticalThreshold != "" {
written, err := fmt.Fprintf(w, "* %s: %v%s",
StateCRITICALLabel,
p.CriticalThreshold,
CheckOutputEOL,
)
if err != nil {
panic("Failed to write thresholds section label to given output sink")
}
totalWritten += written
}
if p.WarningThreshold != "" {
warningThresholdText := fmt.Sprintf(
"* %s: %v%s",
StateWARNINGLabel,
p.WarningThreshold,
CheckOutputEOL,
)
written, err := fmt.Fprint(w, warningThresholdText)
if err != nil {
panic("Failed to write thresholds section label to given output sink")
}
totalWritten += written
}
p.logPluginOutputSize(fmt.Sprintf("%d bytes plugin thresholds section content written to given output sink", totalWritten))
}
// handleLongServiceOutput is a wrapper around the logic used to
// handle/process the LongServiceOutput content.
func (p Plugin) handleLongServiceOutput(w io.Writer) {
// Early exit if there is no content to emit.
if p.LongServiceOutput == "" {
p.logAction("Skipping processing of LongServiceOutput; LongServiceOutput is empty")
return
}
var totalWritten int
// Hide section header/label if threshold and error values were not
// specified by client code, if client code opted to explicitly hide
// threshold or error sections or if no encoded payload content was
// provided; there is no need to use a header to separate the
// LongServiceOutput from those sections if they are not displayed (or
// provided in the case of an encoded payload).
//
// If we hide the section header, we still provide some padding to
// prevent the LongServiceOutput from running up against the
// ServiceOutput content.
switch {
case !p.isThresholdsSectionHidden() || !p.isErrorsHidden() || !p.isPayloadSectionHidden():
written, err := fmt.Fprintf(w,
"%s**%s**%s",
CheckOutputEOL,
p.getDetailedInfoLabelText(),
CheckOutputEOL,
)
if err != nil {
panic("Failed to write LongServiceOutput section label to given output sink")
}
totalWritten += written
default:
written, err := fmt.Fprint(w, CheckOutputEOL)
if err != nil {
panic("Failed to write LongServiceOutput section label spacer to given output sink")
}
totalWritten += written
}
// Note: fmt.Println() (and fmt.Fprintln()) has the same issue as `\n`:
// Nagios seems to interpret them literally instead of emitting an actual
// newline. We work around that by using fmt.Fprintf() for output that is
// intended for display within the Nagios web UI.
written, err := fmt.Fprintf(w,
"%s%v%s",
CheckOutputEOL,
p.LongServiceOutput,
CheckOutputEOL,
)
if err != nil {
panic("Failed to write LongServiceOutput field content to given output sink")
}
totalWritten += written
p.logPluginOutputSize(fmt.Sprintf("%d bytes plugin LongServiceOutput content written to given output sink", totalWritten))
}
// handleEncodedPayload is a wrapper around the logic used to handle/process
// any user-provided content to be encoded and included in the plugin output.
func (p Plugin) handleEncodedPayload(w io.Writer) {
// Early exit if there is no content to process.
if p.encodedPayloadBuffer.Len() == 0 {
p.logAction("Skipping processing of encoded payload buffer; buffer is empty")
return
}
p.logPluginOutputSize(fmt.Sprintf("%d bytes unencoded EncodedPayload content before compression attempt", p.encodedPayloadBuffer.Len()))
// We opt to continue with original data instead of failing due to a
// compression error; failing at this stage loses all results gathered by
// the plugin.
payloadData := p.compressPayloadBufferOrFallback()
p.logPluginOutputSize(fmt.Sprintf("%d bytes EncodedPayload data retrieved", len(payloadData)))
leftDelimiter := p.getEncodedPayloadDelimiterLeft()
rightDelimiter := p.getEncodedPayloadDelimiterRight()
encodedWithDelimiters := encodeASCII85(
payloadData,
leftDelimiter,
rightDelimiter,
)
p.logPluginOutputSize(fmt.Sprintf("%d bytes EncodedPayload data encoded", len(encodedWithDelimiters)))
var totalWritten int
// Hide section header/label if no payload was specified.
//
// If we hide the section header, we still provide some padding to prevent
// this output from running up against the LongServiceOutput content.
switch {
case p.encodedPayloadBuffer.Len() > 0:
written, err := fmt.Fprintf(w,
"%s**%s**%s",
CheckOutputEOL,
p.getEncodedPayloadLabelText(),
CheckOutputEOL,
)
if err != nil {
panic("Failed to write EncodedPayload section label to given output sink")
} else {
p.logPluginOutputSize(fmt.Sprintf("%d bytes EncodedPayload section header written", len(encodedWithDelimiters)))
}
totalWritten += written
// Note: fmt.Println() (and fmt.Fprintln()) has the same issue as
// `\n`: Nagios seems to interpret them literally instead of emitting
// an actual newline. We work around that by using fmt.Fprintf() for
// output that is intended for display within the Nagios web UI.
written, err = fmt.Fprintf(w,
"%s%v%s",
CheckOutputEOL,
encodedWithDelimiters,
CheckOutputEOL,
)
if err != nil {
panic("Failed to write EncodedPayload content to given output sink")
}
totalWritten += written
default:
written, err := fmt.Fprint(w, CheckOutputEOL)
if err != nil {
panic("Failed to write EncodedPayload section spacer to given output sink")
}
totalWritten += written
}
p.logPluginOutputSize(fmt.Sprintf("%d bytes plugin EncodedPayload content written to given output sink", totalWritten))
}
// handlePerformanceData is a wrapper around the logic used to
// handle/process plugin Performance Data.
func (p *Plugin) handlePerformanceData(w io.Writer) {
// We require that a one-line summary is set by client code before
// emitting performance data metrics.
if strings.TrimSpace(p.ServiceOutput) == "" {
p.logAction("Skipping processing of performance data; ServiceOutput is empty")
return
}
// If the value is available, use it, otherwise this is a NOOP.
p.tryAddDefaultTimeMetric()
// If no metrics have been collected by this point we have nothing further
// to do.
if len(p.perfData) == 0 {
p.logAction("Skipping processing of performance data; perfdata collection is empty")
return
}
var totalWritten int
// Performance data metrics are appended to plugin output. These
// metrics are provided as a single line, leading with a pipe
// character, a space and one or more metrics each separated from
// another by a single space.
written, err := fmt.Fprint(w, " |")
if err != nil {
panic("Failed to write performance data content to given output sink")
}
totalWritten += written
// Sort performance data values prior to emitting them so that the
// output is consistent across plugin execution.
perfData := p.getSortedPerfData()
for _, pd := range perfData {
written, err = fmt.Fprint(w, pd.String())
if err != nil {
panic("Failed to write performance data content to given output sink")
}
totalWritten += written
}
// Add final trailing newline to satisfy Nagios plugin output format.
written, err = fmt.Fprint(w, CheckOutputEOL)
if err != nil {
panic("Failed to write performance data content to given output sink")
}
totalWritten += written
p.logPluginOutputSize(fmt.Sprintf("%d bytes plugin performance data content written to given output sink", totalWritten))
}
// isThresholdsSectionHidden indicates whether the Thresholds section should
// be omitted from output.
func (p Plugin) isThresholdsSectionHidden() bool {
if p.hideThresholdsSection || (p.WarningThreshold == "" && p.CriticalThreshold == "") {
return true
}
return false
}
// isErrorsHidden indicates whether the Thresholds section should be omitted
// from output.
func (p Plugin) isErrorsHidden() bool {
if p.hideErrorsSection || (len(p.Errors) == 0 && p.LastError == nil) {
return true
}
return false
}
// isPayloadSectionHidden indicates whether the Payload section should be
// omitted from output.
func (p Plugin) isPayloadSectionHidden() bool {
return p.encodedPayloadBuffer.Len() == 0
}
// getThresholdsLabelText retrieves the custom thresholds label text if set,
// otherwise returns the default value.
func (p Plugin) getThresholdsLabelText() string {
switch {
case p.thresholdsLabel != "":
return p.thresholdsLabel
default:
return defaultThresholdsLabel
}
}
// getErrorsLabelText retrieves the custom errors label text if set, otherwise
// returns the default value.
func (p Plugin) getErrorsLabelText() string {
switch {
case p.errorsLabel != "":
return p.errorsLabel
default:
return defaultErrorsLabel
}
}
// getErrorsLabelText retrieves the custom detailed info label text if set,
// otherwise returns the default value.
func (p Plugin) getDetailedInfoLabelText() string {
switch {
case p.detailedInfoLabel != "":
return p.detailedInfoLabel
default:
return defaultDetailedInfoLabel
}
}
// getEncodedPayloadLabelText retrieves the custom encoded payload label text
// if set, otherwise returns the default value.
func (p Plugin) getEncodedPayloadLabelText() string {
switch {
case p.encodedPayloadLabel != "":
return p.encodedPayloadLabel
default:
return defaultEncodedPayloadLabel
}
}
// SetThresholdsLabel overrides the default thresholds label text.
func (p *Plugin) SetThresholdsLabel(newLabel string) {
p.thresholdsLabel = newLabel
}
// SetErrorsLabel overrides the default errors label text.
func (p *Plugin) SetErrorsLabel(newLabel string) {
p.errorsLabel = newLabel
}
// SetDetailedInfoLabel overrides the default detailed info label text.
func (p *Plugin) SetDetailedInfoLabel(newLabel string) {
p.detailedInfoLabel = newLabel
}
// SetEncodedPayloadLabel overrides the default encoded payload label text.
func (p *Plugin) SetEncodedPayloadLabel(newLabel string) {
p.encodedPayloadLabel = newLabel
}
// HideThresholdsSection indicates that client code has opted to hide the
// thresholds section, regardless of whether values were previously provided
// for display.
func (p *Plugin) HideThresholdsSection() {
p.hideThresholdsSection = true
}
// HideErrorsSection indicates that client code has opted to hide the errors
// section, regardless of whether values were previously provided for display.
func (p *Plugin) HideErrorsSection() {
p.hideErrorsSection = true
}
// getSortedPerfData returns a sorted copy of the performance data metrics.
func (p Plugin) getSortedPerfData() []PerformanceData {
keys := make([]string, 0, len(p.perfData))
perfData := make([]PerformanceData, 0, len(p.perfData))
for k := range p.perfData {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
pd := p.perfData[key]
perfData = append(perfData, pd)
}
return perfData
}