blob: bb363dc9ac34aabd5ddde580dceb36109bd02d99 [file] [log] [blame]
Norman James323ed972015-12-09 09:06:37 -06001#!/usr/bin/python -u
2
3import sys
4import os
5import gobject
Norman James3bb97d92015-12-18 14:57:22 -06006import glob
Norman James323ed972015-12-09 09:06:37 -06007import dbus
8import dbus.service
9import dbus.mainloop.glib
Norman James3bb97d92015-12-18 14:57:22 -060010import re
Brad Bishop84e73b52016-05-12 15:57:52 -040011from obmc.dbuslib.bindings import get_dbus
Norman James3bb97d92015-12-18 14:57:22 -060012
Brad Bishopee1b1542016-05-12 16:55:00 -040013from obmc.sensors import SensorValue as SensorValue
14from obmc.sensors import HwmonSensor as HwmonSensor
15from obmc.sensors import SensorThresholds as SensorThresholds
Brad Bishop0b380f72016-06-10 00:29:50 -040016import obmc_system_config as System
Norman James323ed972015-12-09 09:06:37 -060017
18SENSOR_BUS = 'org.openbmc.Sensors'
19SENSOR_PATH = '/org/openbmc/sensors'
Norman James3bb97d92015-12-18 14:57:22 -060020DIR_POLL_INTERVAL = 30000
Norman James323ed972015-12-09 09:06:37 -060021HWMON_PATH = '/sys/class/hwmon'
22
23## static define which interface each property is under
24## need a better way that is not slow
25IFACE_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 James72567ba2016-01-13 16:57:48 -060033 'emergency_enabled' : SensorThresholds.IFACE_NAME,
Norman James323ed972015-12-09 09:06:37 -060034}
35
36class 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):
Ken6956bbd2016-06-08 14:50:59 +080044 val = "-1"
45 try:
Brad Bishop7dc89892016-06-14 18:18:19 -040046 with open(filename, 'r') as f:
47 for line in f:
48 val = line.rstrip('\n')
Ken6956bbd2016-06-08 14:50:59 +080049 except (OSError, IOError):
Brad Bishop7dc89892016-06-14 18:18:19 -040050 print "Cannot read attributes:", filename
Norman James323ed972015-12-09 09:06:37 -060051 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 Bishopbd01c242016-06-24 08:16:19 -040061 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 James323ed972015-12-09 09:06:37 -060066 except:
67 print "HWMON: Attibute no longer exists: "+attribute
Brad Bishope7e7e132016-07-01 10:58:08 -040068 self.sensors.pop(objpath,None)
Norman James323ed972015-12-09 09:06:37 -060069 return False
70
71
72 return True
73
74
Norman James3bb97d92015-12-18 14:57:22 -060075 def addObject(self,dpath,hwmon_path,hwmon):
Norman James323ed972015-12-09 09:06:37 -060076 objsuf = hwmon['object_path']
Norman James323ed972015-12-09 09:06:37 -060077 objpath = SENSOR_PATH+'/'+objsuf
Norman James3bb97d92015-12-18 14:57:22 -060078
Norman James323ed972015-12-09 09:06:37 -060079 if (self.sensors.has_key(objpath) == False):
Norman James3bb97d92015-12-18 14:57:22 -060080 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 James323ed972015-12-09 09:06:37 -060091
Norman James3bb97d92015-12-18 14:57:22 -060092 ## 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 James323ed972015-12-09 09:06:37 -060096
Norman James3bb97d92015-12-18 14:57:22 -060097 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 James323ed972015-12-09 09:06:37 -0600101
Norman James3bb97d92015-12-18 14:57:22 -0600102 self.sensors[objpath]=True
103 self.hwmon_root[dpath].append(objpath)
104 gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path)
Norman James323ed972015-12-09 09:06:37 -0600105
106 def scanDirectory(self):
107 devices = os.listdir(HWMON_PATH)
108 found_hwmon = {}
Norman James3bb97d92015-12-18 14:57:22 -0600109 regx = re.compile('([a-z]+)\d+\_')
Norman James323ed972015-12-09 09:06:37 -0600110 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 James3bb97d92015-12-18 14:57:22 -0600117
118
Norman James323ed972015-12-09 09:06:37 -0600119 if (System.HWMON_CONFIG.has_key(instance_name)):
Norman James3bb97d92015-12-18 14:57:22 -0600120 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 James323ed972015-12-09 09:06:37 -0600137 else:
Norman James3bb97d92015-12-18 14:57:22 -0600138 print "WARNING - hwmon: Unhandled hwmon: "+dpath
Norman James323ed972015-12-09 09:06:37 -0600139
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 James3bb97d92015-12-18 14:57:22 -0600149 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False)
Norman James323ed972015-12-09 09:06:37 -0600150 intf = dbus.Interface(obj,SENSOR_BUS)
151 intf.delete(objpath)
152
153 self.hwmon_root.pop(k,None)
154
155 return True
156
157
158if __name__ == '__main__':
159
160 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400161 bus = get_dbus()
Norman James323ed972015-12-09 09:06:37 -0600162 root_sensor = Hwmons(bus)
163 mainloop = gobject.MainLoop()
164
165 print "Starting HWMON sensors"
166 mainloop.run()
167