Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 4 | #from gi.repository import GObject |
| 5 | import gobject |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | |
| 10 | if (len(sys.argv) < 2): |
| 11 | print "Usage: sensor_manager.py [system name]" |
| 12 | exit(1) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 13 | System = __import__(sys.argv[1]) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 14 | import Openbmc |
| 15 | |
| 16 | DBUS_NAME = 'org.openbmc.managers.Sensors' |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 17 | OBJ_NAME = '/org/openbmc/managers/Sensors' |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 18 | |
| 19 | class SensorManager(dbus.service.Object): |
| 20 | def __init__(self,bus,name): |
| 21 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 22 | bus.add_signal_receiver(self.SensorChangedHandler, |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 23 | dbus_interface = 'org.openbmc.SensorValue', |
| 24 | signal_name = 'Changed', path_keyword='path') |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 25 | bus.add_signal_receiver(self.NormalThreshold, |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 26 | dbus_interface = 'org.openbmc.SensorThreshold', |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 27 | signal_name = 'Normal', path_keyword='path') |
| 28 | bus.add_signal_receiver(self.WarningThreshold, |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 29 | dbus_interface = 'org.openbmc.SensorThreshold', |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 30 | signal_name = 'Warning', path_keyword='path') |
| 31 | bus.add_signal_receiver(self.CriticalThreshold, |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 32 | dbus_interface = 'org.openbmc.SensorThreshold', |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 33 | signal_name = 'Critical', path_keyword='path') |
| 34 | |
| 35 | self.sensor_cache = {} |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 36 | |
| 37 | @dbus.service.method(DBUS_NAME, |
| 38 | in_signature='', out_signature='a{sv}') |
| 39 | def getSensorsAll(self): |
| 40 | return self.sensor_cache; |
| 41 | |
| 42 | @dbus.service.method(DBUS_NAME, |
| 43 | in_signature='y', out_signature='v') |
| 44 | def getSensorFromId(self,ipmi_id): |
| 45 | intf_sys = Openbmc.getManagerInterface(bus,"System") |
| 46 | obj_info = intf_sys.getObjFromIpmi(ipmi_id) |
| 47 | intf_name = str(obj_info[0]) |
| 48 | obj_name = str(obj_info[1]) |
| 49 | return self.getSensor(obj_name) |
| 50 | |
| 51 | @dbus.service.method(DBUS_NAME, |
| 52 | in_signature='yv', out_signature='') |
| 53 | def setSensorFromId(self,ipmi_id,value): |
| 54 | intf_sys = Openbmc.getManagerInterface(bus,"System") |
| 55 | obj_info = intf_sys.getObjFromIpmi(ipmi_id) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 56 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 57 | obj = bus.get_object(obj_info[0],obj_info[1]) |
| 58 | intf = dbus.Interface(obj,"org.openbmc.SensorValue") |
| 59 | intf.setValue(value) |
| 60 | return None |
| 61 | |
| 62 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 63 | @dbus.service.method(DBUS_NAME, |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 64 | in_signature='s', out_signature='v') |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 65 | def getSensor(self,path): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 66 | val = 0 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 67 | if (self.sensor_cache.has_key(path) == True): |
| 68 | val = self.sensor_cache[path]['value'] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 69 | else: |
| 70 | # TODO: error handling |
| 71 | print "Unknown sensor at: "+path |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 72 | return val |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 73 | |
| 74 | ## Signal handlers |
| 75 | def SensorChangedHandler(self,value,units,path = None): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 76 | self.initSensorEntry(path) |
| 77 | self.sensor_cache[path]['value'] = value |
| 78 | self.sensor_cache[path]['units'] = units |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 79 | |
| 80 | @dbus.service.signal(DBUS_NAME) |
| 81 | def CriticalThreshold(self, path = None): |
| 82 | print "Critical: "+path |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 83 | self.initSensorEntry(path) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 84 | self.sensor_cache[path]['threshold'] = "CRITICAL" |
| 85 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 86 | @dbus.service.signal(DBUS_NAME) |
| 87 | def WarningThreshold(self, path = None): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 88 | print "Warning:"+path |
| 89 | self.initSensorEntry(path) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 90 | self.sensor_cache[path]['threshold'] = "WARNING" |
| 91 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 92 | @dbus.service.signal(DBUS_NAME) |
| 93 | def NormalThreshold(self, path = None): |
| 94 | print "Normal: "+path |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 95 | self.initSensorEntry(path) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 96 | self.sensor_cache[path]['threshold'] = "NORMAL" |
| 97 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 98 | def initSensorEntry(self,path): |
| 99 | if (self.sensor_cache.has_key(path) == False): |
| 100 | self.sensor_cache[path] = {} |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 101 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 102 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 103 | if __name__ == '__main__': |
| 104 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 105 | bus = dbus.SessionBus() |
| 106 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 107 | obj = SensorManager(bus,OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 108 | mainloop = gobject.MainLoop() |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 109 | |
| 110 | print "Running Sensor Manager" |
| 111 | mainloop.run() |
| 112 | |