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