rename parsers, better journald integration
[logparse.git] / logparse / parsers / temperature.py
index f2c366ccbb3f0b3db5b1a70fc3b2957adf57d5b2..18f72b5bf62f5a4b0d70846efca29a144fcb9ce2 100644 (file)
@@ -1,16 +1,13 @@
-#
-#   temperature.py
-#   
-#   Find current temperature of various system components (CPU, motherboard,
-#   hard drives, ambient). Detection of motherboard-based temperatures (CPU
-#   etc) uses the pysensors library, and produces a similar output to
-#   lmsensors. HDD temperatures are obtained from the hddtemp daemon
-#   (http://www.guzu.net/linux/hddtemp.php) which was orphaned since 2007. For
-#   hddtemp to work, it must be started in daemon mode, either manually or with
-#   a unit file. Manually, it would be started like this:
-#
-#       sudo hddtemp -d /dev/sda /dev/sdb ... /dev/sdX
-#
+"""
+Find current temperature of various system components (CPU, motherboard,
+hard drives, ambient). Detection of motherboard-based temperatures (CPU
+etc) uses the pysensors library, and produces a similar output to
+lmsensors. HDD temperatures are obtained from the hddtemp daemon
+<http://www.guzu.net/linux/hddtemp.php> which was orphaned since 2007. For
+hddtemp to work, it must be started in daemon mode, either manually or with
+a unit file. Manually, it would be started like this:
+    `sudo hddtemp -d /dev/sda /dev/sdb ... /dev/sdX`
+"""
 
 import re
 import sensors
@@ -34,7 +31,8 @@ class Drive(NamedTuple):
 
 class HddtempClient:
 
-    def __init__(self, host: str='127.0.0.1', port: int=7634, timeout: int=10, sep: str='|') -> None:
+    def __init__(self, host: str='127.0.0.1', port: int=7634, timeout: int=10,
+            sep: str='|') -> None:
         self.host = host
         self.port = port
         self.timeout = timeout
@@ -43,9 +41,10 @@ class HddtempClient:
     def _parse_drive(self, drive: str) -> Drive:
         try:
             drive_data = drive.split(self.sep)
-            return Drive(drive_data[0], drive_data[1], int(drive_data[2]), drive_data[3])
+            return Drive(drive_data[0], drive_data[1], 
+                    int(drive_data[2]), drive_data[3])
         except Exception as e:
-            logger.warning("Error processing drive: {0}".format(str(drive_data)))
+            logger.warning("Error processing drive: " + str(drive_data))
             return None
 
     def _parse(self, data: str) -> List[Drive]:
@@ -57,8 +56,6 @@ class HddtempClient:
             if parsed_drive != None:
                 parsed_drives.append(parsed_drive)
 
-#        return [self._parse_drive(drive) for drive in drives if drive != None]
-#        return list(filter(lambda drive: self._parse_drive(drive), drives))
         return parsed_drives
 
     def get_drives(self) -> List[Drive]:    # Obtain data from telnet server
@@ -67,7 +64,8 @@ class HddtempClient:
                 raw_data = tn.read_all()
             return self._parse(raw_data.decode('ascii'))    # Return parsed data
         except Exception as e:
-            logger.warning("Couldn't read data from {0}:{1} - {2}".format(self.host, self.port, str(e)))
+            logger.warning("Couldn't read data from {0}:{1} - {2}".format(
+                self.host, self.port, str(e)))
             return 1
 
 
@@ -76,7 +74,8 @@ 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)."
+        self.info = "Find current temperature of various system components "
+                "(CPU, motherboard, hard drives, ambient)."
 
     def parse_log(self):
 
@@ -93,25 +92,34 @@ class Temperature(Parser):
             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())])
+                        coretemp.items.append([feature.label, 
+                            float(feature.get_value())])
                         continue
                     if "CPUTIN" in feature.label:
-                        pkgtemp.items.append([feature.label, float(feature.get_value())])
+                        pkgtemp.items.append([feature.label, 
+                            float(feature.get_value())])
                         continue
                     if "SYS" in feature.label:
-                        systemp.items.append([feature.label, float(feature.get_value())])
+                        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)))
+                logger.debug("Looking at temp data {0}".format(
+                    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]
+                    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, avg, DEG, CEL))
+                    temp_data.subtitle += " (avg {0}{1}{2})".format(
+                            avg, DEG, CEL)
+                    temp_data.items = ["{0}: {1}{2}{3}".format(
+                        feature[0], 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)
@@ -142,16 +150,24 @@ class Temperature(Parser):
         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))
+                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("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)
+            # use units of first drive
+            hddavg = '{0:.1f}{1}{2}'.format(
+                    sumtemp/len(drives), DEG, drives[0].units)
+            logger.debug("Sum of temperatures: {}; Number of drives: {}; "
+                "=> Avg disk temp is {}".format(sumtemp, len(drives), hddavg)) 
+            hddtemp_data.subtitle += " (avg {0}{1}{2})".format(hddavg, DEG, CEL)
             section.append_data(hddtemp_data)
 
         logger.debug("Finished processing drive temperatures")