Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import re | 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: |
Felix Dahlke
2013/10/01 14:05:33
Whitespace around *, as below?
| |
7 return "%i MB" % (bytes / (1024 * 1024)) | 7 return "%i MB" % (bytes / (1024 * 1024)) |
Felix Dahlke
2013/10/01 14:05:33
Would actually prefer to use MiB here and KiB belo
Wladimir Palant
2013/11/04 07:54:41
I would have to look up MiB and KiB every time, no
Felix Dahlke
2013/11/20 16:00:51
It's not true that nobody uses it, but it's quite
| |
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") | |
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)") | |
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)") | |
19 args = parser.parse_args() | |
20 | |
14 memtotal = None | 21 memtotal = None |
15 memfree = None | 22 memfree = None |
16 swaptotal = None | 23 swaptotal = None |
17 swapfree = None | 24 swapfree = None |
18 with open("/proc/meminfo", "r") as file: | 25 with open("/proc/meminfo", "r") as file: |
19 for line in file: | 26 for line in file: |
20 label, value = line.split(None, 1) | 27 label, value = line.split(None, 1) |
21 | 28 |
22 label = label.lower().rstrip(":") | 29 label = label.lower().rstrip(":") |
23 value = value.strip() | 30 value = value.strip() |
(...skipping 17 matching lines...) Expand all Loading... | |
41 swapfree = value | 48 swapfree = value |
42 | 49 |
43 mempercentage = round(float(memfree) / memtotal * 100) | 50 mempercentage = round(float(memfree) / memtotal * 100) |
44 swappercentage = round(float(swapfree) / swaptotal * 100) | 51 swappercentage = round(float(swapfree) / swaptotal * 100) |
45 | 52 |
46 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( | 53 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( |
47 mempercentage, format_memory(memfree), format_memory(memtotal), | 54 mempercentage, format_memory(memfree), format_memory(memtotal), |
48 swappercentage, format_memory(swapfree), format_memory(swaptotal) | 55 swappercentage, format_memory(swapfree), format_memory(swaptotal) |
49 ) | 56 ) |
50 | 57 |
51 perfdata = "memory=%i swap=%i" % (mempercentage, swappercentage) | 58 perfdata = "memory=%i;%i;%i swap=%i;%i;%i" % ( |
59 mempercentage, args.memwarn, args.memcrit, | |
60 swappercentage, args.swapwarn, args.swapcrit | |
61 ) | |
52 | 62 |
53 output = "%s|%s" % (status, perfdata) | 63 output = "%s|%s" % (status, perfdata) |
54 | 64 |
55 print "OK - " + output | 65 if mempercentage <= args.memcrit or swappercentage <= args.swapcrit: |
66 print "CRITICAL - " + output | |
67 elif mempercentage <= args.memwarn or swappercentage <= args.swapwarn: | |
68 print "WARNING - " + output | |
69 else: | |
70 print "OK - " + output | |
LEFT | RIGHT |