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 |