logparse / parsers / mem.pyon commit update readme & docs (29c0dc8)
   1#
   2#   mem.py
   3#
   4#   Get instantaneous memory statistics (installed, total, free, available)
   5#
   6
   7import re
   8
   9from logparse.formatting import *
  10from logparse import config
  11from logparse.load_parsers import Parser
  12
  13class Mem(Parser):
  14
  15    def __init__(self):
  16        super().__init__()
  17        self.name = "mem"
  18        self.info = "Get instantaneous memory statistics (installed, total, free, available)"
  19
  20    def parse_log(self):
  21
  22        logger.debug("Starting memory section")
  23        section = Section("memory")
  24        
  25        table = Table()
  26
  27        ram_b = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
  28        table.add_row(Row([Column("Installed"), Column(parsesize(ram_b))]))
  29
  30        raw_mem = util.readlog(config.prefs.get("logs", "meminfo"))
  31        line_regex = re.compile("^Mem(\w+):\s*(\d*)\s*kB$")
  32
  33        for line in raw_mem.splitlines():
  34
  35            matches = line_regex.findall(line)
  36
  37            if len(matches) > 0:
  38                logger.debug("Detected {0} memory of {1} kB".format(matches[0][0].lower(), matches[0][1]))
  39                table.add_row(Row([Column(matches[0][0]), Column(parsesize(float(matches[0][1])*1000))]))
  40
  41        table.align_column(0, "right")
  42        section.append_table(table)
  43
  44        logger.info("Finished memory section")
  45        return section