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 |
Brad Bishop | e7e7e13 | 2016-07-01 10:58:08 -0400 | [diff] [blame] | 68 | self.sensors.pop(objpath,None) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 69 | return False |
| 70 | |
| 71 | |
| 72 | return True |
| 73 | |
| 74 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 75 | def addObject(self,dpath,hwmon_path,hwmon): |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 76 | objsuf = hwmon['object_path'] |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 77 | objpath = SENSOR_PATH+'/'+objsuf |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 78 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 79 | if (self.sensors.has_key(objpath) == False): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 80 | print "HWMON add: "+objpath+" : "+hwmon_path |
| 81 | |
| 82 | ## register object with sensor manager |
| 83 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
| 84 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 85 | intf.register("HwmonSensor",objpath) |
| 86 | |
| 87 | ## set some properties in dbus object |
| 88 | obj = bus.get_object(SENSOR_BUS,objpath,introspect=False) |
| 89 | intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE) |
| 90 | intf.Set(HwmonSensor.IFACE_NAME,'filename',hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 91 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 92 | ## check if one of thresholds is defined to know |
| 93 | ## whether to enable thresholds or not |
| 94 | if (hwmon.has_key('critical_upper')): |
| 95 | intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 96 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 97 | for prop in hwmon.keys(): |
| 98 | if (IFACE_LOOKUP.has_key(prop)): |
| 99 | intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop]) |
| 100 | print "Setting: "+prop+" = "+str(hwmon[prop]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 101 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 102 | self.sensors[objpath]=True |
| 103 | self.hwmon_root[dpath].append(objpath) |
| 104 | gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 105 | |
| 106 | def scanDirectory(self): |
| 107 | devices = os.listdir(HWMON_PATH) |
| 108 | found_hwmon = {} |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 109 | regx = re.compile('([a-z]+)\d+\_') |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 110 | for d in devices: |
| 111 | dpath = HWMON_PATH+'/'+d+'/' |
| 112 | found_hwmon[dpath] = True |
| 113 | if (self.hwmon_root.has_key(dpath) == False): |
| 114 | self.hwmon_root[dpath] = [] |
| 115 | ## the instance name is a soft link |
| 116 | instance_name = os.path.realpath(dpath+'device').split('/').pop() |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 117 | |
| 118 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 119 | if (System.HWMON_CONFIG.has_key(instance_name)): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 120 | hwmon = System.HWMON_CONFIG[instance_name] |
| 121 | |
| 122 | if (hwmon.has_key('labels')): |
| 123 | label_files = glob.glob(dpath+'/*_label') |
| 124 | for f in label_files: |
| 125 | label_key = self.readAttribute(f) |
| 126 | if (hwmon['labels'].has_key(label_key)): |
| 127 | namef = f.replace('_label','_input') |
| 128 | self.addObject(dpath,namef,hwmon['labels'][label_key]) |
| 129 | else: |
| 130 | pass |
| 131 | #print "WARNING - hwmon: label ("+label_key+") not found in lookup: "+f |
| 132 | |
| 133 | if hwmon.has_key('names'): |
| 134 | for attribute in hwmon['names'].keys(): |
| 135 | self.addObject(dpath,dpath+attribute,hwmon['names'][attribute]) |
| 136 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 137 | else: |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 138 | print "WARNING - hwmon: Unhandled hwmon: "+dpath |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 139 | |
| 140 | |
| 141 | for k in self.hwmon_root.keys(): |
| 142 | if (found_hwmon.has_key(k) == False): |
| 143 | ## need to remove all objects associated with this path |
| 144 | print "Removing: "+k |
| 145 | for objpath in self.hwmon_root[k]: |
| 146 | if (self.sensors.has_key(objpath) == True): |
| 147 | print "HWMON remove: "+objpath |
| 148 | self.sensors.pop(objpath,None) |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 149 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 150 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 151 | intf.delete(objpath) |
| 152 | |
| 153 | self.hwmon_root.pop(k,None) |
| 154 | |
| 155 | return True |
| 156 | |
| 157 | |
| 158 | if __name__ == '__main__': |
| 159 | |
| 160 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 161 | bus = get_dbus() |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 162 | root_sensor = Hwmons(bus) |
| 163 | mainloop = gobject.MainLoop() |
| 164 | |
| 165 | print "Starting HWMON sensors" |
| 166 | mainloop.run() |
| 167 | |