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 |
| 10 | import Openbmc |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 11 | import re |
| 12 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 13 | from Sensors import SensorValue as SensorValue |
| 14 | from Sensors import HwmonSensor as HwmonSensor |
| 15 | from 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): |
| 50 | val = "" |
| 51 | with open(filename, 'r') as f: |
| 52 | for line in f: |
| 53 | val = line.rstrip('\n') |
| 54 | return val |
| 55 | |
| 56 | def writeAttribute(self,filename,value): |
| 57 | with open(filename, 'w') as f: |
| 58 | f.write(str(value)+'\n') |
| 59 | |
Brad Bishop | efc6897 | 2016-06-05 17:56:20 -0400 | [diff] [blame^] | 60 | def should_update(attribute, value): |
| 61 | if attribute not in self.cache: |
| 62 | self.cache[attribute] = value |
| 63 | return True |
| 64 | |
| 65 | update = (value != self.cache[attribute]) |
| 66 | self.cache[attribute] = value |
| 67 | |
| 68 | return update |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 69 | |
| 70 | def poll(self,objpath,attribute): |
| 71 | try: |
| 72 | raw_value = int(self.readAttribute(attribute)) |
Brad Bishop | efc6897 | 2016-06-05 17:56:20 -0400 | [diff] [blame^] | 73 | if self.should_update(attribute, raw_value): |
| 74 | obj = bus.get_object(SENSOR_BUS,objpath,introspect=False) |
| 75 | intf = dbus.Interface(obj,HwmonSensor.IFACE_NAME) |
| 76 | rtn = intf.setByPoll(raw_value) |
| 77 | if (rtn[0] == True): |
| 78 | self.writeAttribute(attribute,rtn[1]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 79 | except: |
| 80 | print "HWMON: Attibute no longer exists: "+attribute |
Brad Bishop | efc6897 | 2016-06-05 17:56:20 -0400 | [diff] [blame^] | 81 | if attribute in self.cache: |
| 82 | del self.cache[attribute] |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 83 | return False |
| 84 | |
| 85 | |
| 86 | return True |
| 87 | |
| 88 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 89 | def addObject(self,dpath,hwmon_path,hwmon): |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 90 | objsuf = hwmon['object_path'] |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 91 | objpath = SENSOR_PATH+'/'+objsuf |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 92 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 93 | if (self.sensors.has_key(objpath) == False): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 94 | print "HWMON add: "+objpath+" : "+hwmon_path |
| 95 | |
| 96 | ## register object with sensor manager |
| 97 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
| 98 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 99 | intf.register("HwmonSensor",objpath) |
| 100 | |
| 101 | ## set some properties in dbus object |
| 102 | obj = bus.get_object(SENSOR_BUS,objpath,introspect=False) |
| 103 | intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE) |
| 104 | intf.Set(HwmonSensor.IFACE_NAME,'filename',hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 105 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 106 | ## check if one of thresholds is defined to know |
| 107 | ## whether to enable thresholds or not |
| 108 | if (hwmon.has_key('critical_upper')): |
| 109 | intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 110 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 111 | for prop in hwmon.keys(): |
| 112 | if (IFACE_LOOKUP.has_key(prop)): |
| 113 | intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop]) |
| 114 | print "Setting: "+prop+" = "+str(hwmon[prop]) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 115 | |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 116 | self.sensors[objpath]=True |
| 117 | self.hwmon_root[dpath].append(objpath) |
| 118 | gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 119 | |
| 120 | def scanDirectory(self): |
| 121 | devices = os.listdir(HWMON_PATH) |
| 122 | found_hwmon = {} |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 123 | regx = re.compile('([a-z]+)\d+\_') |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 124 | for d in devices: |
| 125 | dpath = HWMON_PATH+'/'+d+'/' |
| 126 | found_hwmon[dpath] = True |
| 127 | if (self.hwmon_root.has_key(dpath) == False): |
| 128 | self.hwmon_root[dpath] = [] |
| 129 | ## the instance name is a soft link |
| 130 | instance_name = os.path.realpath(dpath+'device').split('/').pop() |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 131 | |
| 132 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 133 | if (System.HWMON_CONFIG.has_key(instance_name)): |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 134 | hwmon = System.HWMON_CONFIG[instance_name] |
| 135 | |
| 136 | if (hwmon.has_key('labels')): |
| 137 | label_files = glob.glob(dpath+'/*_label') |
| 138 | for f in label_files: |
| 139 | label_key = self.readAttribute(f) |
| 140 | if (hwmon['labels'].has_key(label_key)): |
| 141 | namef = f.replace('_label','_input') |
| 142 | self.addObject(dpath,namef,hwmon['labels'][label_key]) |
| 143 | else: |
| 144 | pass |
| 145 | #print "WARNING - hwmon: label ("+label_key+") not found in lookup: "+f |
| 146 | |
| 147 | if hwmon.has_key('names'): |
| 148 | for attribute in hwmon['names'].keys(): |
| 149 | self.addObject(dpath,dpath+attribute,hwmon['names'][attribute]) |
| 150 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 151 | else: |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 152 | print "WARNING - hwmon: Unhandled hwmon: "+dpath |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 153 | |
| 154 | |
| 155 | for k in self.hwmon_root.keys(): |
| 156 | if (found_hwmon.has_key(k) == False): |
| 157 | ## need to remove all objects associated with this path |
| 158 | print "Removing: "+k |
| 159 | for objpath in self.hwmon_root[k]: |
| 160 | if (self.sensors.has_key(objpath) == True): |
| 161 | print "HWMON remove: "+objpath |
| 162 | self.sensors.pop(objpath,None) |
Norman James | 3bb97d9 | 2015-12-18 14:57:22 -0600 | [diff] [blame] | 163 | obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 164 | intf = dbus.Interface(obj,SENSOR_BUS) |
| 165 | intf.delete(objpath) |
| 166 | |
| 167 | self.hwmon_root.pop(k,None) |
| 168 | |
| 169 | return True |
| 170 | |
| 171 | |
| 172 | if __name__ == '__main__': |
| 173 | |
| 174 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 175 | bus = Openbmc.getDBus() |
| 176 | root_sensor = Hwmons(bus) |
| 177 | mainloop = gobject.MainLoop() |
| 178 | |
| 179 | print "Starting HWMON sensors" |
| 180 | mainloop.run() |
| 181 | |