-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_zfs
executable file
·349 lines (314 loc) · 10.4 KB
/
check_zfs
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
#!/usr/bin/python
########################################################################
##
## Written by Zachary LaCelle
## Copyright 2016
## Licensed under GPL (see below)
##
## Nagios script to monitor ZFS pools/filesystems
## in Linux.
##
## Tested operating systems/ZFS versions:
## * Ubuntu 14.04.3 LTS, ZFS v5
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
########################################################################
from sys import exit
import subprocess
import argparse
from array import *
from types import *
##
# Commands to run
# CHANGE THESE IF YOU NEED TO
##
zpoolCommand='/sbin/zpool'
zfsCommand='/sbin/zfs'
##
# Variables to print at the end
##
nagiosStatus=('OK','WARNING','CRITICAL','UNKNOWN')
stateNum=0
msg=''
perfdata=''
##
# Filled from command line arguments
##
checkCapacity=False
capWarnThreshold=50
capCritThreshold=80
checkFragmentation=False
fragWarnThreshold=50
fragCritThreshold=80
def CheckArgBounds( valueArr, minVal, maxVal ):
for value in valueArr:
if value < minVal:
return False
elif value > maxVal:
return False
return True
def ConvertToGB( valueStr ):
value = valueStr[:-1]
if valueStr.endswith('G'):
return float(value)
elif valueStr.endswith('T'):
gigs=float(value)*1024
return float(gigs)
elif valueStr.endswith('M'):
gigs=float(value) / 1024.0
return float(gigs)
elif valueStr.endswith('K'):
gigs=float(value) / (1024.0 * 1024.0)
return float(gigs)
def RaiseStateNum( stateNumIn, stateNum ):
if stateNumIn > stateNum:
return stateNumIn
###################################################################################
##
# Parse command line args
##
parser = argparse.ArgumentParser(
prog='check_zfs',
description='Check the ZFS pool specified by an argument.',
epilog='Note that monitor flags (e.g. capacity) require 2 arguments: warning threshold, and critical threshold')
parser.add_argument('--capacity', help="monitor utilization of zpool (%%, int [0-100]", type=int, nargs=2)
parser.add_argument('--fragmentation', help="monitor fragmentation of zpool (%%, int [0-100])", type=int, nargs=2)
parser.add_argument('pool', help="name of the zpool to check", type=str)
args = parser.parse_args()
retVal = True
if args.capacity is not None:
checkCapacity=True
capWarnThreshold=args.capacity[0]
capCritThreshold=args.capacity[1]
capArr = array('i', [capWarnThreshold, capCritThreshold])
retVal = CheckArgBounds(capArr, 0, 100)
if retVal is False:
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Capacity thresholds must be between 0 and 100 (as a percent)."
parser.print_help()
exit(stateNum)
retVal = True
if args.fragmentation is not None:
checkFragmentation=True
fragWarnThreshold=args.fragmentation[0]
fragCritThreshold=args.fragmentation[1]
fragArr = array('i', [fragWarnThreshold, fragCritThreshold])
retVal = CheckArgBounds(fragArr, 0, 100)
if retVal is False:
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Fragmentation thresholds must be between 0 and 100 (as a percent)."
parser.print_help()
exit(stateNum)
###################################################################################
###################################################################################
##
# Get generic info about the ZFS environment
zfsEntries = []
zfsArgs=' list'
fullCommand='sudo -n ' + zfsCommand + zfsArgs
childProcess = subprocess.Popen(fullCommand, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
zfsString = childProcess.communicate()[0]
zfsRetval = childProcess.returncode
if zfsRetval is 1:
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": process must be run as root. Possible solution: add the following to your visudo: nagios ALL=NOPASSWD: /sbin/zfs"
exit(stateNum)
zfsLines = zfsString.splitlines()
for idx, line in enumerate(zfsLines):
if idx != 0:
zfsEntry=line.split()
zfsEntries.append(zfsEntry)
# Make sure the pool we specified is valid
validPool=False
for entry in zfsEntries:
if entry[0] == args.pool:
validPool=True
if not validPool:
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Pool " + args.pool + " is invalid. Please select a valid pool."
exit(stateNum)
###################################################################################
##
# Get info on zpool
zpoolArgs=' list ' + args.pool;
fullCommand='sudo -n ' + zpoolCommand + zpoolArgs
childProcess = subprocess.Popen(fullCommand, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
zpoolString = childProcess.communicate()[0]
zpoolRetval = childProcess.returncode
if zpoolRetval is 1:
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": process must be run as root. Possible solution: add the following to your visudo: nagios ALL=NOPASSWD: /sbin/zpool"
exit(stateNum)
zpoolLines=zpoolString.splitlines()
zpoolMeta=zpoolLines[0].split()
zpoolMetaStr=','.join(zpoolMeta)
zpoolEntry=zpoolLines[1].split()
zpoolEntryStr=','.join(zpoolEntry)
name=''
size=''
alloc=''
free=''
expandsz=''
frag=''
cap=''
dedup=''
health=''
altroot=''
for idx, fieldName in enumerate(zpoolMeta):
if fieldName=='NAME':
name=zpoolEntry[idx]
elif fieldName=='SIZE':
size=zpoolEntry[idx]
elif fieldName=='ALLOC':
alloc=zpoolEntry[idx]
elif fieldName=='FREE':
free=zpoolEntry[idx]
elif fieldName=='EXPANDSZ':
expandsz=zpoolEntry[idx]
elif fieldName=='FRAG':
frag=zpoolEntry[idx]
elif fieldName=='CAP':
cap=zpoolEntry[idx]
elif fieldName=='DEDUP':
dedup=zpoolEntry[idx]
elif fieldName=='HEALTH':
health=zpoolEntry[idx]
elif fieldName=='ALTROOT':
altroot=zpoolEntry[idx]
if name=='':
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Missing required field in zpool output: NAME"
exit(stateNum)
if health=='':
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Missing required field in zpool output: HEALTH"
exit(stateNum)
if checkCapacity and cap=='':
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Cannot monitor capacity without zpool output: CAP. Outputs are" + zpoolMetaStr
exit(stateNum)
if checkFragmentation and frag=='':
stateNum = RaiseStateNum(3, stateNum)
print nagiosStatus[stateNum] + ": Cannot monitor fragmentation without zpool output: FRAG. Outputs are " + zpoolMetaStr
exit(stateNum)
###################################################################################
##
# OK, finally in the actual status checking of the zpool
# Let's build up our perfdata, regardless of what we're checking
fragPercent=''
if frag!='':
fragPercent=frag.replace("%", "")
fragPerfStr="frag="+str(fragPercent)+"%;"
if checkFragmentation:
fragPerfStr=fragPerfStr+str(fragWarnThreshold)+";"+str(fragCritThreshold)+";"
else:
fragPerfStr+=(";;");
perfdata+=(fragPerfStr)
perfdata+=" "
capPercent=''
if cap!='':
capPercent=cap.replace("%", "")
capPerfStr="cap="+str(capPercent)+"%;"
if checkCapacity:
capPerfStr=capPerfStr+str(capWarnThreshold)+";"+str(capCritThreshold)+";"
else:
capPerfStr+=(";;");
perfdata+=(capPerfStr)
perfdata+=" "
# Sizes can be in K, M, G, or T (maybe P, but I'm not doing this yet)
if size!='':
sizeGB = ConvertToGB(size)
perfdata+="size="+str(sizeGB)+"GB;;;"
perfdata+=" "
if alloc!='':
allocGB = ConvertToGB(alloc)
perfdata+="alloc="+str(allocGB)+"GB;;;"
perfdata+=" "
if free!='':
freeGB = ConvertToGB(free)
perfdata+="free="+str(freeGB)+"GB;;;"
perfdata+=" "
##
# Do mandatory checks
healthNum=-1
if health=='ONLINE':
healthNum=0
elif health=='OFFLINE':
stateNum = RaiseStateNum(1, stateNum)
healthNum=1
elif health=='REMOVED':
stateNum = RaiseStateNum(1, stateNum)
healthNum=2
elif health=='UNAVAIL':
stateNum = RaiseStateNum(1, stateNum)
healthNum=3
elif health=='DEGRADED':
stateNum = RaiseStateNum(2, stateNum)
healthNum=4
elif health=='FAULTED':
stateNum = RaiseStateNum(2, stateNum)
healthNum=5
perfdata+="health="+str(healthNum)+";1;3;"
perfdata+=" "
##
# Initial part of msg
msg="POOL: "+str(name)
healthMsgFilled=False
if healthNum > 0:
msg+=", STATUS: "+str(health)
healthMsgFilled=True
##
# Do optional checks
fragMsgFilled=False
capMsgFilled=False
if checkFragmentation and fragPercent!='':
if int(fragPercent) > int(fragCritThreshold):
fragMsgFilled=True
stateNum = RaiseStateNum(2, stateNum)
msg+=", FRAG CRIT: "+str(frag)
elif int(fragPercent) > int(fragWarnThreshold):
fragMsgFilled=True
stateNum = RaiseStateNum(1, stateNum)
msg+=", FRAG WARN: "+str(frag)
if checkCapacity and capPercent!='':
if int(capPercent) > int(capCritThreshold):
capMsgFilled=True
stateNum = RaiseStateNum(2, stateNum)
msg+=", CAP CRIT: "+str(cap)
elif int(capPercent) > int(capWarnThreshold):
capMsgFilled=True
stateNum = RaiseStateNum(1, stateNum)
msg+=", CAP WARN: "+str(cap)
##
# Build up rest of message
if not healthMsgFilled:
msg+=", STATUS: "+str(health)
if size!='':
msg+=", SIZE: "+str(size)
if alloc!='':
msg+=", ALLOC: "+str(alloc)
if free!='':
msg+=", FREE: "+str(free)
if frag!='' and not fragMsgFilled:
msg+=", FRAG: "+str(frag)
if cap!='' and not capMsgFilled:
msg+=", CAP: "+str(cap)
if dedup!='' and not capMsgFilled:
msg+=", DEDUP: "+str(dedup)
##
# Print our output and return
print nagiosStatus[stateNum]+": "+msg+" | "+perfdata
exit(stateNum)