blob: e5dc19afcddbf09a2d96695273994733f90eb1ec [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
68 return False
69
70
71 return True
72
73
Norman James3bb97d92015-12-18 14:57:22 -060074 def addObject(self,dpath,hwmon_path,hwmon):
Norman James323ed972015-12-09 09:06:37 -060075 objsuf = hwmon['object_path']
Norman James323ed972015-12-09 09:06:37 -060076 objpath = SENSOR_PATH+'/'+objsuf
Norman James3bb97d92015-12-18 14:57:22 -060077
Norman James323ed972015-12-09 09:06:37 -060078 if (self.sensors.has_key(objpath) == False):
Norman James3bb97d92015-12-18 14:57:22 -060079 print "HWMON add: "+objpath+" : "+hwmon_path
80
81 ## register object with sensor manager
82 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False)
83 intf = dbus.Interface(obj,SENSOR_BUS)
84 intf.register("HwmonSensor",objpath)
85
86 ## set some properties in dbus object
87 obj = bus.get_object(SENSOR_BUS,objpath,introspect=False)
88 intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE)
89 intf.Set(HwmonSensor.IFACE_NAME,'filename',hwmon_path)
Norman James323ed972015-12-09 09:06:37 -060090
Norman James3bb97d92015-12-18 14:57:22 -060091 ## check if one of thresholds is defined to know
92 ## whether to enable thresholds or not
93 if (hwmon.has_key('critical_upper')):
94 intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True)
Norman James323ed972015-12-09 09:06:37 -060095
Norman James3bb97d92015-12-18 14:57:22 -060096 for prop in hwmon.keys():
97 if (IFACE_LOOKUP.has_key(prop)):
98 intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop])
99 print "Setting: "+prop+" = "+str(hwmon[prop])
Norman James323ed972015-12-09 09:06:37 -0600100
Norman James3bb97d92015-12-18 14:57:22 -0600101 self.sensors[objpath]=True
102 self.hwmon_root[dpath].append(objpath)
103 gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,hwmon_path)
Norman James323ed972015-12-09 09:06:37 -0600104
105 def scanDirectory(self):
106 devices = os.listdir(HWMON_PATH)
107 found_hwmon = {}
Norman James3bb97d92015-12-18 14:57:22 -0600108 regx = re.compile('([a-z]+)\d+\_')
Norman James323ed972015-12-09 09:06:37 -0600109 for d in devices:
110 dpath = HWMON_PATH+'/'+d+'/'
111 found_hwmon[dpath] = True
112 if (self.hwmon_root.has_key(dpath) == False):
113 self.hwmon_root[dpath] = []
114 ## the instance name is a soft link
115 instance_name = os.path.realpath(dpath+'device').split('/').pop()
Norman James3bb97d92015-12-18 14:57:22 -0600116
117
Norman James323ed972015-12-09 09:06:37 -0600118 if (System.HWMON_CONFIG.has_key(instance_name)):
Norman James3bb97d92015-12-18 14:57:22 -0600119 hwmon = System.HWMON_CONFIG[instance_name]
120
121 if (hwmon.has_key('labels')):
122 label_files = glob.glob(dpath+'/*_label')
123 for f in label_files:
124 label_key = self.readAttribute(f)
125 if (hwmon['labels'].has_key(label_key)):
126 namef = f.replace('_label','_input')
127 self.addObject(dpath,namef,hwmon['labels'][label_key])
128 else:
129 pass
130 #print "WARNING - hwmon: label ("+label_key+") not found in lookup: "+f
131
132 if hwmon.has_key('names'):
133 for attribute in hwmon['names'].keys():
134 self.addObject(dpath,dpath+attribute,hwmon['names'][attribute])
135
Norman James323ed972015-12-09 09:06:37 -0600136 else:
Norman James3bb97d92015-12-18 14:57:22 -0600137 print "WARNING - hwmon: Unhandled hwmon: "+dpath
Norman James323ed972015-12-09 09:06:37 -0600138
139
140 for k in self.hwmon_root.keys():
141 if (found_hwmon.has_key(k) == False):
142 ## need to remove all objects associated with this path
143 print "Removing: "+k
144 for objpath in self.hwmon_root[k]:
145 if (self.sensors.has_key(objpath) == True):
146 print "HWMON remove: "+objpath
147 self.sensors.pop(objpath,None)
Norman James3bb97d92015-12-18 14:57:22 -0600148 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH,introspect=False)
Norman James323ed972015-12-09 09:06:37 -0600149 intf = dbus.Interface(obj,SENSOR_BUS)
150 intf.delete(objpath)
151
152 self.hwmon_root.pop(k,None)
153
154 return True
155
156
157if __name__ == '__main__':
158
159 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400160 bus = get_dbus()
Norman James323ed972015-12-09 09:06:37 -0600161 root_sensor = Hwmons(bus)
162 mainloop = gobject.MainLoop()
163
164 print "Starting HWMON sensors"
165 mainloop.run()
166