python-esmonitor/pymonitor/monitors/meminfo.py

72 lines
2.6 KiB
Python
Raw Normal View History

2018-10-04 18:50:34 -07:00
from pymonitor import Metric
2015-12-05 02:41:14 -08:00
import re
memline_pattern = re.compile(r'^(?P<key>[^\\:]+)\:\s+(?P<value>[0-9]+)(\s(?P<unit>[a-zA-Z]+))?')
computed_fields = {
2018-02-26 21:43:00 -08:00
"mempctused": lambda items: round((items["memtotal"] - items["memfree"]) / items["memtotal"], 5),
"mempctfree": lambda items: 1 - round((items["memtotal"] - items["memfree"]) / items["memtotal"], 5),
2017-05-15 17:29:20 -07:00
"mempctused_nocache": lambda items: round((items["memtotal"] - items["memfree"] - items["cached"]) /
2018-02-26 21:43:00 -08:00
items["memtotal"], 5),
2017-05-15 17:29:20 -07:00
"mempctfree_nocache": lambda items: 1 - round((items["memtotal"] - items["memfree"] - items["cached"]) /
2018-02-26 21:43:00 -08:00
items["memtotal"], 5),
2017-05-15 17:29:20 -07:00
"swappctused": lambda items: round((items["swaptotal"] - items["swapfree"]) /
2018-10-04 18:50:34 -07:00
items["swaptotal"] if items["swaptotal"] > 0 else 0.0, 5),
2017-05-15 17:29:20 -07:00
"swappctfree": lambda items: 1 - round((items["swaptotal"] - items["swapfree"]) /
2018-10-04 18:50:34 -07:00
items["swaptotal"] if items["swaptotal"] > 0 else 0.0, 5)
2015-12-05 02:41:14 -08:00
}
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
def meminfo(whitelist=[]):
if not whitelist:
2017-05-15 17:29:20 -07:00
whitelist = ["swaptotal", "swapfree", "swapcached",
"memtotal", "memfree", "cached",
2015-12-05 02:41:14 -08:00
"active", "inactive", ]
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
result = {}
with open("/proc/meminfo", "r") as f:
for line in f.read().strip().split("\n"):
matches = memline_pattern.match(line)
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
value = int(matches.group("value"))
unit = matches.group("unit")
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
if unit:
if unit == "kB":
2017-05-15 17:29:20 -07:00
value *= 1024
2015-12-05 02:41:14 -08:00
else:
raise Exception("Unknown unit")
2017-05-15 17:29:20 -07:00
name = ''.join(c for c in matches.group("key").lower() if 96 < ord(c) < 123)
2015-12-05 02:41:14 -08:00
if name in whitelist:
result[name] = value
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
for key in computed_fields:
result[key] = computed_fields[key](result)
2017-05-15 17:29:20 -07:00
2018-10-04 18:50:34 -07:00
yield Metric(result)
2015-12-05 02:41:14 -08:00
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
mapping = {
2018-10-04 18:50:34 -07:00
"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"},
"mempctused_nocache": {"type": "double"},
"mempctfree_nocache": {"type": "double"},
"swappctused": {"type": "double"},
"swappctfree": {"type": "double"}
2015-12-05 02:41:14 -08:00
}
2017-05-15 17:29:20 -07:00
2015-12-05 02:41:14 -08:00
if __name__ == '__main__':
for item in meminfo():
2016-01-28 22:18:56 -08:00
print(item)