-#
-# mem.py
-#
-# Get instantaneous memory statistics (installed, total, free, available)
-#
+"""
+Get instantaneous memory statistics (installed, total, free, available)
+"""
import re
-from ..formatting import *
-from .. import config
+from logparse.formatting import *
+from logparse import config
+from logparse.load_parsers import Parser
-import logging
-logger = logging.getLogger(__name__)
+class Mem(Parser):
-def parse_log():
+ def __init__(self):
+ super().__init__()
+ self.name = "mem"
+ self.info = "Get instantaneous memory statistics "
+ "(installed, total, free, available)"
- logger.debug("Starting memory section")
- section = Section("memory")
-
- table = Table()
+ def parse_log(self):
- ram_b = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
- table.add_row(Row([Column("Installed"), Column(parsesize(ram_b))]))
+ logger.debug("Starting memory section")
+ section = Section("memory")
+
+ table = Table()
- raw_mem = util.readlog(config.prefs['logs']['meminfo'])
- line_regex = re.compile("^Mem(\w+):\s*(\d*)\s*kB$")
+ ram_b = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
+ table.add_row(Row([Column("Installed"), Column(parsesize(ram_b))]))
- for line in raw_mem.splitlines():
+ raw_mem = util.readlog(config.prefs.get("logs", "meminfo"))
+ line_regex = re.compile("^Mem(\w+):\s*(\d*)\s*kB$")
- matches = line_regex.findall(line)
+ for line in raw_mem.splitlines():
- 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))]))
+ matches = line_regex.findall(line)
- table.align_column(0, "right")
- section.append_table(table)
+ 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))]))
- logger.info("Finished memory section")
- return section
+ table.align_column(0, "right")
+ section.append_table(table)
+
+ logger.info("Finished memory section")
+ return section