blob: e7b9f1ccf4762c507c57bc9e4fd705fe7e51e00c [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,
Edward A. Jamesc14f6fe2016-08-11 11:02:15 -050027 'adjust' : HwmonSensor.IFACE_NAME,
Norman James323ed972015-12-09 09:06:37 -060028 'scale' : HwmonSensor.IFACE_NAME,
29 'offset' : HwmonSensor.IFACE_NAME,
30 'critical_upper' : SensorThresholds.IFACE_NAME,
31 'warning_upper' : SensorThresholds.IFACE_NAME,
32 'critical_lower' : SensorThresholds.IFACE_NAME,
33 'warning_lower' : SensorThresholds.IFACE_NAME,
Norman James72567ba2016-01-13 16:57:48 -060034 'emergency_enabled' : SensorThresholds.IFACE_NAME,
Norman James323ed972015-12-09 09:06:37 -060035}
36
37class Hwmons():
38 def __init__(self,bus):
39 self.sensors = { }
40 self.hwmon_root = { }
41 self.scanDirectory()
42 gobject.timeout_add(DIR_POLL_INTERVAL, self.scanDirectory)
43
44 def readAttribute(self,filename):
Ken6956bbd2016-06-08 14:50:59 +080045 val = "-1"
46 try:
Brad Bishop7dc89892016-06-14 18:18:19 -040047 with open(filename, 'r') as f:
48 for line in f:
49 val = line.rstrip('\n')
Ken6956bbd2016-06-08 14:50:59 +080050 except (OSError, IOError):
Brad Bishop7dc89892016-06-14 18:18:19 -040051 print "Cannot read attributes:", filename
Norman James323ed972015-12-09 09:06:37 -060052 return val
53
54 def writeAttribute(self,filename,value):
55 with open(filename, 'w') as f:
56 f.write(str(value)+'\n')
57
58
59 def poll(self,objpath,attribute):
60 try:
61 raw_value = int(self.readAttribute(attribute))
Brad Bishopbd01c242016-06-24 08:16:19 -040062 obj = bus.get_object(SENSOR_BUS,objpath,introspect=False)
63 intf = dbus.Interface(obj,HwmonSensor.IFACE_NAME)
64 rtn = intf.setByPoll(raw_value)
65 if (rtn[0] == True):
66 self.writeAttribute(attribute,rtn[1])
Norman James323ed972015-12-09 09:06:37 -060067 except:
68 print "HWMON: Attibute no longer exists: "+attribute
Brad Bishope7e7e132016-07-01 10:58:08 -040069 self.sensors.pop(objpath,None)
Norman James323ed972015-12-09 09:06:37 -060070 return False
71
72
73 return True
74
75
Norman James3bb97d92015-12-18 14:57:22 -060076 def addObject(self,dpath,hwmon_path,hwmon):
Norman James323ed972015-12-09 09:06:37 -060077 objsuf = hwmon['object_path']
Norman James323ed972015-12-09 09:06:37 -060078 objpath = SENSOR_PATH+'/'+objsuf
Norman James3bb97d92015-12-18 14:57:22 -060079
Norman James323ed972015-12-09 09:06:37 -060080 if (self.sensors.has_key(objpath) == False):
Norman James3bb97d92015-12-18 14:57:22 -060081 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 James323ed972015-12-09 09:06:37 -060092
Norman James3bb97d92015-12-18 14:57:22 -060093 ## 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 James323ed972015-12-09 09:06:37 -060097
Norman James3bb97d92015-12-18 14:57:22 -060098 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 James323ed972015-12-09 09:06:37 -0600102
Norman James3bb97d92015-12-18 14:57:22 -0600103 self.sensors[objpath]=True
104 self.hwmon_root[dpath].append(objpath)
105 gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path)
Norman James323ed972015-12-09 09:06:37 -0600106
107 def scanDirectory(self):
108 devices = os.listdir(HWMON_PATH)
109 found_hwmon = {}
Norman James3bb97d92015-12-18 14:57:22 -0600110 regx = re.compile('([a-z]+)\d+\_')
Norman James323ed972015-12-09 09:06:37 -0600111 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 James3bb97d92015-12-18 14:57:22 -0600118
119
Norman James323ed972015-12-09 09:06:37 -0600120 if (System.HWMON_CONFIG.has_key(instance_name)):
Norman James3bb97d92015-12-18 14:57:22 -0600121 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 James323ed972015-12-09 09:06:37 -0600138 else:
Norman James3bb97d92015-12-18 14:57:22 -0600139 print "WARNING - hwmon: Unhandled hwmon: "+dpath
Norman James323ed972015-12-09 09:06:37 -0600140
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 James3bb97d92015-12-18 14:57:22 -0600150 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False)
Norman James323ed972015-12-09 09:06:37 -0600151 intf = dbus.Interface(obj,SENSOR_BUS)
152 intf.delete(objpath)
153
154 self.hwmon_root.pop(k,None)
155
156 return True
157
158
159if __name__ == '__main__':
160
161 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400162 bus = get_dbus()
Norman James323ed972015-12-09 09:06:37 -0600163 root_sensor = Hwmons(bus)
164 mainloop = gobject.MainLoop()
165
166 print "Starting HWMON sensors"
167 mainloop.run()
168