-
Notifications
You must be signed in to change notification settings - Fork 3
/
nagios.go
575 lines (492 loc) · 19.9 KB
/
nagios.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// 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 (
"errors"
"fmt"
"io"
"os"
"runtime/debug"
"strings"
"time"
)
// Nagios plugin/service check states. These constants replicate the values
// from utils.sh which is normally found at one of these two locations,
// depending on which Linux distribution you're using:
//
// - /usr/lib/nagios/plugins/utils.sh
// - /usr/local/nagios/libexec/utils.sh
//
// See also http://nagios-plugins.org/doc/guidelines.html
const (
StateOKExitCode int = 0
StateWARNINGExitCode int = 1
StateCRITICALExitCode int = 2
StateUNKNOWNExitCode int = 3
StateDEPENDENTExitCode int = 4
)
// Nagios plugin/service check state "labels". These constants are provided as
// an alternative to using literal state strings throughout client application
// code.
const (
StateOKLabel string = "OK"
StateWARNINGLabel string = "WARNING"
StateCRITICALLabel string = "CRITICAL"
StateUNKNOWNLabel string = "UNKNOWN"
StateDEPENDENTLabel string = "DEPENDENT"
)
// CheckOutputEOL is the newline character(s) used with formatted service and
// host check output. Based on previous testing, Nagios treats LF newlines
// (without a leading space) within the `$LONGSERVICEOUTPUT$` macro as literal
// values instead of parsing them for display purposes.
//
// Using DOS EOL values with fmt.Fprintf() (or fmt.Fprintln()) gives expected
// formatting results in the Nagios Core web UI, but results in double
// newlines in Nagios XI output (see GH-109). Using a UNIX EOL with a single
// leading space appears to give the intended results for both Nagios Core and
// Nagios XI.
const CheckOutputEOL string = " \n"
// Default header text for various sections of the output if not overridden.
const (
defaultThresholdsLabel string = "THRESHOLDS"
defaultErrorsLabel string = "ERRORS"
defaultDetailedInfoLabel string = "DETAILED INFO"
)
// Default performance data metrics emitted if not specified by client code.
const (
defaultTimeMetricLabel string = "time"
defaultTimeMetricUnitOfMeasurement string = "ms"
)
// Sentinel error collection. Exported for potential use by client code to
// detect & handle specific error scenarios.
var (
// ErrPanicDetected indicates that client code has an unhandled panic and
// that this library detected it before it could cause the plugin to
// abort. This error is included in the LongServiceOutput emitted by the
// plugin.
ErrPanicDetected = errors.New("plugin crash/panic detected")
// ErrPerformanceDataMissingLabel indicates that client code did not
// provide a PerformanceData value in the expected format; the label for
// the label/value pair is missing.
ErrPerformanceDataMissingLabel = errors.New("provided performance data missing required label")
// ErrPerformanceDataMissingValue indicates that client code did not
// provide a PerformanceData value in the expected format; the value for
// the label/value pair is missing.
ErrPerformanceDataMissingValue = errors.New("provided performance data missing required value")
// ErrNoPerformanceDataProvided indicates that client code did not provide
// the expected PerformanceData value(s).
ErrNoPerformanceDataProvided = errors.New("no performance data provided")
// ErrInvalidPerformanceDataFormat indicates that a given performance data
// metric is not in a supported format.
ErrInvalidPerformanceDataFormat = errors.New("invalid performance data format")
// ErrInvalidRangeThreshold indicates that a given range threshold is not in a supported format.
ErrInvalidRangeThreshold = errors.New("invalid range threshold")
// TODO: Should we use field-specific errors or is the more general
// ErrInvalidPerformanceDataFormat "good enough" ? Wrapped versions of
// that error will likely already indicate which field is a problem, but
// that approach won't lend itself to automatic behavior regarding issues
// with a specific field.
//
// ErrInvalidPerformanceDataLabelField = errors.New("invalid field Label in parsed performance data")
// ErrInvalidPerformanceDataValueField = errors.New("invalid field Value in parsed performance data")
// ErrInvalidPerformanceDataUoMField = errors.New("invalid field UnitOfMeasurement in parsed performance data")
// ErrInvalidPerformanceDataWarnField = errors.New("invalid field Warn in parsed performance data")
// ErrInvalidPerformanceDataCritField = errors.New("invalid field Crit in parsed performance data")
// ErrInvalidPerformanceDataMinField = errors.New("invalid field Min in parsed performance data")
// ErrInvalidPerformanceDataMaxField = errors.New("invalid field Max in parsed performance data")
)
// ServiceState represents the status label and exit code for a service check.
type ServiceState struct {
// Label maps directly to one of the supported Nagios state labels.
Label string
// ExitCode is the exit or exit status code associated with a Nagios
// service check.
ExitCode int
}
// ExitCallBackFunc represents a function that is called as a final step
// before application termination so that branding information can be emitted
// for inclusion in the notification. This helps identify which specific
// application (and its version) that is responsible for the notification.
type ExitCallBackFunc func() string
// Plugin represents the state of a monitoring plugin, including the most
// recent error and the final intended plugin state.
type Plugin struct {
// outputSink is the user-specified or fallback target for plugin output.
outputSink io.Writer
// start tracks when the associated plugin begins executing. This value is
// used to generate a default `time` performance data metric (which can be
// overridden by client code).
start time.Time
// LastError is the last error encountered which should be reported as
// part of ending the service check (e.g., "Failed to connect to XYZ to
// check contents of Inbox").
//
// Deprecated: Use Errors field or AddError method instead.
LastError error
// Errors is a collection of one or more recorded errors to be displayed
// in LongServiceOutput as a list when ending the service check.
Errors []error
// ExitStatusCode is the exit or exit status code provided to the Nagios
// instance that calls this service check. These status codes indicate to
// Nagios "state" the service is considered to be in. The most common
// states are OK (0), WARNING (1) and CRITICAL (2).
ExitStatusCode int
// ServiceOutput is the first line of text output from the last service
// check (i.e. "Ping OK").
ServiceOutput string
// LongServiceOutput is the full text output (aside from the first line)
// from the last service check.
LongServiceOutput string
// perfData is the collection of zero or more PerformanceData values
// generated by the plugin. Each entry in the collection is unique.
perfData map[string]PerformanceData
// WarningThreshold is the value used to determine when the service check
// has crossed between an existing state into a WARNING state. This value
// is used for display purposes.
WarningThreshold string
// CriticalThreshold is the value used to determine when the service check
// has crossed between an existing state into a CRITICAL state. This value
// is used for display purposes.
CriticalThreshold string
// thresholdLabel is an optional custom label used in place of the
// standard text prior to a list of threshold values.
thresholdsLabel string
// errorsLabel is an optional custom label used in place of the standard
// text prior to a list of recorded error values.
errorsLabel string
// detailedInfoLabel is an optional custom label used in place of the
// standard text prior to emitting LongServiceOutput.
detailedInfoLabel string
// hideThresholdsSection indicates whether client code has opted to hide
// the thresholds section, regardless of whether client code previously
// specified values for display.
hideThresholdsSection bool
// hideErrorsSection indicates whether client code has opted to hide the
// errors section, regardless of whether client code previously specified
// values for display.
hideErrorsSection bool
// shouldSkipOSExit is intended to support tests where actually performing
// the final os.Exit(x) call results in a panic (Go 1.16+). If set,
// calling os.Exit(x) is skipped and a message is logged to os.Stderr
// instead.
shouldSkipOSExit bool
// BrandingCallback is a function that is called before application
// termination to emit branding details at the end of the notification.
// See also ExitCallBackFunc.
BrandingCallback ExitCallBackFunc
}
// NewPlugin constructs a new Plugin value in the same way that client code
// has been using this library. We also record a default time performance data
// metric. This default metric is ignored if supplied by client code.
func NewPlugin() *Plugin {
es := Plugin{
start: time.Now(),
LastError: nil,
ExitStatusCode: StateOKExitCode,
}
return &es
}
// ReturnCheckResults is intended to provide a reliable way to return a
// desired exit code from applications used as Nagios plugins. In most cases,
// this method should be registered as the first deferred function in client
// code. See remarks regarding "masking" or "swallowing" application panics.
//
// Since Nagios relies on plugin exit codes to determine success/failure of
// checks, the approach that is most often used with other languages is to use
// something like Using os.Exit() directly and force an early exit of the
// application with an explicit exit code. Using os.Exit() directly in Go does
// not run deferred functions. Go-based plugins that do not rely on deferring
// function calls may be able to use os.Exit(), but introducing new
// dependencies later could introduce problems if those dependencies rely on
// deferring functions.
//
// Before calling this method, client code should first set appropriate field
// values on the receiver. When called, this method will process them and exit
// with the desired exit code and status output.
//
// To repeat, if scheduled via defer, this method should be registered first;
// because this method calls os.Exit to set the intended plugin exit state, no
// other deferred functions will have an opportunity to run, so register this
// method first so that when deferred, it will be run last (FILO).
//
// Because this method is (or should be) deferred first within client code, it
// will run after all other deferred functions. It will also run before a
// panic in client code forces the application to exit. As already noted, this
// method calls os.Exit to set the plugin exit state. Because os.Exit forces
// the application to terminate immediately without running other deferred
// functions or processing panics, this "masks", "swallows" or "blocks" panics
// from client code from surfacing. This method checks for unhandled panics
// and if found, overrides exit state details from client code and surfaces
// details from the panic instead as a CRITICAL state.
func (p *Plugin) ReturnCheckResults() {
var output strings.Builder
// ##################################################################
// 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() and fmt.Fprint()
// for output that is intended for display within the Nagios web UI.
// ##################################################################
// Check for unhandled panic in client code. If present, override
// Plugin and make clear that the client code/plugin crashed.
if err := recover(); err != nil {
p.AddError(fmt.Errorf("%w: %s", ErrPanicDetected, err))
p.ServiceOutput = fmt.Sprintf(
"%s: plugin crash detected. See details via web UI or run plugin manually via CLI.",
StateCRITICALLabel,
)
// Gather stack trace associated with panic.
stackTrace := debug.Stack()
// Wrap stack trace details in an attempt to prevent these details
// from being interpreted as formatting characters when passed through
// web UI, text, email, Teams, etc. We use Markdown fenced code blocks
// instead of `<pre>` start/end tags because Nagios strips out angle
// brackets (due to default `illegal_macro_output_chars` settings).
p.LongServiceOutput = fmt.Sprintf(
"```%s%s%s%s%s%s```",
CheckOutputEOL,
err,
CheckOutputEOL,
CheckOutputEOL,
stackTrace,
CheckOutputEOL,
)
p.ExitStatusCode = StateCRITICALExitCode
}
p.handleServiceOutputSection(&output)
p.handleErrorsSection(&output)
p.handleThresholdsSection(&output)
p.handleLongServiceOutput(&output)
// If set, call user-provided branding function before emitting
// performance data and exiting application.
if p.BrandingCallback != nil {
_, _ = fmt.Fprintf(&output, "%s%s%s", CheckOutputEOL, p.BrandingCallback(), CheckOutputEOL)
}
p.handlePerformanceData(&output)
// Emit all collected plugin output using user-specified or fallback
// output target.
p.emitOutput(output.String())
// TODO: Should we offer an option to redirect the log message to stderr
// to another error output sink?
//
// TODO: Perhaps just don't emit anything at all?
switch {
case p.shouldSkipOSExit:
_, _ = fmt.Fprintln(os.Stderr, "Skipping os.Exit call as requested.")
default:
os.Exit(p.ExitStatusCode)
}
}
// AddPerfData adds provided performance data to the collection overwriting
// any previous performance data metrics using the same label.
//
// Validation is skipped if requested, otherwise an error is returned if
// validation fails. Validation failure results in no performance data being
// appended. Client code may wish to disable validation if performing this
// step directly.
func (p *Plugin) AddPerfData(skipValidate bool, perfData ...PerformanceData) error {
if len(perfData) == 0 {
return ErrNoPerformanceDataProvided
}
if !skipValidate {
for i := range perfData {
if err := perfData[i].Validate(); err != nil {
return err
}
}
}
if p.perfData == nil {
p.perfData = make(map[string]PerformanceData)
}
for _, pd := range perfData {
p.perfData[strings.ToLower(pd.Label)] = pd
}
return nil
}
// AddError appends provided errors to the collection.
//
// NOTE: Deduplication of errors is *not* performed. The caller is responsible
// for ensuring that a given error is not already recorded in the collection.
func (p *Plugin) AddError(errs ...error) {
p.Errors = append(p.Errors, errs...)
}
// AddUniqueError appends provided errors to the collection if they are not
// already present. If a given error is already in the collection then it will
// be skipped.
//
// Errors are evaluated using case-insensitive string comparison.
func (p *Plugin) AddUniqueError(errs ...error) {
existingErrStrings := make([]string, 0, len(p.Errors))
for i := range p.Errors {
existingErrStrings[i] = p.Errors[i].Error()
}
for _, err := range errs {
if inList(err.Error(), existingErrStrings, true) {
continue
}
p.Errors = append(p.Errors, err)
}
}
// SetOutputTarget assigns a target for Nagios plugin output. By default
// output is emitted to os.Stdout. If given an invalid output target the
// default output target will be used instead.
func (p *Plugin) SetOutputTarget(w io.Writer) {
// Guard against potential nil argument.
if w == nil {
p.outputSink = os.Stdout
return
}
p.outputSink = w
}
// SkipOSExit indicates that the os.Exit(x) step used to signal to Nagios what
// state plugin execution has completed in (e.g., OK, WARNING, ...) should be
// skipped. If skipped, a message is logged to os.Stderr in place of the
// os.Exit(x) call.
//
// Disabling the call to os.Exit is needed by tests to prevent panics in Go
// 1.16 and newer.
func (p *Plugin) SkipOSExit() {
p.shouldSkipOSExit = true
}
// defaultPluginOutputTarget returns the fallback/default plugin output target
// used when a user-specified value is not provided.
func defaultPluginOutputTarget() io.Writer {
return os.Stdout
}
// emitOutput writes final plugin output to the previously set output target.
// No further modifications to plugin output are performed.
func (p Plugin) emitOutput(pluginOutput string) {
// Emit all collected output using user-specified output target. Fall back
// to standard output if not set.
if p.outputSink == nil {
p.outputSink = os.Stdout
}
// Attempt to write to output sink. If this fails, send error to
// os.Stderr. If that fails (however unlikely), we have bigger problems
// and should abort.
_, sinkWriteErr := fmt.Fprint(p.outputSink, pluginOutput)
if sinkWriteErr != nil {
_, stdErrWriteErr := fmt.Fprintf(
os.Stderr,
"Failed to write output to given output sink: %s",
sinkWriteErr.Error(),
)
if stdErrWriteErr != nil {
panic("Failed to initial output sink failure error message to stderr")
}
}
}
// tryAddDefaultTimeMetric inserts a default `time` performance data metric
// into the collection IF client code has not already specified such a value
// AND we have a non-zero start value to use.
func (p *Plugin) tryAddDefaultTimeMetric() {
// We already have an existing time metric, skip replacing it.
if _, hasTimeMetric := p.perfData[defaultTimeMetricLabel]; hasTimeMetric {
return
}
// Our Plugin value was not generated from the constructor, so we do
// not have an internal plugin start time that we can use to generate a
// default time metric.
if p.start.IsZero() {
return
}
if p.perfData == nil {
p.perfData = make(map[string]PerformanceData)
}
p.perfData[defaultTimeMetricLabel] = defaultTimeMetric(p.start)
}
// defaultTimeMetric is a helper function that wraps the logic used to provide
// a default performance data metric that tracks plugin execution time.
func defaultTimeMetric(start time.Time) PerformanceData {
return PerformanceData{
Label: defaultTimeMetricLabel,
Value: fmt.Sprintf("%d", time.Since(start).Milliseconds()),
UnitOfMeasurement: defaultTimeMetricUnitOfMeasurement,
}
}
// SupportedStateLabels returns a list of valid plugin state labels.
func SupportedStateLabels() []string {
return []string{
StateOKLabel,
StateWARNINGLabel,
StateCRITICALLabel,
StateUNKNOWNLabel,
StateDEPENDENTLabel,
}
}
// SupportedExitCodes returns a list of valid plugin exit codes.
func SupportedExitCodes() []int {
return []int{
StateOKExitCode,
StateWARNINGExitCode,
StateCRITICALExitCode,
StateUNKNOWNExitCode,
StateDEPENDENTExitCode,
}
}
// SupportedServiceStates returns a collection of valid plugin service states.
func SupportedServiceStates() []ServiceState {
return []ServiceState{
{
Label: StateOKLabel,
ExitCode: StateOKExitCode,
},
{
Label: StateWARNINGLabel,
ExitCode: StateWARNINGExitCode,
},
{
Label: StateCRITICALLabel,
ExitCode: StateCRITICALExitCode,
},
{
Label: StateUNKNOWNLabel,
ExitCode: StateUNKNOWNExitCode,
},
{
Label: StateDEPENDENTLabel,
ExitCode: StateDEPENDENTExitCode,
},
}
}
// StateLabelToExitCode returns the corresponding plugin exit code for the
// given plugin state label. If an invalid value is provided the
// StateUNKNOWNExitCode value is returned.
func StateLabelToExitCode(label string) int {
switch strings.ToUpper(label) {
case StateOKLabel:
return StateOKExitCode
case StateWARNINGLabel:
return StateWARNINGExitCode
case StateCRITICALLabel:
return StateCRITICALExitCode
case StateUNKNOWNLabel:
return StateUNKNOWNExitCode
case StateDEPENDENTLabel:
return StateDEPENDENTExitCode
default:
return StateUNKNOWNExitCode
}
}
// ExitCodeToStateLabel returns the corresponding plugin state label for the
// given plugin exit code. If an invalid value is provided the
// StateUNKNOWNLabel value is returned.
func ExitCodeToStateLabel(exitCode int) string {
switch exitCode {
case StateOKExitCode:
return StateOKLabel
case StateWARNINGExitCode:
return StateWARNINGLabel
case StateCRITICALExitCode:
return StateCRITICALLabel
case StateUNKNOWNExitCode:
return StateUNKNOWNLabel
case StateDEPENDENTExitCode:
return StateDEPENDENTLabel
default:
return StateUNKNOWNLabel
}
}