add parser-specific docs & rewrite sudo parser for journald
[logparse.git] / logparse / parsers / temperature.py
index ebca1ac1eb5ece03125f1d7b3703547f078a1671..f2c366ccbb3f0b3db5b1a70fc3b2957adf57d5b2 100644 (file)
@@ -24,6 +24,7 @@ from logparse import config
 
 import logging
 logger = logging.getLogger(__name__)
+from logparse.load_parsers import Parser
 
 class Drive(NamedTuple):
     path: str
@@ -70,81 +71,90 @@ class HddtempClient:
             return 1
 
 
-def parse_log():
-
-    logger.debug("Starting temp section")
-    section = Section("temperatures")
-
-    sensors.init()
-
-    systemp = Data("Sys", [])
-    coretemp = Data("Cores", [])
-    pkgtemp = Data("Processor", [])
-
-    try:
-        for chip in sensors.iter_detected_chips():
-            for feature in chip:
-                if "Core" in feature.label:
-                    coretemp.items.append([feature.label, float(feature.get_value())])
-                    continue
-                if "CPUTIN" in feature.label:
-                    pkgtemp.items.append([feature.label, float(feature.get_value())])
-                    continue
-                if "SYS" in feature.label:
-                    systemp.items.append([feature.label, float(feature.get_value())])
-                    continue
-
-        logger.debug("Core data is {0}".format(str(coretemp.items)))
-        logger.debug("Sys data is {0}".format(str(systemp.items)))
-        logger.debug("Pkg data is {0}".format(str(pkgtemp.items)))
-        for temp_data in [systemp, coretemp, pkgtemp]:
-            logger.debug("Looking at temp data {0}".format(str(temp_data.items)))
-            if len(temp_data.items) > 1:
-                avg = float(sum(feature[1] for feature in temp_data.items)) / len(temp_data.items)
-                logger.debug("Avg temp for {0} is {1} {2}{3}".format(temp_data.subtitle, str(avg), DEG, CEL))
-                temp_data.subtitle += " (avg {0}{1}{2})".format(str(avg), DEG, CEL)
-                temp_data.items = ["{0}: {1}{2}{3}".format(feature[0], str(feature[1]), DEG, CEL) for feature in temp_data.items]
+class Temperature(Parser):
+
+    def __init__(self):
+        super().__init__()
+        self.name = "temperature"
+        self.info = "Find current temperature of various system components (CPU, motherboard, hard drives, ambient)."
+
+    def parse_log(self):
+
+        logger.debug("Starting temp section")
+        section = Section("temperatures")
+
+        sensors.init()
+
+        systemp = Data("Sys", [])
+        coretemp = Data("Cores", [])
+        pkgtemp = Data("Processor", [])
+
+        try:
+            for chip in sensors.iter_detected_chips():
+                for feature in chip:
+                    if "Core" in feature.label:
+                        coretemp.items.append([feature.label, float(feature.get_value())])
+                        continue
+                    if "CPUTIN" in feature.label:
+                        pkgtemp.items.append([feature.label, float(feature.get_value())])
+                        continue
+                    if "SYS" in feature.label:
+                        systemp.items.append([feature.label, float(feature.get_value())])
+                        continue
+
+            logger.debug("Core data is {0}".format(str(coretemp.items)))
+            logger.debug("Sys data is {0}".format(str(systemp.items)))
+            logger.debug("Pkg data is {0}".format(str(pkgtemp.items)))
+            for temp_data in [systemp, coretemp, pkgtemp]:
+                logger.debug("Looking at temp data {0}".format(str(temp_data.items)))
+                if len(temp_data.items) > 1:
+                    avg = float(sum(feature[1] for feature in temp_data.items)) / len(temp_data.items)
+                    logger.debug("Avg temp for {0} is {1} {2}{3}".format(temp_data.subtitle, str(avg), DEG, CEL))
+                    temp_data.subtitle += " (avg {0}{1}{2})".format(str(avg), DEG, CEL)
+                    temp_data.items = ["{0}: {1}{2}{3}".format(feature[0], str(feature[1]), DEG, CEL) for feature in temp_data.items]
+                else:
+                    temp_data.items = [str(temp_data.items[0][1]) + DEG + CEL]
+                section.append_data(temp_data)
+
+        finally:
+            logger.debug("Finished reading onboard temperatures")
+            sensors.cleanup()
+
+
+        # drive temp
+
+        # For this to work, `hddtemp` must be running in daemon mode.
+        # Start it like this (bash):   sudo hddtemp -d /dev/sda /dev/sdX...
+        
+        received = ''
+        sumtemp = 0.0 
+        data = ""
+        hddtemp_data = Data("Disks")
+        
+        client = HddtempClient(
+            host=config.prefs.get("temperatures", "host"),
+            port=config.prefs.getint("temperatures", "port"),
+            sep=config.prefs.get("temperatures", "separator"),
+            timeout=int(config.prefs.get("temperatures", "timeout")))
+        drives = client.get_drives()
+        logger.debug("Received drive info: " + str(drives))
+
+        for drive in sorted(drives, key=lambda x: x.path):
+            if drive.path in config.prefs.get("temperatures", "drives").split():
+                sumtemp += drive.temperature
+                hddtemp_data.items.append(("{0} ({1})".format(drive.path, drive.model) if config.prefs.getboolean("temperatures", "show-model") else drive.path) + ": {0}{1}{2}".format(drive.temperature, DEG, drive.units))
             else:
-                temp_data.items = [str(temp_data.items[0][1]) + DEG + CEL]
-            section.append_data(temp_data)
-
-    finally:
-        logger.debug("Finished reading onboard temperatures")
-        sensors.cleanup()
-
-
-    # drive temp
-
-    # For this to work, `hddtemp` must be running in daemon mode.
-    # Start it like this (bash):   sudo hddtemp -d /dev/sda /dev/sdX...
-    
-    received = ''
-    sumtemp = 0.0 
-    data = ""
-    hddtemp_data = Data("Disks")
-    
-    client = HddtempClient(
-        host=config.prefs.get("temperatures", "host"),
-        port=config.prefs.getint("temperatures", "port"),
-        sep=config.prefs.get("temperatures", "separator"),
-        timeout=int(config.prefs.get("temperatures", "timeout")))
-    drives = client.get_drives()
-    logger.debug("Received drive info: " + str(drives))
-
-    for drive in sorted(drives, key=lambda x: x.path):
-        if drive.path in config.prefs.get("temperatures", "drives").split():
-            sumtemp += drive.temperature
-            hddtemp_data.items.append(("{0} ({1})".format(drive.path, drive.model) if config.prefs.getboolean("temperatures", "show-model") else drive.path) + ": {0}{1}{2}".format(drive.temperature, DEG, drive.units))
-        else:
-            drives.remove(drive)
-            logger.debug("Ignoring drive {0} ({1}) due to config".format(drive.path, drive.model))
-    logger.debug("Sorted drive info: " + str(drives))
-
-    hddavg = '{0:.1f}{1}{2}'.format(sumtemp/len(drives), DEG, drives[0].units) # use units of first drive
-    logger.debug("Sum of temperatures: {}; Number of drives: {}; => Avg disk temp is {}".format(str(sumtemp), str(len(drives)), hddavg)) 
-    hddtemp_data.subtitle += " (avg {0}{1}{2})".format(str(hddavg), DEG, CEL)
-
-    logger.debug("Finished processing drive temperatures")
-    logger.info("Finished temp section")
-
-    return section
+                drives.remove(drive)
+                logger.debug("Ignoring drive {0} ({1}) due to config".format(drive.path, drive.model))
+        logger.debug("Sorted drive info: " + str(drives))
+
+        if not len(drives) == 0:
+            hddavg = '{0:.1f}{1}{2}'.format(sumtemp/len(drives), DEG, drives[0].units) # use units of first drive
+            logger.debug("Sum of temperatures: {}; Number of drives: {}; => Avg disk temp is {}".format(str(sumtemp), str(len(drives)), hddavg)) 
+            hddtemp_data.subtitle += " (avg {0}{1}{2})".format(str(hddavg), DEG, CEL)
+            section.append_data(hddtemp_data)
+
+        logger.debug("Finished processing drive temperatures")
+        logger.info("Finished temp section")
+
+        return section