Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import cPickle |
| 10 | import json |
| 11 | |
| 12 | if (len(sys.argv) < 2): |
| 13 | print "Usage: inventory_items.py [system name]" |
| 14 | exit(1) |
| 15 | System = __import__(sys.argv[1]) |
| 16 | import Openbmc |
| 17 | |
| 18 | |
| 19 | INTF_NAME = 'org.openbmc.InventoryItem' |
| 20 | DBUS_NAME = 'org.openbmc.managers.Inventory' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 21 | ENUM_INTF = 'org.openbmc.Object.Enumerate' |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 22 | |
| 23 | FRUS = System.FRU_INSTANCES |
| 24 | FRU_PATH = System.FRU_PATH |
| 25 | |
| 26 | class Inventory(dbus.service.Object): |
| 27 | def __init__(self,bus,name): |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 28 | global FRU_PATH |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 29 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 30 | if not os.path.exists(FRU_PATH): |
| 31 | os.makedirs(FRU_PATH) |
| 32 | |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 33 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 34 | self.objects = [ ] |
| 35 | |
| 36 | def addItem(self,item): |
| 37 | self.objects.append(item) |
| 38 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 39 | @dbus.service.method(ENUM_INTF, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 40 | in_signature='', out_signature='a{sa{sv}}') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 41 | def enumerate(self): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 42 | tmp_obj = {} |
| 43 | for item in self.objects: |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 44 | tmp_obj[str(item.name)]=item.GetAll(INTF_NAME) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 45 | return tmp_obj |
| 46 | |
| 47 | |
| 48 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 49 | class InventoryItem(Openbmc.DbusProperties): |
| 50 | def __init__(self,bus,name): |
| 51 | Openbmc.DbusProperties.__init__(self) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 52 | dbus.service.Object.__init__(self,bus,name) |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 53 | self.name = name |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 54 | self.cache = True |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 55 | self.Set(INTF_NAME,'is_fru',False) |
| 56 | self.Set(INTF_NAME,'fru_type',0) |
| 57 | self.Set(INTF_NAME,'present',"INACTIVE") |
| 58 | self.Set(INTF_NAME,'fault',"NONE") |
| 59 | |
| 60 | |
| 61 | |
| 62 | @dbus.service.method(INTF_NAME, |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 63 | in_signature='a{sv}', out_signature='') |
| 64 | def update(self,data): |
| 65 | ## translate dbus data into basic data types |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 66 | self.SetAll(INTF_NAME,data) |
| 67 | #self.saveToCache() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 68 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 69 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 70 | in_signature='s', out_signature='') |
| 71 | def setPresent(self,present): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 72 | self.setField('present',present) |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 73 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 74 | @dbus.service.method(INTF_NAME, |
Norman James | 96da5c2 | 2015-10-15 10:17:06 -0500 | [diff] [blame] | 75 | in_signature='s', out_signature='') |
| 76 | def setFault(self,fault): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 77 | self.setField('fault',fault) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 79 | def setField(self,field,value): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 80 | f = str(field) |
| 81 | d = Openbmc.DbusVariable(f,value) |
| 82 | self.Set(INTF_NAME,f,d.getBaseValue()) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 83 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 84 | def isCached(self): |
| 85 | return self.cache |
| 86 | |
| 87 | def getCacheFilename(self): |
| 88 | global FRU_PATH |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 89 | name = self.name.replace('/','.') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 90 | filename = FRU_PATH+name[1:]+".fru" |
| 91 | return filename |
| 92 | |
| 93 | def saveToCache(self): |
| 94 | if (self.isCached() == False): |
| 95 | return |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 96 | print "Caching: "+self.name |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 97 | try: |
| 98 | output = open(self.getCacheFilename(), 'wb') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 99 | ## save properties |
| 100 | cPickle.dump(self.properties[INTF_NAME],output) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 101 | except Exception as e: |
| 102 | print "ERROR: "+str(e) |
| 103 | finally: |
| 104 | output.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 105 | |
| 106 | def loadFromCache(self): |
| 107 | if (self.isCached() == False): |
| 108 | return; |
| 109 | ## overlay with pickled data |
| 110 | filename=self.getCacheFilename() |
| 111 | if (os.path.isfile(filename)): |
| 112 | print "Loading from cache: "+filename |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 113 | try: |
| 114 | p = open(filename, 'rb') |
| 115 | data2 = cPickle.load(p) |
| 116 | for k in data2.keys(): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 117 | self.setField(k,data2[k]) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 118 | except Exception as e: |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 119 | print "No cache file found: " +str(e) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 120 | finally: |
| 121 | p.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 126 | bus = Openbmc.getDBus() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 127 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 128 | mainloop = gobject.MainLoop() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 129 | obj_parent = Inventory(bus, '/org/openbmc/inventory') |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 130 | |
| 131 | for f in FRUS.keys(): |
| 132 | obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT) |
| 133 | obj = InventoryItem(bus,obj_path) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 134 | obj.setField('is_fru',FRUS[f]['is_fru']) |
| 135 | obj.setField('fru_type',FRUS[f]['fru_type']) |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 136 | #obj.loadFromCache(); |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 137 | obj_parent.addItem(obj) |
| 138 | |
| 139 | print "Running Inventory Manager" |
| 140 | mainloop.run() |
| 141 | |