From 3d80b84d8932d6f5beb8c1095315def5e12ced62 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Fri, 30 Aug 2019 14:20:43 +1000 Subject: [PATCH] add parsers for memory info and system info --- logparse/parsers/mem.py | 50 ++++++++++++++++++++++++++++++++++ logparse/parsers/sysinfo.py | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 logparse/parsers/mem.py create mode 100644 logparse/parsers/sysinfo.py diff --git a/logparse/parsers/mem.py b/logparse/parsers/mem.py new file mode 100644 index 0000000..b6f8f8d --- /dev/null +++ b/logparse/parsers/mem.py @@ -0,0 +1,50 @@ +# +# mem.py +# +# Get instantaneous memory statistics (installed, total, free, available) +# + +import re + +from ..formatting import * +from .. import config + +import logging +logger = logging.getLogger(__name__) + +def parse_log(): + + logger.debug("Starting memory section") + section = Section("memory") + + table = Table() + + ram_b = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') + table.add_row(Row([Column("Installed"), Column(parsesize(ram_b))])) + + raw_mem = util.readlog(config.prefs['logs']['meminfo']) + total_regex = re.compile("(MemTotal:\s*| kB)+") + free_regex = re.compile("MemFree:\s*") + + for line in raw_mem.splitlines(): + matches = re.findall("^Mem(\w+):\s*(\d*)\s*kB$", line) + if len(matches) > 0: + logger.debug("Detected {0} memory of {1} kB".format(matches[0][0].lower(), matches[0][1])) + table.add_row(Row([Column(matches[0][0]), Column(parsesize(float(matches[0][1])*1000))])) +# if "Mem" in line: +# total = line_regex.sub("", line, 1) +# processor = proc_regex.sub("", processor) +# if not processor in proc_data.items: +# proc_data.items.append(processor) +# else: +# logger.debug("Found duplicate entry (perhaps multiple cores?) for {0}".format(processor)) +# if len(proc_data.items) > 0: +# section.append_data(proc_data) +# else: +# logger.warning("Failed to find processor data") + + table.align_column(0, "right") + section.append_table(table) + + logger.info("Finished memory section") + return section diff --git a/logparse/parsers/sysinfo.py b/logparse/parsers/sysinfo.py new file mode 100644 index 0000000..01a37c2 --- /dev/null +++ b/logparse/parsers/sysinfo.py @@ -0,0 +1,54 @@ +# +# sysinfo.py +# +# Get standard system information from basic Unix commands +# + +import platform +import subprocess +import os +import re + +from ..formatting import * +from .. import config + +import logging +logger = logging.getLogger(__name__) + +def parse_log(): + + logger.debug("Starting sysinfo section") + section = Section("system") + table = Table() + + table.add_row(Row([Column("Hostname"), Column(util.hostname(config.prefs['hostname-path']))])) + table.add_row(Row([Column("OS"), Column(platform.platform())])) + table.add_row(Row([Column("OS version"), Column(platform.version())])) + table.add_row(Row([Column("Platform"), Column(platform.system() + " " + platform.machine())])) + + processors = [] + raw_proc = util.readlog(config.prefs['logs']['cpuinfo']) + line_regex = re.compile(".*model name.*:\s*") + proc_regex = re.compile("\s*(\(R\)|\(TM\)|CPU)") + for line in raw_proc.splitlines(): + if "model name" in line: + processor = line_regex.sub("", line, 1) + processor = " ".join(proc_regex.sub("", processor).split()) # remove extraneous text and whitespace + if not processor in processors: + processors.append(processor) + else: + logger.debug("Found duplicate entry (perhaps multiple cores?) for {0}".format(processor)) + table.align_column(0, "right") + if len(processors) == 1: + table.add_row(Row([Column("Processor"), Column("; ".join(processors))])) + section.append_table(table) + elif len(processors) > 1: + section.append_table(table) + proc_data = Data("Processors") + proc_data.items = processors + section.append_data(proc_data) + else: + logger.warning("Failed to find processor data") + + logger.info("Finished sysinfo section") + return section -- 2.47.1