Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import Openbmc |
| 10 | from Sensors import SensorValue as SensorValue |
| 11 | from Sensors import HwmonSensor as HwmonSensor |
| 12 | from Sensors import SensorThresholds as SensorThresholds |
| 13 | if (len(sys.argv) < 2): |
| 14 | print "Usage: sensors_hwmon.py [system name]" |
| 15 | exit(1) |
| 16 | |
| 17 | System = __import__(sys.argv[1]) |
| 18 | |
| 19 | SENSOR_BUS = 'org.openbmc.Sensors' |
| 20 | SENSOR_PATH = '/org/openbmc/sensors' |
| 21 | DIR_POLL_INTERVAL = 10000 |
| 22 | HWMON_PATH = '/sys/class/hwmon' |
| 23 | |
| 24 | ## static define which interface each property is under |
| 25 | ## need a better way that is not slow |
| 26 | IFACE_LOOKUP = { |
| 27 | 'units' : SensorValue.IFACE_NAME, |
| 28 | 'scale' : HwmonSensor.IFACE_NAME, |
| 29 | 'offset' : HwmonSensor.IFACE_NAME, |
| 30 | 'critical_upper' : SensorThresholds.IFACE_NAME, |
| 31 | 'warning_upper' : SensorThresholds.IFACE_NAME, |
| 32 | 'critical_lower' : SensorThresholds.IFACE_NAME, |
| 33 | 'warning_lower' : SensorThresholds.IFACE_NAME, |
| 34 | } |
| 35 | |
| 36 | class Hwmons(): |
| 37 | def __init__(self,bus): |
| 38 | self.sensors = { } |
| 39 | self.hwmon_root = { } |
| 40 | self.scanDirectory() |
| 41 | gobject.timeout_add(DIR_POLL_INTERVAL, self.scanDirectory) |
| 42 | |
| 43 | def readAttribute(self,filename): |
| 44 | val = "" |
| 45 | with open(filename, 'r') as f: |
| 46 | for line in f: |
| 47 | val = line.rstrip('\n') |
| 48 | return val |
| 49 | |
| 50 | def writeAttribute(self,filename,value): |
| 51 | with open(filename, 'w') as f: |
| 52 | f.write(str(value)+'\n') |
| 53 | |
| 54 | |
| 55 | def poll(self,objpath,attribute): |
| 56 | try: |
| 57 | raw_value = int(self.readAttribute(attribute)) |
| 58 | obj = bus.get_object(SENSOR_BUS,objpath) |
| 59 | intf = dbus.Interface(obj,HwmonSensor.IFACE_NAME) |
| 60 | rtn = intf.setByPoll(raw_value) |
| 61 | if (rtn[0] == True): |
| 62 | self.writeAttribute(attribute,rtn[1]) |
| 63 | except: |
| 64 | print "HWMON: Attibute no longer exists: "+attribute |
| 65 | return False |
| 66 | |
| 67 | |
| 68 | return True |
| 69 | |
| 70 | |
| 71 | def addObject(self,dpath,instance_name,attribute): |
| 72 | hwmon = System.HWMON_CONFIG[instance_name][attribute] |
| 73 | objsuf = hwmon['object_path'] |
| 74 | try: |
| 75 | if (objsuf.find('<label>') > -1): |
| 76 | label_file = attribute.replace('_input','_label') |
| 77 | label = self.readAttribute(dpath+label_file) |
| 78 | objsuf = objsuf.replace('<label>',label) |
| 79 | except Exception as e: |
| 80 | print e |
| 81 | return |
| 82 | |
| 83 | objpath = SENSOR_PATH+'/'+objsuf |
| 84 | spath = dpath+attribute |
| 85 | if (self.sensors.has_key(objpath) == False): |
| 86 | if os.path.isfile(spath): |
| 87 | print "HWMON add: "+objpath+" : "+spath |
| 88 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH) |
| 89 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 90 | intf.register("HwmonSensor",objpath) |
| 91 | |
| 92 | obj = bus.get_object(SENSOR_BUS,objpath) |
| 93 | intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE) |
| 94 | intf.Set(HwmonSensor.IFACE_NAME,'filename',spath) |
| 95 | |
| 96 | ## check if one of thresholds is defined to know |
| 97 | ## whether to enable thresholds or not |
| 98 | if (hwmon.has_key('critical_upper')): |
| 99 | intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True) |
| 100 | |
| 101 | for prop in hwmon.keys(): |
| 102 | if (IFACE_LOOKUP.has_key(prop)): |
| 103 | intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop]) |
| 104 | print "Setting: "+prop+" = "+str(hwmon[prop]) |
| 105 | |
| 106 | self.sensors[objpath]=True |
| 107 | self.hwmon_root[dpath].append(objpath) |
| 108 | gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,spath) |
| 109 | |
| 110 | def scanDirectory(self): |
| 111 | devices = os.listdir(HWMON_PATH) |
| 112 | found_hwmon = {} |
| 113 | for d in devices: |
| 114 | dpath = HWMON_PATH+'/'+d+'/' |
| 115 | found_hwmon[dpath] = True |
| 116 | if (self.hwmon_root.has_key(dpath) == False): |
| 117 | self.hwmon_root[dpath] = [] |
| 118 | ## the instance name is a soft link |
| 119 | instance_name = os.path.realpath(dpath+'device').split('/').pop() |
| 120 | if (System.HWMON_CONFIG.has_key(instance_name)): |
| 121 | for attribute in System.HWMON_CONFIG[instance_name].keys(): |
| 122 | self.addObject(dpath,instance_name,attribute) |
| 123 | else: |
| 124 | print "WARNING: Unhandled hwmon: "+dpath |
| 125 | |
| 126 | |
| 127 | for k in self.hwmon_root.keys(): |
| 128 | if (found_hwmon.has_key(k) == False): |
| 129 | ## need to remove all objects associated with this path |
| 130 | print "Removing: "+k |
| 131 | for objpath in self.hwmon_root[k]: |
| 132 | if (self.sensors.has_key(objpath) == True): |
| 133 | print "HWMON remove: "+objpath |
| 134 | self.sensors.pop(objpath,None) |
| 135 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH) |
| 136 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 137 | intf.delete(objpath) |
| 138 | |
| 139 | self.hwmon_root.pop(k,None) |
| 140 | |
| 141 | return True |
| 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | |
| 146 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 147 | bus = Openbmc.getDBus() |
| 148 | root_sensor = Hwmons(bus) |
| 149 | mainloop = gobject.MainLoop() |
| 150 | |
| 151 | print "Starting HWMON sensors" |
| 152 | mainloop.run() |
| 153 | |