Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: modules/nagios/files/check_memory

Issue 11773028: Added connections and memory monitoring (Closed)
Left Patch Set: Created Sept. 20, 2013, 10:25 a.m.
Right Patch Set: Addressed comments Created Nov. 4, 2013, 7:53 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld