from ..formatting import *
from ..util import readlog, resolve
-from .. import config
+from ..config import *
import logging
logger = logging.getLogger(__name__)
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))
+ except Exception as e:
+ logger.warning("Couldn't read data from {0}:{1} - {2}".format(self.host, self.port, str(e)))
return 1
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
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("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))
- if (prefs['hddtemp']['drives'] != ''):
+ if (config.prefs['hddtemp']['drives'] != ''):
output += writedata("disks", fields)
logger.info("Finished processing drive temperatures")