#!/usr/bin/python import hal, time import commands, re, string # Note: this may not work on all computers as the output of the 'sensors' command is highly variable, # modification of the matching logic in the getSensings() function would address any host-specific nuances of the 'sensors' command output # This component will run the sensors command, extracting FAN RPM speeds, CPU temperatures, # motherboard temperatures and atx bus voltages and export them dynmaicly as float-type hal pins def getKeyname(keyname): keyname = keyname.lower(); #make lower case keyname = re.sub(' ', '_', keyname) #replace space with underscore keyname = re.sub('[^\w\d_]', '', keyname) #remove non letter/number chars keyname = re.sub('^_+', '', keyname) #remove leading underscores return keyname def cToF(c): return((float(c) * 9/5)+32) def getSensings(): outputs = {} sensings = commands.getoutput("sensors"); #run the sensors command from the lm_sensors package #print sensings line_list = string.split(sensings, '\n') for line in line_list[:]: line = line.strip('\n') # do a simple drop thru set of regex matches from most specific to least specific m = re.match('(.*?fan.*?):\s+(\d+)\s*RPM.*?min.*?(\d+).*?RPM.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) outputs[keyname + "_min"] = m.group(3) continue m = re.match('(.*?fan.*?):\s+(\d+)\s*RPM.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) continue # e.g. CPU Temperature: +25.0 C (high = +60.0 C, crit = +95.0 C) m = re.match('(.*?temp.*?):\s+\+?(\d+\.\d+).*?C.*?high.*?(\d+\.\d+).*?C.*?crit.*?(\d+\.\d+).*?C.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) outputs[keyname + "_f"] = cToF(m.group(2)) outputs[keyname + "_high"] = m.group(3) outputs[keyname + "_crit"] = m.group(4) outputs[keyname + "_high_f"] = cToF(m.group(3)) outputs[keyname + "_crit_f"] = cToF(m.group(4)) continue # e.g. CPU Temperature: +25.0 C (high = +60.0 C, crit = +95.0 C) m = re.match('(.*?temp.*?):\s+\+?(\d+\.\d+).*?C.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname + "_f"] = cToF(m.group(2)) outputs[keyname] = m.group(2) continue # e.g. +3.3 Voltage: +3.33 V (min = +2.97 V, max = +3.63 V) m = re.match('(.*?volt.*?):\s+\+?(\d+\.\d+).*?V.*?min.*?(\d+\.\d+).*?V.*?max.*?(\d+\.\d+).*?V.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) outputs[keyname + "_min"] = m.group(3) outputs[keyname + "_max"] = m.group(4) continue # e.g. +3.3 Voltage: +3.33 V (min = +2.97 V, max = +3.63 V) m = re.match('(.*?volt.*?):\s+\+?(\d+\.\d+).*?V.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) continue # e.g. Core 1: +25.0 C (high = +60.0 C, crit = +95.0 C) m = re.match('(.*?):\s+\+?(\d+\.\d+).*?C.*?high.*?(\d+\.\d+).*?C.*?crit.*?(\d+\.\d+).*?C.*', line, re.I) if m != None: keyname = getKeyname(m.group(1)) outputs[keyname] = m.group(2) outputs[keyname + "_f"] = cToF(m.group(2)) outputs[keyname + "_high"] = m.group(3) outputs[keyname + "_crit"] = m.group(4) outputs[keyname + "_high_f"] = cToF(m.group(3)) outputs[keyname + "_crit_f"] = cToF(m.group(4)) continue #print outputs return outputs sensings = getSensings() h = hal.component("sensors") for k, v in sensings.iteritems(): h.newpin(k, hal.HAL_FLOAT, hal.HAL_OUT) h.ready() try: while 1: sensings = getSensings() for k, v in sensings.iteritems(): h[k] = float(v) time.sleep(5) except KeyboardInterrupt: raise SystemExit