logparse / parsers / sysinfo.pyon commit add parser-specific docs & rewrite sudo parser for journald (aa62c56)
   1#
   2#   sysinfo.py
   3#
   4#   Get standard system information from basic Unix commands
   5#
   6
   7import platform
   8import subprocess
   9import os
  10import re
  11from datetime import timedelta
  12from multiprocessing import cpu_count
  13
  14from logparse.formatting import *
  15from logparse.config import prefs
  16from logparse.load_parsers import Parser
  17
  18class Sysinfo(Parser):
  19
  20    def __init__(self):
  21        super().__init__()
  22        self.name = "sysinfo"
  23        self.info = "Get standard system information from basic Unix commands"
  24
  25    def parse_log(self):
  26
  27        logger.debug("Starting sysinfo section")
  28        section = Section("system")
  29        table = Table()
  30
  31        table.add_row(Row([Column("Hostname"), Column(util.hostname(prefs.get("logparse", "hostname-path")))]))
  32        table.add_row(Row([Column("OS"), Column(platform.platform())]))
  33        table.add_row(Row([Column("OS version"), Column(platform.version())]))
  34        table.add_row(Row([Column("Platform"), Column(platform.system() + " " + platform.machine())]))
  35
  36        processors = []
  37        raw_proc = util.readlog(prefs.get("logs", "cpuinfo"))
  38        line_regex = re.compile(".*model name.*:\s*")
  39        proc_regex = re.compile("\s*(\(R\)|\(TM\)|CPU)")
  40        for line in raw_proc.splitlines():
  41            if "model name" in line:
  42                processor = line_regex.sub("", line, 1)
  43                processor = " ".join(proc_regex.sub("", processor).split()) # remove extraneous text and whitespace
  44                if not processor in processors:
  45                    processors.append(processor)
  46                else:
  47                    logger.debug("Found duplicate entry (perhaps multiple cores?) for {0}".format(processor))
  48        table.align_column(0, "right")
  49        if len(processors) == 1:
  50            table.add_row(Row([Column("Processor"), Column("; ".join(processors))]))
  51            section.append_table(table)
  52        elif len(processors) > 1:
  53            section.append_table(table)
  54            proc_data = Data("Processors")
  55            proc_data.items = processors
  56            section.append_data(proc_data)
  57        else:
  58            logger.warning("Failed to find processor data")
  59
  60        raw_uptime = util.readlog(prefs.get("logs", "uptime")).split("\n")[0]
  61        logger.debug("Found uptime data " + str(raw_uptime))
  62
  63        uptime_total = float(raw_uptime.split()[0])
  64        table.add_row(Row([Column("Uptime"), Column("%d d %d h %d m" % (uptime_total // 86400, uptime_total % 86400 // 3600, uptime_total % 3600 // 60))]))
  65
  66        idle_time = float(raw_uptime.split()[1]) / cpu_count()
  67        m, s = divmod(idle_time, 60)
  68        h, m = divmod(m, 60)
  69        table.add_row(Row([Column("Idle time"), Column("%d d %d h %d m per core (avg)" % (idle_time // 86400, idle_time % 86400 // 3600, idle_time % 3600 // 60))]))
  70
  71        logger.info("Finished sysinfo section")
  72        return section