blob: 513c76022a56da4dbaf871c893facd01d054084b [file] [log] [blame]
Norman James323ed972015-12-09 09:06:37 -06001#!/usr/bin/python -u
2
3import sys
4import os
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import Openbmc
10from Sensors import SensorValue as SensorValue
11from Sensors import HwmonSensor as HwmonSensor
12from Sensors import SensorThresholds as SensorThresholds
13if (len(sys.argv) < 2):
14 print "Usage: sensors_hwmon.py [system name]"
15 exit(1)
16
17System = __import__(sys.argv[1])
18
19SENSOR_BUS = 'org.openbmc.Sensors'
20SENSOR_PATH = '/org/openbmc/sensors'
21DIR_POLL_INTERVAL = 10000
22HWMON_PATH = '/sys/class/hwmon'
23
24## static define which interface each property is under
25## need a better way that is not slow
26IFACE_LOOKUP = {
27 'units' : SensorValue.IFACE_NAME,
28 '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,
34}
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):
44 val = ""
45 with open(filename, 'r') as f:
46 for line in f:
47 val = line.rstrip('\n')
48 return val
49
50 def writeAttribute(self,filename,value):
51 with open(filename, 'w') as f:
52 f.write(str(value)+'\n')
53
54
55 def poll(self,objpath,attribute):
56 try:
57 raw_value = int(self.readAttribute(attribute))
58 obj = bus.get_object(SENSOR_BUS,objpath)
59 intf = dbus.Interface(obj,HwmonSensor.IFACE_NAME)
60 rtn = intf.setByPoll(raw_value)
61 if (rtn[0] == True):
62 self.writeAttribute(attribute,rtn[1])
63 except:
64 print "HWMON: Attibute no longer exists: "+attribute
65 return False
66
67
68 return True
69
70
71 def addObject(self,dpath,instance_name,attribute):
72 hwmon = System.HWMON_CONFIG[instance_name][attribute]
73 objsuf = hwmon['object_path']
74 try:
75 if (objsuf.find('<label>') > -1):
76 label_file = attribute.replace('_input','_label')
77 label = self.readAttribute(dpath+label_file)
78 objsuf = objsuf.replace('<label>',label)
79 except Exception as e:
80 print e
81 return
82
83 objpath = SENSOR_PATH+'/'+objsuf
84 spath = dpath+attribute
85 if (self.sensors.has_key(objpath) == False):
86 if os.path.isfile(spath):
87 print "HWMON add: "+objpath+" : "+spath
88 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH)
89 intf = dbus.Interface(obj,SENSOR_BUS)
90 intf.register("HwmonSensor",objpath)
91
92 obj = bus.get_object(SENSOR_BUS,objpath)
93 intf = dbus.Interface(obj,dbus.PROPERTIES_IFACE)
94 intf.Set(HwmonSensor.IFACE_NAME,'filename',spath)
95
96 ## check if one of thresholds is defined to know
97 ## whether to enable thresholds or not
98 if (hwmon.has_key('critical_upper')):
99 intf.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',True)
100
101 for prop in hwmon.keys():
102 if (IFACE_LOOKUP.has_key(prop)):
103 intf.Set(IFACE_LOOKUP[prop],prop,hwmon[prop])
104 print "Setting: "+prop+" = "+str(hwmon[prop])
105
106 self.sensors[objpath]=True
107 self.hwmon_root[dpath].append(objpath)
108 gobject.timeout_add(hwmon['poll_interval'],self.poll,objpath,spath)
109
110 def scanDirectory(self):
111 devices = os.listdir(HWMON_PATH)
112 found_hwmon = {}
113 for d in devices:
114 dpath = HWMON_PATH+'/'+d+'/'
115 found_hwmon[dpath] = True
116 if (self.hwmon_root.has_key(dpath) == False):
117 self.hwmon_root[dpath] = []
118 ## the instance name is a soft link
119 instance_name = os.path.realpath(dpath+'device').split('/').pop()
120 if (System.HWMON_CONFIG.has_key(instance_name)):
121 for attribute in System.HWMON_CONFIG[instance_name].keys():
122 self.addObject(dpath,instance_name,attribute)
123 else:
124 print "WARNING: Unhandled hwmon: "+dpath
125
126
127 for k in self.hwmon_root.keys():
128 if (found_hwmon.has_key(k) == False):
129 ## need to remove all objects associated with this path
130 print "Removing: "+k
131 for objpath in self.hwmon_root[k]:
132 if (self.sensors.has_key(objpath) == True):
133 print "HWMON remove: "+objpath
134 self.sensors.pop(objpath,None)
135 obj = bus.get_object(SENSOR_BUS,SENSOR_PATH)
136 intf = dbus.Interface(obj,SENSOR_BUS)
137 intf.delete(objpath)
138
139 self.hwmon_root.pop(k,None)
140
141 return True
142
143
144if __name__ == '__main__':
145
146 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
147 bus = Openbmc.getDBus()
148 root_sensor = Hwmons(bus)
149 mainloop = gobject.MainLoop()
150
151 print "Starting HWMON sensors"
152 mainloop.run()
153