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
|
#!/usr/bin/python
# Copyright (C) 2014 Red Hat Inc
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
import os
import re
import sys
import commands
from optparse import OptionParser
from glusternagios import utils
WARNING_LEVEL = 80
CRITICAL_LEVEL = 90
INVALID_STATUS_CODE = -1
def getVal(val):
dmatch = re.compile('[0-9]+').match(val)
if (dmatch):
return float(eval(dmatch.group(0)))
else:
return 0
def getLVdetails(filename, lvs):
dev_name = os.path.realpath(filename)
pool = ""
disk = {}
for a in lvs:
dev = os.path.realpath(a['LVM2_LV_PATH'])
if dev_name == dev:
if a['LVM2_LV_ATTR'][0] == 'V':
pool = a['LVM2_POOL_LV']
disk['thinLv'] = True
break
else:
disk['thinLv'] = False
return disk
else:
return None
for b in lvs:
if b['LVM2_LV_NAME'] == pool:
disk['actualUsedPercent'] = float(b['LVM2_DATA_PERCENT'])
disk['actualTotalSize'] = float(b['LVM2_LV_SIZE'])
disk['actualUsed'] = disk['actualTotalSize'] * disk[
'actualUsedPercent'] / 100
return disk
else:
return None
def getUsageAndFree(command, path, crit, warn, lvm):
disk = {'path': None, 'usePcent': 0, 'avail': 0,
'used': 0, 'size': 0, 'fs': None,
'status': None, 'msg': None, 'availPcent': 0,
'statusCode': utils.PluginStatusCode.UNKNOWN}
# Check if device exists and permissions are ok
if not os.access(path, os.F_OK):
disk['status'] = "Device not found!"
disk['msg'] = 'no device'
disk['fs'] = path
disk['statusCode'] = utils.PluginStatusCode.CRITICAL
return disk
if not os.access(path, os.R_OK):
disk['status'] = "Unable to access the device"
disk['msg'] = 'no access'
disk['fs'] = path
disk['statusCode'] = utils.PluginStatusCode.CRITICAL
return disk
status = commands.getstatusoutput(command)
# Sample output
# (0, 'Filesystem 1G-blocks Used Available Use% Mounted on\n/dev/sda1
# 290G 196G 79G 72% /')
if status[0] != 0:
disk['msg'] = 'error:%s' % status[0]
if status[0] == 256:
disk['status'] = "Brick/Device path not found!"
else:
disk['status'] = status[1]
disk['statusCode'] = utils.PluginStatusCode.CRITICAL
return disk
status = status[1].split()
disk['path'] = status[-1]
disk['avail'] = getVal(status[-3])
disk['used'] = getVal(status[-4])
disk['size'] = getVal(status[-5])
disk['fs'] = status[-6]
disk['usePcent'] = getVal(status[-2])
if disk['usePcent'] >= crit:
disk['statusCode'] = utils.PluginStatusCode.CRITICAL
elif disk['usePcent'] >= warn:
disk['statusCode'] = utils.PluginStatusCode.WARNING
elif disk['usePcent'] < warn:
disk['statusCode'] = utils.PluginStatusCode.OK
disk['availPcent'] = 100 - disk['usePcent']
return disk
def getDisk(path, crit, warn, usage=None, lvm=False):
if usage:
return getUsageAndFree("df -B%s %s" % (usage, path),
path, crit, warn, lvm)
else:
return getUsageAndFree("df -BG %s" % path,
path, crit, warn, lvm)
def getInode(path, crit, warn, lvm=False):
return getUsageAndFree("df -i %s" % path,
path, crit, warn, lvm)
def getMounts(searchQuery, excludeList=[]):
mountPaths = []
f = open("/etc/mtab")
for i in f.readlines():
if i.startswith(searchQuery):
if not excludeList:
mountPaths.append(i.split()[0])
else:
device = i.split()
if not device[0] in options.exclude and\
not device[1] in options.exclude:
mountPaths.append(device[0])
f.close()
return mountPaths
def parse_input():
parser = OptionParser()
parser.add_option('-w', '--warning', action='store', type='int',
dest='warn',
help='Warning count in %', default=WARNING_LEVEL)
parser.add_option('-c', '--critical', action='store', type='int',
dest='crit',
help='Critical count in %', default=CRITICAL_LEVEL)
parser.add_option('-u', '--usage', action="store", dest='usage',
help='Usage in %/GB/MB/KB', default=None)
parser.add_option('-l', '--lvm', action="store_true",
dest='lvm',
help='List lvm mounts', default=False)
parser.add_option('-d', '--inode', action="store_true", dest='inode',
help='List inode usage along with disk/brick usage',
default=False)
parser.add_option('-a', '--all', action="store_true",
dest='all',
help='List all mounts', default=False)
parser.add_option('-n', '--ignore', action="store_true",
dest='ignore',
help='Ignore errors', default=False)
parser.add_option('-i', '--include', action='append', type='string',
dest='mountPath',
help='Mount path', default=[])
parser.add_option('-x', '--exclude', action="append", type='string',
dest='exclude',
help='Exclude disk/brick')
parser.add_option('-s', action="store_true", default=False,
dest='showErrorDisk',
help='Show critical or warning disks in the status')
parser.add_option('-t', '--thinPool', action="store_true",
dest='thinPool',
help='Lists detail of underlying thin pool',
default=False)
return parser.parse_args()
def _getMsg(okList, warnList, critList):
msg = ", ".join(critList)
errorDiskMsg = msg
if critList and (warnList or okList):
msg = "CRITICAL: " + msg
if warnList:
if msg:
msg += "; WARNING: "
msg += ", ".join(warnList)
if errorDiskMsg:
errorDiskMsg += "; WARNING: " + ", ".join(warnList)
else:
errorDiskMsg = msg
if okList:
if msg:
msg += "; OK: "
msg += ", ".join(okList)
return msg, errorDiskMsg
def _getUnitAndType(val):
unit = utils.convertSize(val, "GB", "TB")
if unit >= 1:
return unit, "TB"
else:
return val, "GB"
def showDiskUsage(warn, crit, mountPaths, toListInode, usage=False,
isLvm=False, ignoreError=False, showErrorDisk=True,
thinPool=False):
diskPerf = []
warnList = []
critList = []
okList = []
mounts = []
statusCode = INVALID_STATUS_CODE
totalUsed = 0
totalSize = 0
noOfMounts = len(mountPaths)
maxPercentUsed = 0
if thinPool:
rc, out, err = utils.execCmd(["lvm"] +
("vgs --unquoted --noheading " +
"--nameprefixes --separator : " +
"--nosuffix --units g -o " +
"lv_path,data_percent,pool_lv,lv_attr,"
"lv_size,lv_name").split())
if rc == 0:
res = []
for i in out:
tem = i.split(":")
dct = {}
for j in tem:
tp = j.split("=")
dct[tp[0].replace(" ", "")] = tp[1]
res.append(dct)
else:
thinPool = False
for path in mountPaths:
disk = getDisk(path, crit, warn, usage, isLvm)
inode = getInode(path, crit, warn, isLvm)
if thinPool:
thinLv = getLVdetails(disk['fs'], res)
if thinLv and thinLv['thinLv']:
if disk['usePcent'] >= crit or thinLv[
'actualUsedPercent'] >= crit:
disk['statusCode'] = utils.PluginStatusCode.CRITICAL
elif disk['usePcent'] >= warn or thinLv[
'actualUsedPercent'] >= warn:
disk['statusCode'] = utils.PluginStatusCode.WARNING
elif disk['usePcent'] < warn or thinLv[
'actualUsedPercent'] < warn:
disk['statusCode'] = utils.PluginStatusCode.OK
else:
thinPool = False
if disk['path'] in mounts:
continue
if not disk['used'] or not inode['used']:
if not ignoreError:
sys.exit(utils.PluginStatusCode.UNKNOWN)
if disk['path']:
mounts.append(disk['path'])
data = ""
if usage and disk['path']:
data = "%s=%.1f%s;%.1f;%.1f;0;%.1f" % (
disk['path'],
disk['used'],
usage,
warn * disk['size'] / 100,
crit * disk['size'] / 100,
disk['size'])
if toListInode:
data += " %s=%.1f;%.1f;%.1f;0;%.1f" % (
inode['path'],
inode['used'],
warn * inode['used'] / 100,
crit * inode['used'] / 100,
inode['size'])
elif disk['path']:
if thinPool and thinLv['thinLv']:
data = "%s=%.2f%%;%s;%s;0;%s" % (
disk['path'],
disk['usePcent'],
warn,
crit,
disk['size'])
data += " Thin-pool=%.2f%%;%s;%s;0;%.1f" % (
thinLv['actualUsedPercent'],
warn,
crit,
thinLv['actualTotalSize'])
else:
data = "%s=%.2f%%;%s;%s;0;%s" % (
disk['path'],
disk['usePcent'],
warn,
crit,
disk['size'])
if toListInode:
data += " %s=%.2f%%;%s;%s;0;%s" % (
inode['path'],
inode['usePcent'],
warn,
crit,
inode['size'])
diskPerf.append(data)
totalUsed += disk['used']
totalSize += disk['size']
if disk['usePcent'] > maxPercentUsed:
maxPercentUsed = disk['usePcent']
# adding into status message if there is any
# specfic status found (short msg for list of disks)
msg = ""
if disk['status'] and disk['msg']:
if noOfMounts == 1:
msg = "%s=%s(%s)" % (disk['fs'], disk['path'],
disk['status'])
else:
msg = "%s(%s)" % (disk['fs'], disk['msg'])
else:
if noOfMounts == 1:
msg = "%s=%s" % (disk['fs'], disk['path'])
else:
msg = "%s" % (disk['path'])
if disk['statusCode'] == utils.PluginStatusCode.CRITICAL or \
inode['statusCode'] == utils.PluginStatusCode.CRITICAL:
statusCode = utils.PluginStatusCode.CRITICAL
critList.append(msg)
elif (disk['statusCode'] == utils.PluginStatusCode.WARNING or
inode['statusCode'] == utils.PluginStatusCode.WARNING):
# if any previous disk statusCode is not critical
# we should not change the statusCode into warning
if statusCode != utils.PluginStatusCode.CRITICAL:
statusCode = utils.PluginStatusCode.WARNING
# just adding warning values into the list
warnList.append(msg)
elif disk['statusCode'] == utils.PluginStatusCode.OK:
if statusCode == INVALID_STATUS_CODE or \
statusCode == utils.PluginStatusCode.OK:
statusCode = utils.PluginStatusCode.OK
okList.append(msg)
else:
# added \ to fix E125 pep8 error
if statusCode != utils.PluginStatusCode.CRITICAL or \
statusCode != utils.PluginStatusCode.WARNING:
statusCode = utils.PluginStatusCode.UNKNOWN
okList.append(msg)
msg, errorDiskMsg = _getMsg(okList, warnList, critList)
if totalUsed == 0 and totalSize == 0:
# avoid zero div error
return statusCode, "mount: %s" % msg, diskPerf
if totalUsed == 0:
# avoid zero div error
totUsagePercent = 0
elif len(mounts) > 1:
totUsagePercent = totalUsed / totalSize * 100
else:
totUsagePercent = maxPercentUsed
usageMsg = ""
if not usage:
totUsedSz, totUsedSzUnit = _getUnitAndType(totalUsed)
totSpaceSz, totSpaceSzUnit = _getUnitAndType(totalSize)
usageMsg = "%.1f%% used (%s%s out of %s%s)" % (totUsagePercent,
totUsedSz,
totUsedSzUnit,
totSpaceSz,
totSpaceSzUnit)
if thinPool and thinLv['thinLv']:
usageMsg += " [Thin-pool: %.1f%% (%.1fG out of %.1fG)]" % (
thinLv['actualUsedPercent'],
thinLv['actualUsed'],
thinLv['actualTotalSize'])
else:
usageMsg = "%.1f%% used (%s%s out of %s%s)" % (totUsagePercent,
totalUsed,
usage,
totalSize,
usage)
if showErrorDisk:
msg = "%s\n:mount(s): (%s)" % (errorDiskMsg, msg)
else:
msg = "%s\n:mount(s): (%s)" % (usageMsg, msg)
return statusCode, msg, diskPerf
if __name__ == '__main__':
(options, args) = parse_input()
if options.lvm:
searchQuery = "/dev/mapper"
else:
searchQuery = "/"
if not options.mountPath or options.lvm or options.all:
options.mountPath += getMounts(searchQuery, options.exclude)
statusCode, msg, diskPerf = showDiskUsage(options.warn,
options.crit,
options.mountPath,
options.inode,
options.usage,
options.lvm,
options.ignore,
options.showErrorDisk,
options.thinPool)
if utils.PluginStatusCode.CRITICAL == statusCode:
sys.stdout.write("%s : %s | %s\n" % (
utils.PluginStatus.CRITICAL,
msg,
" ".join(diskPerf)))
sys.exit(utils.PluginStatusCode.CRITICAL)
elif utils.PluginStatusCode.WARNING == statusCode:
sys.stdout.write("%s : %s | %s\n" % (
utils.PluginStatus.WARNING,
msg,
" ".join(diskPerf)))
sys.exit(utils.PluginStatusCode.WARNING)
else:
if options.showErrorDisk:
sys.stdout.write("%s %s | %s\n" % (
utils.PluginStatus.OK, msg, " ".join(diskPerf)))
else:
sys.stdout.write("%s : %s | %s\n" % (
utils.PluginStatus.OK, msg, " ".join(diskPerf)))
|