blob: 1209d469917eca4db9986fdec3973e93573d1531 [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
10import Openbmc
Norman James3bb97d92015-12-18 14:57:22 -060011import re
12
Norman James323ed972015-12-09 09:06:37 -060013from Sensors import SensorValue as SensorValue
14from Sensors import HwmonSensor as HwmonSensor
15from Sensors import SensorThresholds as SensorThresholds
Norman James3bb97d92015-12-18 14:57:22 -060016
Norman James323ed972015-12-09 09:06:37 -060017if (len(sys.argv) < 2):
18 print "Usage: sensors_hwmon.py [system name]"
19 exit(1)
20
21System = __import__(sys.argv[1])
22
23SENSOR_BUS = 'org.openbmc.Sensors'
24SENSOR_PATH = '/org/openbmc/sensors'
Norman James3bb97d92015-12-18 14:57:22 -060025DIR_POLL_INTERVAL = 30000
Norman James323ed972015-12-09 09:06:37 -060026HWMON_PATH = '/sys/class/hwmon'
27
28## static define which interface each property is under
29## need a better way that is not slow
30IFACE_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 James72567ba2016-01-13 16:57:48 -060038 'emergency_enabled' : SensorThresholds.IFACE_NAME,
Norman James323ed972015-12-09 09:06:37 -060039}
40
41class 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 Bishopefc68972016-06-05 17:56:20 -040047 self.cache = {}
Norman James323ed972015-12-09 09:06:37 -060048
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 Bishopefc68972016-06-05 17:56:20 -040060 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 James323ed972015-12-09 09:06:37 -060069
70 def poll(self,objpath,attribute):
71 try:
72 raw_value = int(self.readAttribute(attribute))
Brad Bishopefc68972016-06-05 17:56:20 -040073 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 James323ed972015-12-09 09:06:37 -060079 except:
80 print "HWMON: Attibute no longer exists: "+attribute
Brad Bishopefc68972016-06-05 17:56:20 -040081 if attribute in self.cache:
82 del self.cache[attribute]
Norman James323ed972015-12-09 09:06:37 -060083 return False
84
85
86 return True
87
88
Norman James3bb97d92015-12-18 14:57:22 -060089 def addObject(self,dpath,hwmon_path,hwmon):
Norman James323ed972015-12-09 09:06:37 -060090 objsuf = hwmon['object_path']
Norman James323ed972015-12-09 09:06:37 -060091 objpath = SENSOR_PATH+'/'+objsuf
Norman James3bb97d92015-12-18 14:57:22 -060092
Norman James323ed972015-12-09 09:06:37 -060093 if (self.sensors.has_key(objpath) == False):
Norman James3bb97d92015-12-18 14:57:22 -060094 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 James323ed972015-12-09 09:06:37 -0600105
Norman James3bb97d92015-12-18 14:57:22 -0600106 ## 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 James323ed972015-12-09 09:06:37 -0600110
Norman James3bb97d92015-12-18 14:57:22 -0600111 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 James323ed972015-12-09 09:06:37 -0600115
Norman James3bb97d92015-12-18 14:57:22 -0600116 self.sensors[objpath]=True
117 self.hwmon_root[dpath].append(objpath)
118 gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path)
Norman James323ed972015-12-09 09:06:37 -0600119
120 def scanDirectory(self):
121 devices = os.listdir(HWMON_PATH)
122 found_hwmon = {}
Norman James3bb97d92015-12-18 14:57:22 -0600123 regx = re.compile('([a-z]+)\d+\_')
Norman James323ed972015-12-09 09:06:37 -0600124 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 James3bb97d92015-12-18 14:57:22 -0600131
132
Norman James323ed972015-12-09 09:06:37 -0600133 if (System.HWMON_CONFIG.has_key(instance_name)):
Norman James3bb97d92015-12-18 14:57:22 -0600134 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 James323ed972015-12-09 09:06:37 -0600151 else:
Norman James3bb97d92015-12-18 14:57:22 -0600152 print "WARNING - hwmon: Unhandled hwmon: "+dpath
Norman James323ed972015-12-09 09:06:37 -0600153
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 James3bb97d92015-12-18 14:57:22 -0600163 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False)
Norman James323ed972015-12-09 09:06:37 -0600164 intf = dbus.Interface(obj,SENSOR_BUS)
165 intf.delete(objpath)
166
167 self.hwmon_root.pop(k,None)
168
169 return True
170
171
172if __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