add memory module

This commit is contained in:
Dave Pedu 2015-12-05 02:41:14 -08:00
parent 80ebf0bcbf
commit ad4044fbc9
2 changed files with 68 additions and 0 deletions

View File

@ -12,6 +12,11 @@
"type":"load",
"freq":"30",
"args":{}
},
{
"type":"meminfo",
"freq":"30",
"args":{}
}
]
}

View File

@ -0,0 +1,63 @@
import re
memline_pattern = re.compile(r'^(?P<key>[^\\:]+)\:\s+(?P<value>[0-9]+)(\s(?P<unit>[a-zA-Z]+))?')
computed_fields = {
"mempctused": lambda items: round((items["memtotal"]-items["memfree"])/items["memtotal"], 2),
"mempctfree": lambda items: 1-round((items["memtotal"]-items["memfree"])/items["memtotal"], 2),
"swappctused": lambda items: round((items["swaptotal"]-items["swapfree"])/items["swaptotal"], 2),
"swappctfree": lambda items: 1-round((items["swaptotal"]-items["swapfree"])/items["swaptotal"], 2)
}
def meminfo(whitelist=[]):
if not whitelist:
whitelist = ["swaptotal", "swapfree", "swapcached",
"memtotal", "memfree", "memavailable", "cached",
"active", "inactive", ]
result = {}
with open("/proc/meminfo", "r") as f:
for line in f.read().strip().split("\n"):
matches = memline_pattern.match(line)
value = int(matches.group("value"))
unit = matches.group("unit")
if unit:
if unit == "kB":
value*=1024
else:
raise Exception("Unknown unit")
name = ''.join(c for c in matches.group("key").lower() if 96<ord(c)<123)
if name in whitelist:
result[name] = value
for key in computed_fields:
result[key] = computed_fields[key](result)
return result
mapping = {
"load": {
"properties": {
"swaptotal": { "type": "long" },
"swapfree": { "type": "long" },
"swapcached": { "type": "long" },
"memtotal": { "type": "long" },
"memfree": { "type": "long" },
"memavailable": { "type": "long" },
"cached": { "type": "long" },
"active": { "type": "long" },
"inactive": { "type": "long" },
"mempctused": { "type": "double" },
"mempctfree": { "type": "double" },
"swappctused": { "type": "double" },
"swappctfree": { "type": "double" }
}
}
}
if __name__ == '__main__':
for k,v in meminfo().items():
print("%s: %s"%(k,v))