/
icinga.go
202 lines (170 loc) · 4.58 KB
/
icinga.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
package icinga
import (
"fmt"
"log"
"math"
"strconv"
"strings"
"github.com/tidwall/gjson"
)
type Threshold struct {
Raw string
Up float64
Down float64
Negate bool
}
type Icinga struct {
ExitCode int
ExitWording string
PluginOutput string
LongPluginOutput string
PerfData string
hasLongPluginOutput bool
Warning Threshold
Critical Threshold
}
func NewIcinga(output, warning, critical string) *Icinga {
icinga := &Icinga{
ExitCode: 0,
ExitWording: "OK",
PluginOutput: output,
}
icinga.Warning = parseThreshold(warning)
icinga.Critical = parseThreshold(critical)
return icinga
}
func (i *Icinga) GetStatus() int {
return i.ExitCode
}
func (i *Icinga) Evaluate(value float64, problemMessagem, outputOK, outputWarning, outputCritical string) {
if ((value < i.Critical.Down || value > i.Critical.Up) && !i.Critical.Negate) ||
((value >= i.Critical.Down && value <= i.Critical.Up) && i.Critical.Negate) {
i.setStatus(2, outputCritical, "")
i.PluginOutput = outputCritical
} else if ((value < i.Warning.Down || value > i.Warning.Up) && !i.Warning.Negate) ||
((value >= i.Warning.Down && value <= i.Warning.Up) && i.Warning.Negate) {
i.setStatus(1, outputWarning, "")
i.PluginOutput = outputWarning
} else {
i.PluginOutput = outputOK
}
}
func (i *Icinga) MultiEvaluate(value float64, problemMessage, outputOK, outputWarning, outputCritical string) {
if ((value < i.Critical.Down || value > i.Critical.Up) && !i.Critical.Negate) ||
((value >= i.Critical.Down && value <= i.Critical.Up) && i.Critical.Negate) {
i.setStatus(2, outputCritical, "")
i.addLongPluginOutput(outputCritical)
i.PluginOutput = problemMessage
} else if ((value < i.Warning.Down || value > i.Warning.Up) && !i.Warning.Negate) ||
((value >= i.Warning.Down && value <= i.Warning.Up) && i.Warning.Negate) {
i.setStatus(1, outputWarning, "")
i.addLongPluginOutput(outputWarning)
i.PluginOutput = problemMessage
} else {
i.addLongPluginOutput(outputOK)
}
}
func (i *Icinga) GenerateOutput(perfData bool) {
output := i.PluginOutput
if i.PerfData != "" && perfData {
output = output + " | " + i.PerfData
}
fmt.Println(i.ExitWording + " - " + output)
if i.LongPluginOutput != "" {
fmt.Println(i.LongPluginOutput)
}
}
func (i *Icinga) ParseToPerfData(data gjson.Result) {
data.ForEach(func(key, value gjson.Result) bool {
i.PerfData += fmt.Sprintf("'%s'=%f;;;;", key.String(), value.Float())
return true
})
}
func (i *Icinga) addLongPluginOutput(message string) {
if i.hasLongPluginOutput {
i.LongPluginOutput += "\n" + message
} else {
i.LongPluginOutput = message
i.hasLongPluginOutput = true
}
}
func (i *Icinga) setStatus(code int, output, longOutput string) {
if code == 3 {
i.ExitCode = code
i.PluginOutput = output
i.ExitWording = i.parseCodeToWord(code)
}
if code > i.ExitCode && code != 3 {
i.ExitCode = code
i.PluginOutput = output
i.ExitWording = i.parseCodeToWord(code)
}
if longOutput != "" {
i.LongPluginOutput += longOutput + "\n"
}
}
func (i *Icinga) parseCodeToWord(code int) string {
switch code {
case 0:
return "OK"
case 1:
return "WARNING"
case 2:
return "CRITICAL"
default:
return "UNKNOWN"
}
}
func parseThreshold(threshold string) Threshold {
thresholdCopy := threshold
number, err := strconv.ParseFloat(thresholdCopy, 64)
if err != nil {
negate := false
// threshold is more complicated and should be more parsed
if strings.HasPrefix(thresholdCopy, "@") {
// is a negate of the threshold. Add negation and delete the token
thresholdCopy = thresholdCopy[1:]
negate = true
}
// split into parts to parse the value before and after the colon
parts := strings.Split(thresholdCopy, ":")
up := 0.
down := 0.
if len(parts) == 2 {
if parts[0] == "~" {
down = -math.MaxFloat64
} else {
firstPart, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
log.Fatal("Cannot parse first part of threshold", err)
}
down = firstPart
}
if parts[1] == "" {
up = math.MaxFloat64
} else {
secondPart, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
log.Fatal("Cannot parse second part of threshold", err)
}
up = secondPart
}
}
return Threshold{
Raw: threshold,
Up: up,
Down: down,
Negate: negate,
}
} else {
return Threshold{
Raw: threshold,
Up: number,
Down: 0,
Negate: false,
}
}
}
func (i *Icinga) AddPerfData(value float64, data string) {
i.PerfData += fmt.Sprintf("'%s'=%f;%f;%f;;, ", data, value, i.Warning.Up, i.Critical.Up)
}