4 from monitor.wrapper import plc
5 from monitor.generic import *
6 from monitor.database.info.model import *
7 from monitor import database
21 def _param_to_str(self, name, *params):
24 retstr += "%s-" % name
29 def __getattr__(self, name):
30 method = getattr(self.api, name)
32 raise AssertionError("method does not exist")
34 def run_or_returncached(*params):
35 cachename = self._param_to_str(name, *params)
36 #print "cachename is %s" % cachename
37 if hasattr(config, 'refresh'):
38 refresh = config.refresh
43 if not database.cachedRecently(cachename):
44 load_old_cache = False
46 values = method(self.auth, *params)
48 print "Call %s FAILED: Using old cached data" % cachename
52 values = database.dbLoad(cachename)
54 database.dbDump(cachename, values)
58 values = database.dbLoad(cachename)
61 return method(self.auth, *params)
63 return run_or_returncached
65 cacheapi = CachedPLC(plc.auth.auth, plc.auth.server)
69 #print "IMPORTING PLCCACHE: ",
70 #traceback.print_stack()
77 print >>sys.stderr, "initing plccache"
79 print >>sys.stderr, "collecting plcsites"
80 dbsites = PlcSite.query.all()
81 l_sites = [ s.plc_site_stats for s in dbsites ]
83 print >>sys.stderr, "collecting plcnodes"
84 dbnodes = PlcNode.query.all()
85 l_nodes = [ s.plc_node_stats for s in dbnodes ]
87 print >>sys.stderr, "collecting plcpcus"
88 dbpcus = PlcPCU2.query.all()
92 for k in ['username', 'protocol', 'node_ids', 'ip',
93 'pcu_id', 'hostname', 'site_id', 'notes',
94 'model', 'password', 'ports']:
95 pcu[k] = getattr(s, k)
98 print >>sys.stderr, "building id2lb"
99 (d_sites,id2lb) = dsites_from_lsites_id(l_sites)
100 print >>sys.stderr, "building lb2hn"
101 (plcdb, hn2lb, lb2hn) = dsn_from_dsln(d_sites, id2lb, l_nodes)
109 def GetNodesByIds(ids):
112 node = PlcNode.get_by(node_id=node_id)
113 ret.append(node.plc_node_stats)
116 def GetNodesBySite(loginbase):
117 site = PlcSite.get_by(loginbase=loginbase)
118 return GetNodesByIds(site.plc_site_stats['node_ids'])
120 def GetNodeByName(hostname):
121 print "GetNodeByName %s" % hostname
122 node = PlcNode.get_by(hostname=hostname)
123 return node.plc_node_stats
125 def GetSitesByName(sitelist):
127 for site in sitelist:
128 site = PlcSite.get_by(loginbase=site)
129 ret.append(site.plc_site_stats)
132 def GetSitesById(idlist):
134 for site_id in idlist:
135 site = PlcSite.get_by(site_id=site_id)
136 ret.append(site.plc_site_stats)
139 def deleteExtra(l_plc, objectClass=PlcSite, dbKey='loginbase', plcKey='login_base'):
140 dbobjs = objectClass.query.all()
141 dbobj_key = [ getattr(s, dbKey) for s in dbobjs ]
142 plcobj_key = [ s[plcKey] for s in l_plc ]
143 extra_key = set(dbobj_key) - set(plcobj_key)
144 for obj in extra_key:
145 print >>sys.stderr, "deleting %s" % obj
146 dbobj = objectClass.get_by(**{dbKey : obj})
150 l_sites = plc.api.GetSites({'peer_id':None},
151 ['login_base', 'site_id', 'abbreviated_name', 'latitude',
152 'longitude', 'max_slices', 'slice_ids', 'node_ids',
153 'enabled', 'date_created' ])
154 l_nodes = plc.api.GetNodes({'peer_id':None},
155 ['hostname', 'node_id', 'ports', 'site_id', 'boot_state', 'run_level',
156 'version', 'last_updated', 'date_created', 'key',
157 'last_contact', 'pcu_ids', 'interface_ids'])
158 l_pcus = plc.api.GetPCUs()
160 print >>sys.stderr, "sync sites"
162 dbsite = PlcSite.findby_or_create(site_id=site['site_id'])
163 dbsite.loginbase = site['login_base']
164 dbsite.date_checked = datetime.now()
165 dbsite.plc_site_stats = site
166 deleteExtra(l_sites, PlcSite, 'loginbase', 'login_base')
167 deleteExtra(l_sites, HistorySiteRecord, 'loginbase', 'login_base')
170 print >>sys.stderr, "sync pcus"
172 dbpcu = PlcPCU2.findby_or_create(pcu_id=pcu['pcu_id'])
173 dbpcu.date_checked = datetime.now()
174 for key in pcu.keys():
175 print >>sys.stderr, "setting %s = %s" % (key, pcu[key])
176 setattr(dbpcu, key, pcu[key])
178 deleteExtra(l_pcus, PlcPCU2, 'pcu_id', 'pcu_id')
179 deleteExtra(l_pcus, HistoryPCURecord, 'plc_pcuid', 'pcu_id')
180 deleteExtra(l_pcus, FindbadPCURecord, 'plc_pcuid', 'pcu_id')
183 print >>sys.stderr, "sync nodes"
185 dbnode = PlcNode.findby_or_create(node_id=node['node_id'])
186 dbnode.hostname = node['hostname']
187 dbnode.date_checked = datetime.now()
188 dbnode.plc_node_stats = node
189 deleteExtra(l_nodes, PlcNode, 'node_id', 'node_id')
190 deleteExtra(l_nodes, HistoryNodeRecord, 'plc_nodeid', 'node_id')
191 deleteExtra(l_nodes, PlcNode, 'hostname', 'hostname')
192 deleteExtra(l_nodes, HistoryNodeRecord, 'hostname', 'hostname')
193 deleteExtra(l_nodes, FindbadNodeRecord, 'hostname', 'hostname')
200 if __name__ == '__main__':