This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnagios_test.go
235 lines (213 loc) · 7.26 KB
/
nagios_test.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
package main
import (
"strings"
"testing"
)
func TestNagiosOK(t *testing.T) {
z := zpool{
name: "tank",
faulted: -1, // set to -1 to make sure we actually test the all-ONLINE case
}
// Test all ONLINE
z.checkHealth("ONLINE")
z.getCapacity("51%")
z.getFaulted(` pool: tank
state: ONLINE
scan: scrub repaired 0 in 1h1m with 0 errors on Thu Jan 1 13:37:00 1970
config:
NAME STATE READ WRITE CKSUM
zones ONLINE 0 0 0
raidz2-0 ONLINE 0 0 0
c0t5000C5006A6E87D9d0 ONLINE 0 0 0
c0t5000C50024CAAFFCd0 ONLINE 0 0 0
c0t5000CCA249D27B4Ed0 ONLINE 0 0 0
c0t5000C5004425F6F6d0 ONLINE 0 0 0
c0t5000C500652DD0EFd0 ONLINE 0 0 0
c0t50014EE25A580141d0 ONLINE 0 0 0
errors: No known data errors`)
message, exitcode := z.NagiosFormat()
if message != "OK: tank ONLINE, capacity: 51%" {
t.Errorf("Unexpected Nagios status. Got: %s", message)
}
if exitcode != 0 {
t.Errorf("Unexpected Exit code, got %d, should be 0", exitcode)
}
}
func TestNagiosWarningCap(t *testing.T) {
z := zpool{
name: "tank",
faulted: -1, // set to -1 to make sure we actually test the all-ONLINE case
}
z.checkHealth("ONLINE")
z.getCapacity("78%")
z.getFaulted(` pool: tank
state: ONLINE
scan: scrub repaired 0 in 1h1m with 0 errors on Thu Jan 1 13:37:00 1970
config:
NAME STATE READ WRITE CKSUM
zones ONLINE 0 0 0
raidz2-0 ONLINE 0 0 0
c0t5000C5006A6E87D9d0 ONLINE 0 0 0
c0t5000C50024CAAFFCd0 ONLINE 0 0 0
c0t5000CCA249D27B4Ed0 ONLINE 0 0 0
c0t5000C5004425F6F6d0 ONLINE 0 0 0
c0t5000C500652DD0EFd0 ONLINE 0 0 0
c0t50014EE25A580141d0 ONLINE 0 0 0
errors: No known data errors`)
message, exitcode := z.NagiosFormat()
if message != "WARNING: tank ONLINE, capacity: 78%" {
t.Errorf("Unexpected Nagios status. Got: %s", message)
}
if exitcode != 1 {
t.Errorf("Unexpected Exit code, got %d, should be 1", exitcode)
}
}
func TestNagiosCriticalCap(t *testing.T) {
z := zpool{
name: "tank",
faulted: -1, // set to -1 to make sure we actually test the all-ONLINE case
}
z.checkHealth("ONLINE")
z.getCapacity("88%")
z.getFaulted(` pool: tank
state: ONLINE
scan: scrub repaired 0 in 1h1m with 0 errors on Thu Jan 1 13:37:00 1970
config:
NAME STATE READ WRITE CKSUM
zones ONLINE 0 0 0
raidz2-0 ONLINE 0 0 0
c0t5000C5006A6E87D9d0 ONLINE 0 0 0
c0t5000C50024CAAFFCd0 ONLINE 0 0 0
c0t5000CCA249D27B4Ed0 ONLINE 0 0 0
c0t5000C5004425F6F6d0 ONLINE 0 0 0
c0t5000C500652DD0EFd0 ONLINE 0 0 0
c0t50014EE25A580141d0 ONLINE 0 0 0
errors: No known data errors`)
message, exitcode := z.NagiosFormat()
if message != "CRITICAL: tank ONLINE, capacity: 88%" {
t.Errorf("Unexpected Nagios status. Got: %s", message)
}
if exitcode != 2 {
t.Errorf("Unexpected Exit code, got %d, should be 2", exitcode)
}
}
func TestNagiosCriticalDegraded(t *testing.T) {
z := zpool{
name: "tank",
faulted: -1, // set to -1 to make sure we actually test the all-ONLINE case
}
z.checkHealth("DEGRADED")
z.getCapacity("32%")
z.getFaulted(` pool: tank
state: DEGRADED
scan: scrub repaired 0 in 1h1m with 0 errors on Thu Jan 1 13:37:00 1970
config:
NAME STATE READ WRITE CKSUM
zones DEGRADED 0 0 0
raidz2-0 ONLINE 0 0 0
c0t5000C5006A6E87D9d0 FAULTED 0 0 0
c0t5000C50024CAAFFCd0 ONLINE 0 0 0
c0t5000CCA249D27B4Ed0 ONLINE 0 0 0
c0t5000C5004425F6F6d0 UNAVAIL 0 0 0
c0t5000C500652DD0EFd0 ONLINE 0 0 0
c0t50014EE25A580141d0 ONLINE 0 0 0
errors: No known data errors`)
message, exitcode := z.NagiosFormat()
if message != "CRITICAL: tank DEGRADED, capacity: 32%, faulted: 2" {
t.Errorf("Unexpected Nagios status. Got: %s", message)
}
if exitcode != 2 {
t.Errorf("Unexpected Exit code, got %d, should be 2", exitcode)
}
}
func TestNagiosCriticalFaulted(t *testing.T) {
z := zpool{
name: "tank",
faulted: -1, // set to -1 to make sure we actually test the all-ONLINE case
}
z.checkHealth("FAULTED")
z.getCapacity("43%")
z.getFaulted(` pool: tank
state: FAULTED
scan: scrub repaired 0 in 1h1m with 0 errors on Thu Jan 1 13:37:00 1970
config:
NAME STATE READ WRITE CKSUM
zones DEGRADED 0 0 0
raidz2-0 ONLINE 0 0 0
c0t5000C5006A6E87D9d0 FAULTED 0 0 0
c0t5000C50024CAAFFCd0 ONLINE 0 0 0
c0t5000CCA249D27B4Ed0 FAULTED 0 0 0
c0t5000C5004425F6F6d0 FAULTED 0 0 0
c0t5000C500652DD0EFd0 ONLINE 0 0 0
c0t50014EE25A580141d0 ONLINE 0 0 0
errors: No known data errors`)
message, exitcode := z.NagiosFormat()
if message != "CRITICAL: tank FAULTED, capacity: 43%, faulted: 3" {
t.Errorf("Unexpected Nagios status. Got: %s", message)
}
if exitcode != 2 {
t.Errorf("Unexpected Exit code, got %d, should be 2", exitcode)
}
}
func TestNagiosOutput(t *testing.T) {
nagiosStatus := map[string]int{
"OK:": 0,
"WARNING:": 1,
"CRITICAL:": 2,
"UNKNOWN:": 3,
}
pools := map[string]zpool{
"OK: tank ONLINE, capacity: 43%": {
name: "tank",
faulted: 0,
capacity: 43,
healthy: true,
status: "ONLINE",
},
"WARNING: zones ONLINE, capacity: 78%": {
name: "zones",
faulted: 0,
capacity: 78,
healthy: true,
status: "ONLINE",
},
"CRITICAL: tank ONLINE, capacity: 83%": {
name: "tank",
faulted: 0,
capacity: 83,
healthy: true,
status: "ONLINE",
},
"CRITICAL: tank DEGRADED, capacity: 43%, faulted: 1": {
name: "tank",
faulted: 1,
capacity: 43,
healthy: false,
status: "DEGRADED",
},
"CRITICAL: tank FAULTED, capacity: 13%, faulted: 2": {
name: "tank",
faulted: 2,
capacity: 13,
healthy: false,
status: "FAULTED",
},
"UNKNOWN: zones OTHERSTATE, capacity: -1%, faulted: 1": {
name: "zones",
faulted: 1,
capacity: -1,
healthy: true,
status: "OTHERSTATE",
},
}
for status, pool := range pools {
message, exitcode := pool.NagiosFormat()
if message != status {
t.Errorf("Unexpected Nagios status. Got: '%s', should be: '%s'", message, status)
}
s := strings.Fields(message)[0]
if exitcode != nagiosStatus[s] {
t.Errorf("Unexpected Exit code, got %d, should be %d", exitcode, nagiosStatus[s])
}
}
}