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