- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect(('localhost',config['hddtemp']['port']))
- output = s.recv(4096)
- output += s.recv(4096)
- s.close()
- config['hddtemp']['drives'] = []
- for drive in re.split('\|1}', output):
- try:
- fields = re.search('\|*(/dev/sd.)\|.*\|(\d+)\|(.)', drive)
- name = fields.group(1)
- temp = float(fields.group(2))
- units = fields.group(3)
- config['hddtemp']['drives'].append(drivetemp(name, temp, DEG))
- except:
- pass
- hddtotal = 0
- data = []
- for drive in config['hddtemp']['drives']:
- data.append(drive.name + ': ' + str(drive.temp) + drive.units)
- logger.debug("found disk " + drive.name + " at " + str(drive.temp))
- hddtotal += drive.temp
- logger.debug("found " + str(len(config['hddtemp']['drives'])) + " disks")
- logger.debug("sum of disk temps is " + str(hddtotal))
- hddavg = "{0:.2f}".format(hddtotal/float(len(config['hddtemp']['drives']))) + DEG
- logger.debug("avg disk temp is " + str(hddavg))
- data.append("avg: " + str(hddavg))
+
+ received = ''
+ sumtemp = 0
+ data = ""
+ output = []
+
+ try:
+ hsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ hsock.connect(("localhost", int(config['hddtemp']['port'])))
+ logger.debug("tcp socket on port " + str(int(config['hddtemp']['port'])) + " opened for `hddtemp` (ensure daemon is running)")
+ hsock.sendall('') # send dummy packet and shut write conn
+ hsock.shutdown(socket.SHUT_WR)
+
+ while 1:
+ line = hsock.recv(1024)
+ if line == "": # exit on blank line
+ break
+ logger.debug("received line " + str(line))
+ data += line
+ hsock.close()
+ logger.debug("closed connection, having received " + str(sys.getsizeof(data)) + " bytes")
+
+ data = data.lstrip('|').rstrip('|') # remove leading & trailing `|`
+ drives = data.split('|' * 2) # split into drives
+
+ for drive in drives:
+ fields = drive.split('|')
+ if fields[0] in config['hddtemp']['drives']:
+ output.append(fields[0] + (' (' + fields[1] + ')' if config['hddtemp']['show-model'] else '')+ ': ' + fields[2] + DEG + fields[3])
+ sumtemp += int(fields[2])
+ logger.debug("added drive " + fields[0])
+ else:
+ logger.debug("ignoring drive " + fields[0])
+
+ hddavg = int(format(sumtemp/float(len(drives)))) + e + DEG + output[0][-1:] # use units of first drive (last character of output)
+ logger.debug("avg disk temp is " + str(hddavg))
+ output.append("avg: " + str(hddavg))
+ except Exception as ex:
+ logger.debug("failed getting hddtemps with error " + str(ex))
+ finally:
+ hsock.close()
+