blob: 08c6f06c6f383496bc237b824e05b4965d3b8af9 [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)
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 James3bb97d92015-12-18 14:57:22 -060063 obj = bus.get_object(SENSOR_BUS,objpath,introspect=False)
Norman James323ed972015-12-09 09:06:37 -060064 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 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)
162 bus = Openbmc.getDBus()
163 root_sensor = Hwmons(bus)
164 mainloop = gobject.MainLoop()
165
166 print "Starting HWMON sensors"
167 mainloop.run()
168