rename parsers, better journald integration
[logparse.git] / logparse / parsers / sysinfo.py
index 01a37c23ac4fbf95374da11bb963bfc8a3d4a388..c8c43e213576b248424a32fd9e82b236f3ec899e 100644 (file)
@@ -1,54 +1,83 @@
-#
-#   sysinfo.py
-#
-#   Get standard system information from basic Unix commands
-#
+"""
+Get standard system information from basic Unix commands
+"""
 
 import platform
 import subprocess
 import os
 import re
+from datetime import timedelta
+from multiprocessing import cpu_count
 
-from ..formatting import *
-from .. import config
-
-import logging
-logger = logging.getLogger(__name__)
-
-def parse_log():
-
-    logger.debug("Starting sysinfo section")
-    section = Section("system")
-    table = Table()
-
-    table.add_row(Row([Column("Hostname"), Column(util.hostname(config.prefs['hostname-path']))]))
-    table.add_row(Row([Column("OS"), Column(platform.platform())]))
-    table.add_row(Row([Column("OS version"), Column(platform.version())]))
-    table.add_row(Row([Column("Platform"), Column(platform.system() + " " + platform.machine())]))
-
-    processors = []
-    raw_proc = util.readlog(config.prefs['logs']['cpuinfo'])
-    line_regex = re.compile(".*model name.*:\s*")
-    proc_regex = re.compile("\s*(\(R\)|\(TM\)|CPU)")
-    for line in raw_proc.splitlines():
-        if "model name" in line:
-            processor = line_regex.sub("", line, 1)
-            processor = " ".join(proc_regex.sub("", processor).split()) # remove extraneous text and whitespace
-            if not processor in processors:
-                processors.append(processor)
-            else:
-                logger.debug("Found duplicate entry (perhaps multiple cores?) for {0}".format(processor))
-    table.align_column(0, "right")
-    if len(processors) == 1:
-        table.add_row(Row([Column("Processor"), Column("; ".join(processors))]))
-        section.append_table(table)
-    elif len(processors) > 1:
-        section.append_table(table)
-        proc_data = Data("Processors")
-        proc_data.items = processors
-        section.append_data(proc_data)
-    else:
-        logger.warning("Failed to find processor data")
-
-    logger.info("Finished sysinfo section")
-    return section
+from logparse.formatting import *
+from logparse.config import prefs
+from logparse.load_parsers import Parser
+
+class Sysinfo(Parser):
+
+    def __init__(self):
+        super().__init__()
+        self.name = "sysinfo"
+        self.info = "Get standard system information from basic Unix commands"
+
+    def parse_log(self):
+
+        logger.debug("Starting sysinfo section")
+        section = Section("system")
+        table = Table()
+
+        table.add_row(Row([Column("Hostname"), 
+            Column(util.hostname(prefs.get("logparse", "hostname-path")))]))
+        table.add_row(Row([Column("OS"), Column(platform.platform())]))
+        table.add_row(Row([Column("OS version"), Column(platform.version())]))
+        table.add_row(Row([Column("Platform"),
+            Column(platform.system() + " " + platform.machine())]))
+
+        processors = []
+        raw_proc = util.readlog(prefs.get("logs", "cpuinfo"))
+        line_regex = re.compile(".*model name.*:\s*")
+        proc_regex = re.compile("\s*(\(R\)|\(TM\)|CPU)")
+        for line in raw_proc.splitlines():
+            if "model name" in line:
+                processor = line_regex.sub("", line, 1)
+                # Remove extraneous text and whitespace:
+                processor = " ".join(proc_regex.sub("", processor).split())
+                if not processor in processors:
+                    processors.append(processor)
+                else:
+                    logger.debug("Found duplicate entry (perhaps multiple "
+                            "cores?) for {0}".format(processor))
+        table.align_column(0, "right")
+        if len(processors) == 1:
+            table.add_row(Row([Column("Processor"),
+                Column("; ".join(processors))]))
+            section.append_table(table)
+        elif len(processors) > 1:
+            section.append_table(table)
+            proc_data = Data("Processors")
+            proc_data.items = processors
+            section.append_data(proc_data)
+        else:
+            logger.warning("Failed to find processor data")
+
+        raw_uptime = util.readlog(prefs.get("logs", "uptime")).split("\n")[0]
+        logger.debug("Found uptime data " + str(raw_uptime))
+
+        uptime_total = float(raw_uptime.split()[0])
+        table.add_row(Row([Column("Uptime"),
+            Column("%d d %d h %d m" % (
+                uptime_total // 86400,
+                uptime_total % 86400 // 3600,
+                uptime_total % 3600 // 60))]))
+
+        idle_time = float(raw_uptime.split()[1]) / cpu_count()
+        m, s = divmod(idle_time, 60)
+        h, m = divmod(m, 60)
+        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))]))
+
+        logger.info("Finished sysinfo section")
+        return section