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

Unified Diff: modules/nagios/files/check_memory

Issue 11773028: Added connections and memory monitoring (Closed)
Patch Set: Created Sept. 20, 2013, 10:25 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « modules/nagios/files/check_connections ('k') | modules/nagios/manifests/client.pp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: modules/nagios/files/check_memory
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/nagios/files/check_memory
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+import re
+
+def format_memory(bytes):
+ if bytes >= 1024*1024:
Felix Dahlke 2013/10/01 14:05:33 Whitespace around *, as below?
+ 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
+ elif bytes >= 1024:
+ return "%i kB" % (bytes / 1024)
+ else:
+ return "%i bytes"
+
+if __name__ == "__main__":
+ memtotal = None
+ memfree = None
+ swaptotal = None
+ swapfree = None
+ with open("/proc/meminfo", "r") as file:
+ for line in file:
+ label, value = line.split(None, 1)
+
+ label = label.lower().rstrip(":")
+ value = value.strip()
+ match = re.match(r"^(\d+)\s+(kb|mb|gb)$", value, re.IGNORECASE)
+ if match:
+ value = int(match.group(1)) * 1024
+ if match.group(2).lower() != "kb":
+ value *= 1024
+ if match.group(2).lower() != "mb":
+ value *= 1024
+ else:
+ value = int(value)
+
+ if label == "memtotal":
+ memtotal = value
+ elif label == "memfree":
+ memfree = value
+ elif label == "swaptotal":
+ swaptotal = value
+ elif label == "swapfree":
+ swapfree = value
+
+ mempercentage = round(float(memfree) / memtotal * 100)
+ swappercentage = round(float(swapfree) / swaptotal * 100)
+
+ status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % (
+ mempercentage, format_memory(memfree), format_memory(memtotal),
+ swappercentage, format_memory(swapfree), format_memory(swaptotal)
+ )
+
+ perfdata = "memory=%i swap=%i" % (mempercentage, swappercentage)
+
+ output = "%s|%s" % (status, perfdata)
+
+ print "OK - " + output
« no previous file with comments | « modules/nagios/files/check_connections ('k') | modules/nagios/manifests/client.pp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld