From b8f1b6c87980772e10c2a55d12dbe9f8bf9c1d1d Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sun, 23 Oct 2016 03:40:44 +0200 Subject: [PATCH] [monit_parser] implement memory alloaction unit parsing Previously monit seems to have output memory usage in kilobytes without a unit. Somewhen this seemed to have changed to an (optional?) suffix (e.g. MB). Thus the new scaling may differ from the previous scaling, which was probably broken for most users. Additionally the units of the resulting values are clarified. --- plugins/monit/monit_parser | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/plugins/monit/monit_parser b/plugins/monit/monit_parser index 3515f977..7306f612 100755 --- a/plugins/monit/monit_parser +++ b/plugins/monit/monit_parser @@ -78,9 +78,21 @@ def parse_processes(): except KeyError: procs[cur_proc] = {} continue - m = re.match(" memory kilobytes total\s+([0-9]+).*$", line) + m = re.match(" memory kilobytes total\s+([0-9.]+)(.*)$", line) if m: - procs[cur_proc]["total_memory"] = m.group(1) + size, suffix = m.groups() + # we store memory consumption as megabytes + factor = { + "": 1024 ** -1, + "KB": 1024 ** -1, + "MB": 1024 ** 0, + "GB": 1024 ** 1, + "TB": 1024 ** 2, + }[suffix.strip().upper()] + try: + procs[cur_proc]["total_memory"] = float(size) * factor + except ValueError: + procs[cur_proc]["total_memory"] = "U" continue m = re.match(" cpu percent total\s+([.0-9]+)%.*$", line) if m: @@ -100,7 +112,7 @@ if action == 'autoconf': elif action == 'config': procs = parse_processes() print('graph_title Per process stats from Monit') - print('graph_vlabel numbers') + print('graph_vlabel usage of memory [MB] or cpu [%]') print('graph_category monit') for process in procs: for stat in procs[process]: