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