4 from monitor.wrapper import plc
5 from monitor.generic import *
6 from monitor.database.info.model import *
7 from monitor import database
8 from monitor import config
19 class CachedPLC(plc.PLC):
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, exclude) = dsn_from_dsln(d_sites, id2lb, l_nodes)
107 l_nodes = filter(lambda x: x['hostname'] not in exclude, l_nodes)
111 def GetNodesByIds(ids):
114 node = PlcNode.get_by(node_id=node_id)
115 ret.append(node.plc_node_stats)
118 def GetNodesBySite(loginbase):
119 site = PlcSite.get_by(loginbase=loginbase)
120 return GetNodesByIds(site.plc_site_stats['node_ids'])
122 def GetNodeByName(hostname):
123 print "GetNodeByName %s" % hostname
124 node = PlcNode.get_by(hostname=hostname)
125 return node.plc_node_stats
127 def GetSitesByName(sitelist):
129 for site in sitelist:
130 site = PlcSite.get_by(loginbase=site)
131 ret.append(site.plc_site_stats)
134 def GetSitesById(idlist):
136 for site_id in idlist:
137 site = PlcSite.get_by(site_id=site_id)
138 ret.append(site.plc_site_stats)
141 def deleteExtra(l_plc, objectClass=PlcSite, dbKey='loginbase', plcKey='login_base'):
142 dbobjs = objectClass.query.all()
143 dbobj_key = [ getattr(s, dbKey) for s in dbobjs ]
144 plcobj_key = [ s[plcKey] for s in l_plc ]
145 extra_key = set(dbobj_key) - set(plcobj_key)
146 for obj in extra_key:
147 print >>sys.stderr, "deleting %s" % obj
148 dbobj = objectClass.get_by(**{dbKey : obj})
152 # strip non-ascii characters to prvent errors
154 if type(s) in (str,unicode):
155 r = "".join([x for x in s if ord(x) < 128])
159 l_sites = plc.api.GetSites({'peer_id':None},
160 ['login_base', 'site_id', 'abbreviated_name', 'latitude',
161 'longitude', 'max_slices', 'slice_ids', 'node_ids',
162 'enabled', 'date_created' ])
163 l_nodes = plc.api.GetNodes({'peer_id':None},
164 ['hostname', 'node_id', 'ports', 'site_id', 'boot_state', 'run_level',
165 'version', 'last_updated', 'date_created', 'key',
166 'last_contact', 'pcu_ids', 'interface_ids',
167 'last_boot', 'last_download', 'last_pcu_reboot', 'last_pcu_confirmation'])
168 l_pcus = plc.api.GetPCUs()
170 print >>sys.stderr, "sync sites"
172 dbsite = PlcSite.findby_or_create(site_id=site['site_id'])
173 dbsite.loginbase = site['login_base']
174 dbsite.date_checked = datetime.now()
175 dbsite.plc_site_stats = site
176 deleteExtra(l_sites, PlcSite, 'loginbase', 'login_base')
177 deleteExtra(l_sites, HistorySiteRecord, 'loginbase', 'login_base')
180 print >>sys.stderr, "sync pcus"
182 dbpcu = PlcPCU2.findby_or_create(pcu_id=pcu['pcu_id'])
183 dbpcu.date_checked = datetime.now()
184 for key in pcu.keys():
185 print >>sys.stderr, "setting %s = %s" % (key, conv(pcu[key]))
186 setattr(dbpcu, key, conv(pcu[key]))
188 deleteExtra(l_pcus, PlcPCU2, 'pcu_id', 'pcu_id')
189 deleteExtra(l_pcus, HistoryPCURecord, 'plc_pcuid', 'pcu_id')
190 deleteExtra(l_pcus, FindbadPCURecord, 'plc_pcuid', 'pcu_id')
193 print >>sys.stderr, "sync nodes"
195 dbnode = PlcNode.findby_or_create(node_id=node['node_id'])
196 dbnode.hostname = node['hostname']
197 dbnode.date_checked = datetime.now()
198 dbnode.plc_node_stats = node
199 deleteExtra(l_nodes, PlcNode, 'node_id', 'node_id')
200 deleteExtra(l_nodes, HistoryNodeRecord, 'plc_nodeid', 'node_id')
201 deleteExtra(l_nodes, PlcNode, 'hostname', 'hostname')
202 deleteExtra(l_nodes, HistoryNodeRecord, 'hostname', 'hostname')
203 deleteExtra(l_nodes, FindbadNodeRecord, 'hostname', 'hostname')
210 if __name__ == '__main__':