make plc.py simpler to reduce the dependencies for plc_hosts_to_nagios.
[monitor.git] / monitor / wrapper / plccache.py
1 #!/usr/bin/python
2
3 import sys
4 from monitor.wrapper import plc
5 from monitor.generic import *
6 from monitor.database.info.model import *
7 from monitor import database
8 import profile
9
10
11 l_sites = None
12 l_nodes = None
13 l_pcus = None
14
15 plcdb_hn2lb = None
16 plcdb_lb2hn = None
17 plcdb_id2lb = None
18
19 class CachedPLC(PLC):
20
21         def _param_to_str(self, name, *params):
22                 fields = len(params)
23                 retstr = ""
24                 retstr += "%s-" % name
25                 for x in params:
26                         retstr += "%s-" % x
27                 return retstr[:-1]
28
29         def __getattr__(self, name):
30                 method = getattr(self.api, name)
31                 if method is None:
32                         raise AssertionError("method does not exist")
33
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
39                         else:
40                                 refresh = False
41
42                         if 'Get' in name:
43                                 if not database.cachedRecently(cachename):
44                                         load_old_cache = False
45                                         try:
46                                                 values = method(self.auth, *params)
47                                         except:
48                                                 print "Call %s FAILED: Using old cached data" % cachename
49                                                 load_old_cache = True
50
51                                         if load_old_cache:
52                                                 values = database.dbLoad(cachename)
53                                         else:
54                                                 database.dbDump(cachename, values)
55
56                                         return values
57                                 else:
58                                         values = database.dbLoad(cachename)
59                                         return values
60                         else:
61                                 return method(self.auth, *params)
62
63                 return run_or_returncached
64
65 cacheapi = CachedPLC(plc.auth.auth, plc.auth.server)
66
67 def init():
68         import traceback
69         #print "IMPORTING PLCCACHE: ",
70         #traceback.print_stack()
71         global l_sites
72         global l_nodes
73         global l_pcus
74         global plcdb_hn2lb
75         global plcdb_lb2hn
76         global plcdb_id2lb
77         print >>sys.stderr, "initing plccache"
78
79         print >>sys.stderr, "collecting plcsites"
80         dbsites = PlcSite.query.all()
81         l_sites = [ s.plc_site_stats for s in dbsites ]
82
83         print >>sys.stderr, "collecting plcnodes"
84         dbnodes = PlcNode.query.all()
85         l_nodes = [ s.plc_node_stats for s in dbnodes ]
86
87         print >>sys.stderr, "collecting plcpcus"
88         dbpcus = PlcPCU2.query.all()
89         l_pcus = []
90         for s in dbpcus:
91                 pcu = {}
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)
96                 l_pcus.append(pcu)
97
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)
102
103         plcdb_hn2lb = hn2lb
104         plcdb_lb2hn = lb2hn
105         plcdb_id2lb = id2lb
106         
107         return
108
109 def GetNodesByIds(ids):
110         ret = []
111         for node_id in ids:
112                 node = PlcNode.get_by(node_id=node_id)
113                 ret.append(node.plc_node_stats)
114         return ret
115
116 def GetNodesBySite(loginbase):
117         site = PlcSite.get_by(loginbase=loginbase)
118         return GetNodesByIds(site.plc_site_stats['node_ids'])
119
120 def GetNodeByName(hostname):
121         print "GetNodeByName %s" % hostname
122         node = PlcNode.get_by(hostname=hostname)
123         return node.plc_node_stats
124
125 def GetSitesByName(sitelist):
126         ret = []
127         for site in sitelist:
128                 site = PlcSite.get_by(loginbase=site)
129                 ret.append(site.plc_site_stats)
130         return ret
131
132 def GetSitesById(idlist):
133         ret = []
134         for site_id in idlist:
135                 site = PlcSite.get_by(site_id=site_id)
136                 ret.append(site.plc_site_stats)
137         return ret
138
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})
147                 dbobj.delete()
148
149 def sync():
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()
159
160         print >>sys.stderr, "sync sites"
161         for site in l_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')
168         session.flush()
169
170         print >>sys.stderr, "sync pcus"
171         for pcu in l_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])
177
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')
181         session.flush()
182
183         print >>sys.stderr, "sync nodes"
184         for node in l_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')
194         session.flush()
195
196         init()
197
198         return
199
200 if __name__ == '__main__':
201         sync()
202 else:
203         init()