-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_swap
98 lines (81 loc) · 3.4 KB
/
check_swap
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
import datetime
import dateutil.parser
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def exitnagios(status,message):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
def dofreeswapscall(warning,critical,minimun):
cmdline = ["/usr/bin/free","--mega","--wide"]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8").strip()
errors = completedproc.stderr.decode("utf-8").strip()
exitcode = completedproc.returncode
infomsg(output)
if exitcode == 0:
lines = output.splitlines()
hasswap = False
total = 0
free = 0
if len(lines)>1:
lines.pop(0)
for line in lines:
parts = line.split()
if parts[0]=="Swap:":
infomsg(parts)
hasswap = True
total = total+int(parts[1])
free = free+int(parts[3])
if not hasswap:
exitnagios("CRITICAL","no swap detected")
else:
usage = free/total
if minimun!="" and total<float(minimun):
exitnagios("CRITICAL","swap is too small")
elif critical!="" and usage>float(critical):
exitnagios("CRITICAL","swap is used at "+str(round(usage,2))+"% | percent="+str(usage))
elif warning!="" and usage>float(warning):
exitnagios("WARNING","swap is used at "+str(round(usage,2))+"% | percent="+str(usage))
else:
exitnagios("OK","swap is used at "+str(round(usage,2))+"% | percent="+str(usage))
else:
exitnagios("CRITICAL","issue retrieving free swap")
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--warning", dest="warning", default="", help="warning level %")
parser.add_argument("-c", "--critical", dest="critical", default="", help="critical level %")
parser.add_argument("-m", "--minimun", dest="minimun", default="", help="minimun size MB")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
dofreeswapscall(args.warning,args.critical,args.minimun)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------