OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import re, argparse | 3 import re, argparse |
4 | 4 |
5 def format_memory(bytes): | 5 def format_memory(bytes): |
6 if bytes >= 1024 * 1024: | 6 if bytes >= 1024 * 1024: |
7 return "%i MB" % (bytes / (1024 * 1024)) | 7 return "%i MB" % (bytes / (1024 * 1024)) |
8 elif bytes >= 1024: | 8 elif bytes >= 1024: |
9 return "%i kB" % (bytes / 1024) | 9 return "%i kB" % (bytes / 1024) |
10 else: | 10 else: |
11 return "%i bytes" | 11 return "%i bytes" |
12 | 12 |
13 if __name__ == "__main__": | 13 if __name__ == "__main__": |
14 parser = argparse.ArgumentParser(description="Determine the amount of availabl
e memory") | 14 parser = argparse.ArgumentParser(description="Determine the amount of availabl
e memory") |
15 parser.add_argument("--physical-warn", dest="memwarn", type=int, default=-1, h
elp="Warning threshold for available physical memory (in percent)") | 15 parser.add_argument("--physical-warn", dest="memwarn", type=int, default=-1, h
elp="Warning threshold for available physical memory (in percent)") |
16 parser.add_argument("--physical-critical", dest="memcrit", type=int, default=-
1, help="Critical threshold for available physical memory (in percent)") | 16 parser.add_argument("--physical-critical", dest="memcrit", type=int, default=-
1, help="Critical threshold for available physical memory (in percent)") |
17 parser.add_argument("--swap-warn", dest="swapwarn", type=int, default=-1, help
="Warning threshold for available swap memory (in percent)") | 17 parser.add_argument("--swap-warn", dest="swapwarn", type=int, default=-1, help
="Warning threshold for available swap memory (in percent)") |
18 parser.add_argument("--swap-critical", dest="swapcrit", type=int, default=-1,
help="Critical threshold for available swap memory (in percent)") | 18 parser.add_argument("--swap-critical", dest="swapcrit", type=int, default=-1,
help="Critical threshold for available swap memory (in percent)") |
19 args = parser.parse_args() | 19 args = parser.parse_args() |
20 | 20 |
21 memtotal = None | 21 meminfo = {} |
22 memfree = None | |
23 swaptotal = None | |
24 swapfree = None | |
25 with open("/proc/meminfo", "r") as file: | 22 with open("/proc/meminfo", "r") as file: |
26 for line in file: | 23 for line in file: |
27 label, value = line.split(None, 1) | 24 label, value = line.split(None, 1) |
28 | 25 |
29 label = label.lower().rstrip(":") | 26 label = label.lower().rstrip(":") |
30 value = value.strip() | 27 value = value.strip() |
31 match = re.match(r"^(\d+)\s+(kb|mb|gb)$", value, re.IGNORECASE) | 28 match = re.match(r"^(\d+)\s+(kb|mb|gb)$", value, re.IGNORECASE) |
32 if match: | 29 if match: |
33 value = int(match.group(1)) * 1024 | 30 value = int(match.group(1)) * 1024 |
34 if match.group(2).lower() != "kb": | 31 if match.group(2).lower() != "kb": |
35 value *= 1024 | 32 value *= 1024 |
36 if match.group(2).lower() != "mb": | 33 if match.group(2).lower() != "mb": |
37 value *= 1024 | 34 value *= 1024 |
38 else: | 35 else: |
39 value = int(value) | 36 value = int(value) |
| 37 meminfo[label] = value |
40 | 38 |
41 if label == "memtotal": | 39 memtotal = meminfo.get("memtotal", 0) |
42 memtotal = value | 40 memfree = meminfo.get("memfree", 0) + meminfo.get("buffers", 0) + meminfo.get(
"cached", 0) |
43 elif label == "memfree": | 41 swaptotal = meminfo.get("swaptotal", 0) |
44 memfree = value | 42 swapfree = meminfo.get("swapfree", 0) + meminfo.get("swapcached", 0) |
45 elif label == "swaptotal": | |
46 swaptotal = value | |
47 elif label == "swapfree": | |
48 swapfree = value | |
49 | 43 |
50 mempercentage = round(float(memfree) / memtotal * 100) | 44 mempercentage = round(float(memfree) / memtotal * 100) |
51 swappercentage = round(float(swapfree) / swaptotal * 100) | 45 swappercentage = round(float(swapfree) / swaptotal * 100) |
52 | 46 |
53 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( | 47 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( |
54 mempercentage, format_memory(memfree), format_memory(memtotal), | 48 mempercentage, format_memory(memfree), format_memory(memtotal), |
55 swappercentage, format_memory(swapfree), format_memory(swaptotal) | 49 swappercentage, format_memory(swapfree), format_memory(swaptotal) |
56 ) | 50 ) |
57 | 51 |
58 perfdata = "memory=%i;%i;%i swap=%i;%i;%i" % ( | 52 perfdata = "memory=%i;%i;%i swap=%i;%i;%i" % ( |
59 mempercentage, args.memwarn, args.memcrit, | 53 mempercentage, args.memwarn, args.memcrit, |
60 swappercentage, args.swapwarn, args.swapcrit | 54 swappercentage, args.swapwarn, args.swapcrit |
61 ) | 55 ) |
62 | 56 |
63 output = "%s|%s" % (status, perfdata) | 57 output = "%s|%s" % (status, perfdata) |
64 | 58 |
65 if mempercentage <= args.memcrit or swappercentage <= args.swapcrit: | 59 if mempercentage <= args.memcrit or swappercentage <= args.swapcrit: |
66 print "CRITICAL - " + output | 60 print "CRITICAL - " + output |
67 elif mempercentage <= args.memwarn or swappercentage <= args.swapwarn: | 61 elif mempercentage <= args.memwarn or swappercentage <= args.swapwarn: |
68 print "WARNING - " + output | 62 print "WARNING - " + output |
69 else: | 63 else: |
70 print "OK - " + output | 64 print "OK - " + output |
OLD | NEW |