Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 import re | |
4 | |
5 def format_memory(bytes): | |
6 if bytes >= 1024*1024: | |
Felix Dahlke
2013/10/01 14:05:33
Whitespace around *, as below?
| |
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: | |
9 return "%i kB" % (bytes / 1024) | |
10 else: | |
11 return "%i bytes" | |
12 | |
13 if __name__ == "__main__": | |
14 memtotal = None | |
15 memfree = None | |
16 swaptotal = None | |
17 swapfree = None | |
18 with open("/proc/meminfo", "r") as file: | |
19 for line in file: | |
20 label, value = line.split(None, 1) | |
21 | |
22 label = label.lower().rstrip(":") | |
23 value = value.strip() | |
24 match = re.match(r"^(\d+)\s+(kb|mb|gb)$", value, re.IGNORECASE) | |
25 if match: | |
26 value = int(match.group(1)) * 1024 | |
27 if match.group(2).lower() != "kb": | |
28 value *= 1024 | |
29 if match.group(2).lower() != "mb": | |
30 value *= 1024 | |
31 else: | |
32 value = int(value) | |
33 | |
34 if label == "memtotal": | |
35 memtotal = value | |
36 elif label == "memfree": | |
37 memfree = value | |
38 elif label == "swaptotal": | |
39 swaptotal = value | |
40 elif label == "swapfree": | |
41 swapfree = value | |
42 | |
43 mempercentage = round(float(memfree) / memtotal * 100) | |
44 swappercentage = round(float(swapfree) / swaptotal * 100) | |
45 | |
46 status = "memory %i%% (%s/%s) swap %i%% (%s/%s)" % ( | |
47 mempercentage, format_memory(memfree), format_memory(memtotal), | |
48 swappercentage, format_memory(swapfree), format_memory(swaptotal) | |
49 ) | |
50 | |
51 perfdata = "memory=%i swap=%i" % (mempercentage, swappercentage) | |
52 | |
53 output = "%s|%s" % (status, perfdata) | |
54 | |
55 print "OK - " + output | |
OLD | NEW |