forked from olorin/nagiosplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
95 lines (83 loc) · 2.7 KB
/
result.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
package nagiosplugin
import "fmt"
// Status represents a plugin exit status.
type Status uint
// https://nagios-plugins.org/doc/guidelines.html#AEN78
const (
OK Status = iota
WARNING
CRITICAL
UNKNOWN
)
type (
// Check results are ordered by severity: only the most severe check
// result will be captured in the plugin's exit status. A status
// policy is used to define severity as a function of check status.
// Higher relative statusSeverity values assign higher severity to a
// status. (Absolute values are insignificant.)
statusSeverity uint
statusPolicy map[Status]statusSeverity
)
// NewDefaultStatusPolicy returns a status policy that assigns relative
// severity in accordance with conventional Nagios plugin return codes.
// Statuses associated with higher return codes are more severe.
func NewDefaultStatusPolicy() *statusPolicy {
return &statusPolicy{
OK: statusSeverity(OK),
WARNING: statusSeverity(WARNING),
CRITICAL: statusSeverity(CRITICAL),
UNKNOWN: statusSeverity(UNKNOWN),
}
}
// NewOUWCStatusPolicy returns a status policy similar to that returned
// by NewDefaultStatusPolicy with one difference: the UNKNOWN check
// status is demoted in severity such that any WARNING or CRITICAL check
// status will take priority.
func NewOUWCStatusPolicy() *statusPolicy {
pol, _ := NewStatusPolicy([]Status{OK, UNKNOWN, WARNING, CRITICAL})
return pol
}
// NewStatusPolicy returns a status policy that assigns relative
// severity in accordance with a user-configurable prioritised slice.
// Check statuses must be listed in ascending severity order.
func NewStatusPolicy(statuses []Status) (*statusPolicy, error) {
newPol := make(statusPolicy)
for i, status := range statuses {
newPol[status] = statusSeverity(i)
}
// Ensure all statuses are covered by the new policy.
defaultPol := NewDefaultStatusPolicy()
for status := range *defaultPol {
_, ok := newPol[status]
if !ok {
return nil, fmt.Errorf("missing status: %v", status)
}
}
return &newPol, nil
}
// Returns string representation of a Status. Panics if given an invalid
// status (this will be recovered in check.Finish if it has been deferred).
func (s Status) String() string {
switch s {
case OK:
return "OK"
case WARNING:
return "WARNING"
case CRITICAL:
return "CRITICAL"
case UNKNOWN:
return "UNKNOWN"
}
panic("Invalid nagiosplugin.Status.")
}
// Result encapsulates a machine-readable result code and a
// human-readable description of a problem. A check may have multiple
// Results. Only the most severe Result will be reported on the first
// line of plugin output and in the plugin's exit status.
type Result struct {
status Status
message string
}
func (r Result) Status() Status {
return r.status
}