rename parsers, better journald integration
[logparse.git] / logparse / parsers / sysinfo.py
index 6b89f82448bf84fe1ce5e92d7617a4263060cad7..c8c43e213576b248424a32fd9e82b236f3ec899e 100644 (file)
@@ -1,13 +1,13 @@
-#
-#   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 logparse.formatting import *
 from logparse.config import prefs
@@ -26,10 +26,12 @@ class Sysinfo(Parser):
         section = Section("system")
         table = Table()
 
-        table.add_row(Row([Column("Hostname"), Column(util.hostname(prefs.get("logparse", "hostname-path")))]))
+        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())]))
+        table.add_row(Row([Column("Platform"),
+            Column(platform.system() + " " + platform.machine())]))
 
         processors = []
         raw_proc = util.readlog(prefs.get("logs", "cpuinfo"))
@@ -38,14 +40,17 @@ class Sysinfo(Parser):
         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
+                # 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))
+                    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))]))
+            table.add_row(Row([Column("Processor"),
+                Column("; ".join(processors))]))
             section.append_table(table)
         elif len(processors) > 1:
             section.append_table(table)
@@ -55,5 +60,24 @@ class Sysinfo(Parser):
         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