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: |
(...skipping 24 matching lines...) Expand all Loading... |
35 else: | 35 else: |
36 value = int(value) | 36 value = int(value) |
37 meminfo[label] = value | 37 meminfo[label] = value |
38 | 38 |
39 memtotal = meminfo.get("memtotal", 0) | 39 memtotal = meminfo.get("memtotal", 0) |
40 memfree = meminfo.get("memfree", 0) + meminfo.get("buffers", 0) + meminfo.get(
"cached", 0) | 40 memfree = meminfo.get("memfree", 0) + meminfo.get("buffers", 0) + meminfo.get(
"cached", 0) |
41 swaptotal = meminfo.get("swaptotal", 0) | 41 swaptotal = meminfo.get("swaptotal", 0) |
42 swapfree = meminfo.get("swapfree", 0) + meminfo.get("swapcached", 0) | 42 swapfree = meminfo.get("swapfree", 0) + meminfo.get("swapcached", 0) |
43 | 43 |
44 mempercentage = round(float(memfree) / memtotal * 100) | 44 mempercentage = round(float(memfree) / memtotal * 100) |
45 swappercentage = round(float(swapfree) / swaptotal * 100) | 45 swappercentage = round(float(swapfree) / swaptotal * 100) if swaptotal else 0 |
46 | 46 |
47 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( | 47 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( |
48 mempercentage, format_memory(memfree), format_memory(memtotal), | 48 mempercentage, format_memory(memfree), format_memory(memtotal), |
49 swappercentage, format_memory(swapfree), format_memory(swaptotal) | 49 swappercentage, format_memory(swapfree), format_memory(swaptotal) |
50 ) | 50 ) |
51 | 51 |
52 perfdata = "memory=%i;%i;%i swap=%i;%i;%i" % ( | 52 perfdata = "memory=%i;%i;%i swap=%i;%i;%i" % ( |
53 mempercentage, args.memwarn, args.memcrit, | 53 mempercentage, args.memwarn, args.memcrit, |
54 swappercentage, args.swapwarn, args.swapcrit | 54 swappercentage, args.swapwarn, args.swapcrit |
55 ) | 55 ) |
56 | 56 |
57 output = "%s|%s" % (status, perfdata) | 57 output = "%s|%s" % (status, perfdata) |
58 | 58 |
59 if mempercentage <= args.memcrit or swappercentage <= args.swapcrit: | 59 if mempercentage <= args.memcrit or swappercentage <= args.swapcrit: |
60 print "CRITICAL - " + output | 60 print "CRITICAL - " + output |
61 elif mempercentage <= args.memwarn or swappercentage <= args.swapwarn: | 61 elif mempercentage <= args.memwarn or swappercentage <= args.swapwarn: |
62 print "WARNING - " + output | 62 print "WARNING - " + output |
63 else: | 63 else: |
64 print "OK - " + output | 64 print "OK - " + output |
OLD | NEW |