fix logrotate functionality
[logparse.git] / logparse / parsers / temperature.py
index 35d88e8caeb0721d7e884cffdcc4ed4179c40460..d1d4058b0f88719544ee04c86f2ee80ea100a1a7 100644 (file)
@@ -18,9 +18,10 @@ import socket, sys
 from telnetlib import Telnet
 from typing import List, Dict, NamedTuple
 
+from logparse import formatting
 from ..formatting import *
 from ..util import readlog, resolve
-from .. import config
+from ..config import * 
 
 import logging
 logger = logging.getLogger(__name__)
@@ -33,70 +34,85 @@ class Drive(NamedTuple):
 
 class HddtempClient:
 
-    def __init__(self, host: str='localhost', 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
         self.sep = sep
 
     def _parse_drive(self, drive: str) -> Drive:
-        drive_data = drive.split(self.sep)
-        return Drive(drive_data[0], drive_data[1], int(drive_data[2]), drive_data[3])
+        try:
+            drive_data = drive.split(self.sep)
+            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)))
+            return None
 
     def _parse(self, data: str) -> List[Drive]:
         line = data.lstrip(self.sep).rstrip(self.sep)   # Remove first/last
         drives = line.split(self.sep * 2)
-        return [self._parse_drive(drive) for drive in drives]
+        parsed_drives = []
+        for drive in drives:
+            parsed_drive = self._parse_drive(drive)
+            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
         try:
             with Telnet(self.host, self.port, timeout=self.timeout) as tn:
-                data = tn.read_all()
-            return self._parse(data.decode('ascii'))    # Return parsed data
-        except:
-            logger.warning("Couldn't read data from {0}:{1}".format(self.host, self.port))
+                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)))
             return 1
 
 
 def parse_log():
-    logger.debug("Starting temp section")
-    output = writetitle("temperatures")
-    output += opentag('div', 1, 'temp', 'section')
 
-    # cpu temp
+    logger.debug("Starting temp section")
+    section = Section("temperatures")
 
     sensors.init()
-    coretemps = []
-    pkgtemp = 0
-    systemp = 0
+
+    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:
-                    coretemps.append([feature.label, feature.get_value()])
-                    logger.debug("found core " + feature.label + " at temp " + str(feature.get_value()))
+                    coretemp.items.append([feature.label, float(feature.get_value())])
+                    continue
                 if "CPUTIN" in feature.label:
-                    pkgtemp = str(feature.get_value())
-                    logger.debug("found cpu package at temperature " + pkgtemp)
+                    pkgtemp.items.append([feature.label, float(feature.get_value())])
+                    continue
                 if "SYS" in feature.label:
-                    systemp = feature.get_value()
-                    logger.debug("found sys input " + feature.label + " at temp " + str(feature.get_value()))
-        logger.debug("Core temp data is: " + str(coretemps))
-#        core_avg = reduce(lambda x, y: x[1] + y[1], coretemps) / len(coretemps)
-        core_avg = sum(core[1] for core in coretemps) / len(coretemps)
-        logger.debug("average cpu temp is " + str(core_avg))
-        coretemps.append(["avg", str(core_avg)])
-        coretemps.append(["pkg", pkgtemp])
-        coretemps = [x[0] + ": " + str(x[1]) + DEG + CEL for x in coretemps]
+                    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()
 
-    if (systemp != 0):
-        output += writedata("sys: " + str(systemp) + DEG)
-    if (coretemps != ''):
-        output += writedata("cores", coretemps)
-    
-    logger.info("Finished reading onboard temperatures")
 
     # drive temp
 
@@ -106,28 +122,26 @@ def parse_log():
     received = ''
     sumtemp = 0.0 
     data = ""
-    fields = []
+    hddtemp_data = Data("Disks")
     
     client = HddtempClient(host=config.prefs['hddtemp']['host'], port=int(config.prefs['hddtemp']['port']), sep=config.prefs['hddtemp']['separator'], timeout=int(config.prefs['hddtemp']['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['hddtemp']['drives']:
             sumtemp += drive.temperature
-            fields.append(("{0} ({1})".format(drive.path, drive.model) if config.prefs['hddtemp']['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['hddtemp']['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("Received drive info: " + str(drives))
+            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)) 
-    fields.append("avg: " + str(hddavg))
+    hddtemp_data.subtitle += " (avg {0}{1}{2})".format(str(hddavg), DEG, CEL)
 
-    if (prefs['hddtemp']['drives'] != ''):
-        output += writedata("disks", fields)
-    logger.info("Finished processing drive temperatures")
-
-
-    output += closetag('div', 1)
+    logger.debug("Finished processing drive temperatures")
     logger.info("Finished temp section")
-    return output
+
+    return section