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 |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 6 | import glob |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 7 | import dbus |
| 8 | import dbus.service |
| 9 | import dbus.mainloop.glib |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 10 | import re |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 11 | from obmc.dbuslib.bindings import get_dbus |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 12 | |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 13 | from obmc.sensors import SensorValue as SensorValue |
| 14 | from obmc.sensors import HwmonSensor as HwmonSensor |
| 15 | from obmc.sensors import SensorThresholds as SensorThresholds |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 16 | import obmc_system_config as System |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 17 | |
| 18 | SENSOR_BUS = 'org.openbmc.Sensors' |
| 19 | SENSOR_PATH = '/org/openbmc/sensors' |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 20 | DIR_POLL_INTERVAL = 30000 |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 21 | HWMON_PATH = '/sys/class/hwmon' |
| 22 | |
| 23 | ## static define which interface each property is under |
| 24 | ## need a better way that is not slow |
| 25 | IFACE_LOOKUP = { |
| 26 | 'units' : SensorValue.IFACE_NAME, |
| 27 | 'scale' : HwmonSensor.IFACE_NAME, |
| 28 | 'offset' : HwmonSensor.IFACE_NAME, |
| 29 | 'critical_upper' : SensorThresholds.IFACE_NAME, |
| 30 | 'warning_upper' : SensorThresholds.IFACE_NAME, |
| 31 | 'critical_lower' : SensorThresholds.IFACE_NAME, |
| 32 | 'warning_lower' : SensorThresholds.IFACE_NAME, |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 33 | 'emergency_enabled' : SensorThresholds.IFACE_NAME, |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 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): |
Ken | 6956bbd | 2016-06-08 14:50:59 +0800 | [diff] [blame] | 44 | val = "-1" |
| 45 | try: |
Brad Bishop | 7dc8989 | 2016-06-14 18:18:19 -0400 | [diff] [blame] | 46 | with open(filename, 'r') as f: |
| 47 | for line in f: |
| 48 | val = line.rstrip('\n') |
Ken | 6956bbd | 2016-06-08 14:50:59 +0800 | [diff] [blame] | 49 | except (OSError, IOError): |
Brad Bishop | 7dc8989 | 2016-06-14 18:18:19 -0400 | [diff] [blame] | 50 | print "Cannot read attributes:", filename |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 51 | return val |
| 52 | |
| 53 | def writeAttribute(self,filename,value): |
| 54 | with open(filename, 'w') as f: |
| 55 | f.write(str(value)+'\n') |
| 56 | |
| 57 | |
| 58 | def poll(self,objpath,attribute): |
| 59 | try: |
| 60 | raw_value = int(self.readAttribute(attribute)) |
Brad Bishop | bd01c24 | 2016-06-24 08:16:19 -0400 | [diff] [blame] | 61 | obj = bus.get_object(SENSOR_BUS,objpath,introspect=False) |
| 62 | intf = dbus.Interface(obj,HwmonSensor.IFACE_NAME) |
| 63 | rtn = intf.setByPoll(raw_value) |
| 64 | if (rtn[0] == True): |
| 65 | self.writeAttribute(attribute,rtn[1]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 66 | except: |
| 67 | print "HWMON: Attibute no longer exists: "+attribute |
| 68 | return False |
| 69 | |
| 70 | |
| 71 | return True |
| 72 | |
| 73 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 74 | def addObject(self,dpath,hwmon_path,hwmon): |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 75 | objsuf = hwmon['object_path'] |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 76 | objpath = SENSOR_PATH+'/'+objsuf |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 77 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 78 | if (self.sensors.has_key(objpath) == False): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 79 | print "HWMON add: "+objpath+" : "+hwmon_path |
| 80 | |
| 81 | ## register object with sensor manager |
| 82 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
| 83 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 84 | intf.register("HwmonSensor",objpath) |
| 85 | |
| 86 | ## set some properties in dbus object |
| 87 | obj = bus.get_object(SENSOR_BUS,objpath,introspect=False) |
| 88 | intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE) |
| 89 | intf.Set(HwmonSensor.IFACE_NAME,'filename',hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 90 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 91 | ## check if one of thresholds is defined to know |
| 92 | ## whether to enable thresholds or not |
| 93 | if (hwmon.has_key('critical_upper')): |
| 94 | intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 95 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 96 | for prop in hwmon.keys(): |
| 97 | if (IFACE_LOOKUP.has_key(prop)): |
| 98 | intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop]) |
| 99 | print "Setting: "+prop+" = "+str(hwmon[prop]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 100 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 101 | self.sensors[objpath]=True |
| 102 | self.hwmon_root[dpath].append(objpath) |
| 103 | gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 104 | |
| 105 | def scanDirectory(self): |
| 106 | devices = os.listdir(HWMON_PATH) |
| 107 | found_hwmon = {} |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 108 | regx = re.compile('([a-z]+)\d+\_') |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 109 | for d in devices: |
| 110 | dpath = HWMON_PATH+'/'+d+'/' |
| 111 | found_hwmon[dpath] = True |
| 112 | if (self.hwmon_root.has_key(dpath) == False): |
| 113 | self.hwmon_root[dpath] = [] |
| 114 | ## the instance name is a soft link |
| 115 | instance_name = os.path.realpath(dpath+'device').split('/').pop() |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 116 | |
| 117 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 118 | if (System.HWMON_CONFIG.has_key(instance_name)): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 119 | hwmon = System.HWMON_CONFIG[instance_name] |
| 120 | |
| 121 | if (hwmon.has_key('labels')): |
| 122 | label_files = glob.glob(dpath+'/*_label') |
| 123 | for f in label_files: |
| 124 | label_key = self.readAttribute(f) |
| 125 | if (hwmon['labels'].has_key(label_key)): |
| 126 | namef = f.replace('_label','_input') |
| 127 | self.addObject(dpath,namef,hwmon['labels'][label_key]) |
| 128 | else: |
| 129 | pass |
| 130 | #print "WARNING - hwmon: label ("+label_key+") not found in lookup: "+f |
| 131 | |
| 132 | if hwmon.has_key('names'): |
| 133 | for attribute in hwmon['names'].keys(): |
| 134 | self.addObject(dpath,dpath+attribute,hwmon['names'][attribute]) |
| 135 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 136 | else: |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 137 | print "WARNING - hwmon: Unhandled hwmon: "+dpath |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 138 | |
| 139 | |
| 140 | for k in self.hwmon_root.keys(): |
| 141 | if (found_hwmon.has_key(k) == False): |
| 142 | ## need to remove all objects associated with this path |
| 143 | print "Removing: "+k |
| 144 | for objpath in self.hwmon_root[k]: |
| 145 | if (self.sensors.has_key(objpath) == True): |
| 146 | print "HWMON remove: "+objpath |
| 147 | self.sensors.pop(objpath,None) |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 148 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 149 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 150 | intf.delete(objpath) |
| 151 | |
| 152 | self.hwmon_root.pop(k,None) |
| 153 | |
| 154 | return True |
| 155 | |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | |
| 159 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 160 | bus = get_dbus() |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 161 | root_sensor = Hwmons(bus) |
| 162 | mainloop = gobject.MainLoop() |
| 163 | |
| 164 | print "Starting HWMON sensors" |
| 165 | mainloop.run() |
| 166 | |