-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_memory
61 lines (52 loc) · 2.16 KB
/
check_memory
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
#!/usr/bin/python
import os
import re
import sys
from optparse import OptionParser
# mem.used as mem.total - mem.free
UNKNOWN = 3
OK = 0
WARNING = 1 #optional
CRITICAL = 2
PROC = '/proc/meminfo'
def CalulateIT():
with open(PROC) as f:
for line in f.readlines():
MemTotal = re.search(r'^MemTotal:\s+(\d+)', line)
MemFree = re.search(r'^MemFree:\s+(\d+)', line)
if MemTotal:
mem_total_kB = int(MemTotal.groups()[0])
if MemFree:
mem_free_kB = int(MemFree.groups()[0])
mem_used_kB = mem_total_kB - mem_free_kB
return 100 * mem_used_kB/mem_total_kB
def main():
parser = OptionParser()
parser.add_option('-c', '--critical', dest='critical', help='critical threshold in percent', type=int)
parser.add_option('-w', '--warning', dest='warning', help='warning threshold in percent', type=int)
(options, args) = parser.parse_args()
if not options.critical is None:
if not os.path.exists(PROC) and not os.access(PROC, os.R_OK):
print "This check can only be ran on linux"
sys.exit(UNKNOWN)
percent_used_kB = CalulateIT()
if percent_used_kB >= options.critical:
print "CRICICAL: Used Memory %s has reach critical threshold of %s" %(percent_used_kB,options.critical)
sys.exit(CRITICAL)
if not options.warning is None:
if percent_used_kB >= options.warning:
print "WARNING: Used Memory %s has reach warning threshold of %s" %(percent_used_kB,options.warning)
sys.exit(WARNING)
if percent_used_kB <= options.critical:
if options.warning:
print "OK: Used Memory %s is below warning threshold %s" %(percent_used_kB,options.warning)
else:
print "OK: Used Memory %s is below critical threshold %s" %(percent_used_kB,options.critical)
sys.exit(OK)
else:
print "Please add the percent threshold, example: -c 80"
print "The Warning (-w) is optional"
print "Ex: %s -w 80 -c 90" %os.path.basename(__file__)
sys.exit(UNKNOWN)
if __name__ == '__main__':
main()