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