forked from gluster-rhsc/nagios-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-disk-gluster
executable file
·106 lines (92 loc) · 3.31 KB
/
check-disk-gluster
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
#!/usr/bin/python
import re,sys,commands
from optparse import OptionParser
def getUsageAndFree(command):
status = commands.getstatusoutput(command)[1].split()
path = status[-1]
usagePer = status[-2]
usedSpace = status[-3]
availSpace = status[-4]
device = status[-6]
matchobj = dfPattern.match(usagePer)
if (matchobj):
usage = eval(matchobj.group(0))
return float(usage), float(100 - usage), usedSpace, availSpace, device, path
else:
print "STATE UNKNOWN"
sys.exit(3)
def getDisk(path):
return getUsageAndFree("df -kh %s" % path)
def getInode(path):
usagePer, availablePer, used, avail, dev, path = getUsageAndFree("df -i %s" % path)
return usagePer, availablePer, dev, path
def appendStatus(lst, level, typ, device, mpath, usage):
if 2 == level:
level = "crit"
elif 1 == level:
level = "warn"
else:
level = "ok"
lst.append("%s:%s:%s;%s;%s" % (level, device, mpath, usage))
parser = OptionParser()
parser.add_option('-w', '--warning', action='store', type='int',
dest='warn', help='Warning count in %', default=20)
parser.add_option('-c', '--critical', action='store', type='int',
dest='crit', help='Critical count in %', default=10)
parser.add_option('-p', '--path', action='append', type='string',
dest='mountPath', help='Mount path')
(options, args) = parser.parse_args()
if not options.mountPath:
options.mountPath = []
f = open("/etc/mtab")
for i in f.readlines():
if i.startswith("/"):
options.mountPath.append(i.split()[0])
f.close()
if not options.mountPath:
parser.print_help()
sys.exit(1)
#build regex
dfPattern = re.compile('[0-9]+')
crit = 100 - options.crit
warn = 100 - options.warn
#print "Warning:", options.warn, "Critical:", options.crit, "Path:", options.mountPath[0]
disk = []
warnList = []
critList = []
diskList = []
level = -1
for path in options.mountPath:
diskUsage, diskFree, used, avail, dev, mpath = getDisk(path)
inodeUsage, inodeFree, idev, ipath = getInode(path)
disk.append("%s=%.2f;%s;%s;0;100 %s=%.2f;%s;%s;0;100" % (path, diskUsage, warn, crit, path, inodeUsage, warn, crit))
#status.append(" %s %s(Used %s)" % (path, used, avail))
if diskUsage >= crit or inodeUsage >= crit:
if diskUsage >= crit:
critList.append("crit:disk:%s;%s;%s" % (dev, mpath, diskUsage))
else:
critList.append("crit:inode:%s;%s;%s" % (idev, ipath, inodeUsage))
if not level > 1:
level = 2
elif (diskUsage >= warn and diskUsage < crit) or (inodeUsage >= warn and inodeUsage < crit):
if diskUsage >= warn:
warnList.append("warn:disk:%s;%s;%s" % (dev, mpath, diskUsage))
else:
warnList.append("warn:inode:%s;%s;%s" % (idev, ipath, inodeUsage))
if not level > 0:
level = 1
else:
diskList.append("%s:%s" % (dev, mpath))
# okList.append("ok:disk:%s;%s;%s" % (dev, mpath, diskUsage))
msg = " ".join(critList + warnList)
if not msg:
msg += " disks:mounts:(" + ",".join(diskList) + ")"
if 2 == level:
print "CRITICAL : %s | %s" % (msg, " ".join(disk))
sys.exit(2)
elif 1 == level:
print "WARNING : %s | %s" % (msg, " ".join(disk))
sys.exit(1)
else:
print "OK : %s | %s" % (msg, " ".join(disk))
sys.exit(0)