| Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python | 
|  | 2 |  | 
|  | 3 |  | 
|  | 4 | import gobject | 
|  | 5 | import dbus | 
|  | 6 | import dbus.service | 
|  | 7 | import dbus.mainloop.glib | 
|  | 8 | import xml.etree.ElementTree as ET | 
|  | 9 | import System | 
|  | 10 |  | 
|  | 11 | DBUS_NAME = 'org.openbmc.managers.Sensors' | 
|  | 12 | OBJ_NAME = '/org/openbmc/managers/Sensors' | 
|  | 13 |  | 
|  | 14 | NORMAL   = 0 | 
|  | 15 | LOWER_WARNING  = 1 | 
|  | 16 | LOWER_CRITICAL = 2 | 
|  | 17 | UPPER_WARNING  = 3 | 
|  | 18 | UPPER_CRITICAL = 4 | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | sensor_config = System.BarreleyeSensors() | 
|  | 22 |  | 
|  | 23 | ## finds objects held by Dbus ObjectManager | 
|  | 24 | def get_interface(bus_name): | 
|  | 25 | obj_name = "/"+bus_name.replace('.','/') | 
|  | 26 | obj = bus.get_object(bus_name, obj_name) | 
|  | 27 |  | 
|  | 28 | #Find object in object manager and retrieve interface name | 
|  | 29 | manager = dbus.Interface(obj,'org.freedesktop.DBus.ObjectManager') | 
|  | 30 | objects = manager.GetManagedObjects() | 
|  | 31 | obj_path = None | 
|  | 32 | interface = None | 
|  | 33 |  | 
|  | 34 | for o in objects: | 
|  | 35 | for intf in objects[o].keys(): | 
|  | 36 | if (intf.find('Sensor') > 0): | 
|  | 37 | interface = intf | 
|  | 38 | obj_path = o | 
|  | 39 |  | 
|  | 40 | if (interface == None): | 
|  | 41 | raise Exception("Unable to find sensor: "+obj_path) | 
|  | 42 |  | 
|  | 43 | return [obj_path, interface] | 
|  | 44 |  | 
|  | 45 |  | 
|  | 46 | class SensorManagement(dbus.service.Object): | 
|  | 47 | def __init__(self,bus,name): | 
|  | 48 | dbus.service.Object.__init__(self,bus,name) | 
|  | 49 | self.sensor_cache = {} | 
|  | 50 | for bus_name in bus.list_names(): | 
|  | 51 | if (bus_name.find('org.openbmc.sensors')==0): | 
|  | 52 | self.request_name(bus_name,"",bus_name) | 
|  | 53 |  | 
|  | 54 | bus.add_signal_receiver(self.request_name, | 
|  | 55 | dbus_interface = 'org.freedesktop.DBus', | 
|  | 56 | signal_name = "NameOwnerChanged") | 
|  | 57 |  | 
|  | 58 |  | 
|  | 59 | def request_name(self, bus_name, a, b): | 
|  | 60 | if (len(b) > 0 and bus_name.find('org.openbmc.sensors') == 0): | 
|  | 61 | if (sensor_config.has_key(bus_name) == True): | 
|  | 62 | try: | 
|  | 63 | print "Loading: "+bus_name | 
|  | 64 | obj_info = get_interface(bus_name) | 
|  | 65 | obj = bus.get_object(bus_name,obj_info[0]) | 
|  | 66 | intf = dbus.Interface(obj,obj_info[1]) | 
|  | 67 | intf.setThresholds(sensor_config[bus_name]['lower_critical'], | 
|  | 68 | sensor_config[bus_name]['lower_warning'], | 
|  | 69 | sensor_config[bus_name]['upper_warning'], | 
|  | 70 | sensor_config[bus_name]['upper_critical']) | 
|  | 71 | if (sensor_config[bus_name].has_key('parameters')): | 
|  | 72 | intf.setConfigData(sensor_config[bus_name]['parameters']) | 
|  | 73 |  | 
|  | 74 | except dbus.exceptions.DBusException, e: | 
|  | 75 | # TODO: not sure what to do if can't find other services | 
|  | 76 | print "Unable to find dependent services: ",e | 
|  | 77 | else: | 
|  | 78 | print "Sensor found on bus but no config: "+bus_name | 
|  | 79 | if (len(b) == 0  and bus_name.find('org.openbmc') ==0): | 
|  | 80 | print "Sensor stopped: "+bus_name | 
|  | 81 |  | 
|  | 82 | @dbus.service.method(DBUS_NAME, | 
|  | 83 | in_signature='s', out_signature='a{ss}') | 
|  | 84 | def getAllSensors(self,obj_name): | 
|  | 85 | return None | 
|  | 86 |  | 
|  | 87 | @dbus.service.method(DBUS_NAME, | 
|  | 88 | in_signature='s', out_signature='i') | 
|  | 89 | def getSensorValue(self,obj_name): | 
|  | 90 | sensor = self.sensor_cache[obj_name] | 
|  | 91 | return sensor.get_value() | 
|  | 92 |  | 
|  | 93 | @dbus.service.signal(DBUS_NAME) | 
|  | 94 | def CriticalThreshold(self, obj): | 
|  | 95 | print "Critical: "+obj | 
|  | 96 |  | 
|  | 97 | @dbus.service.signal(DBUS_NAME) | 
|  | 98 | def WarningThreshold(self, obj): | 
|  | 99 | print "Warning: "+obj | 
|  | 100 |  | 
|  | 101 |  | 
|  | 102 | if __name__ == '__main__': | 
|  | 103 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | 
|  | 104 | bus = dbus.SessionBus() | 
|  | 105 | name = dbus.service.BusName(DBUS_NAME,bus) | 
|  | 106 | obj = SensorManagement(bus,OBJ_NAME) | 
|  | 107 | mainloop = gobject.MainLoop() | 
|  | 108 |  | 
|  | 109 | print "Running SensorManagerService" | 
|  | 110 | mainloop.run() | 
|  | 111 |  |