many improvements.
[monitor.git] / monitor / wrapper / plccache.py
1 #!/usr/bin/python
2
3 import sys
4 from monitor.wrapper import plc
5 from monitor import database
6 from monitor import config
7
8 def dsites_from_lsites(l_sites):
9         d_sites = {}
10         id2lb = {}
11         for site in l_sites:
12                 if not site['login_base'] in d_sites:
13                         d_sites[site['login_base']] = site
14                         id2lb[site['site_id']] = site['login_base']
15                 else:
16                         #print "Two sites have the same login_base value %s!" % site['login_base']
17                         #sys.exit(1)
18                         continue
19         return (d_sites, id2lb)
20
21 def dsn_from_dsln(d_sites, id2lb, l_nodes):
22         lb2hn = {}
23         dsn = {}
24         hn2lb = {}
25         for id in id2lb:
26                 if id2lb[id] not in lb2hn:
27                         lb2hn[id2lb[id]] = []
28
29         for node in l_nodes:
30                 # this won't reach sites without nodes, which I guess isn't a problem.
31                 if node['site_id'] in id2lb.keys():
32                         login_base = id2lb[node['site_id']]
33                 else:
34                         print "%s has a foreign site_id %s" % (node['hostname'], 
35                                                                                                         node['site_id'])
36                         continue
37                         for i in id2lb:
38                                 print i, " ", id2lb[i]
39                         raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
40                 if not login_base in dsn:
41                         lb2hn[login_base] = []
42                         dsn[login_base] = {}
43                         dsn[login_base]['plc'] = d_sites[login_base]
44                         dsn[login_base]['monitor'] = {} # event log, or something
45
46                 hostname = node['hostname']
47                 lb2hn[login_base].append(node)
48                 dsn[login_base][hostname] = {}
49                 dsn[login_base][hostname]['plc'] = node
50                 dsn[login_base][hostname]['comon'] = {}
51                 dsn[login_base][hostname]['monitor'] = {}
52
53                 hn2lb[hostname] = login_base
54         return (dsn, hn2lb, lb2hn)
55
56 def create_netid2ip(l_nodes, l_nodenetworks):
57         netid2ip = {}
58         for node in l_nodes:
59                 for netid in node['nodenetwork_ids']:
60                         found = False
61                         for nn in l_nodenetworks:
62                                 if nn['nodenetwork_id'] == netid:
63                                         found = True
64                                         netid2ip[netid] = nn['ip']
65                         if not found:
66                                 print "ERROR! %s" % node
67
68         return netid2ip
69
70 l_sites = None
71 l_nodes = None
72 l_pcus = None
73 l_nodenetworks = None
74
75 plcdb_hn2lb = None
76 plcdb_lb2hn = None
77 plcdb_netid2ip = None
78 plcdb_id2lb = None
79
80 def init():
81         global l_sites
82         global l_nodes
83         global l_pcus
84         global l_nodenetworks
85         global plcdb_hn2lb
86         global plcdb_lb2hn
87         global plcdb_netid2ip
88         global plcdb_id2lb
89
90         api = plc.getCachedAuthAPI()
91         l_sites = api.GetSites({'peer_id':None}, 
92                                                         ['login_base', 'site_id', 'abbreviated_name', 'latitude', 
93                                                         'longitude', 'max_slices', 'slice_ids', 'node_ids', 'enabled' ])
94         l_nodes = api.GetNodes({'peer_id':None}, 
95                                                         ['hostname', 'node_id', 'ports', 'site_id', 'version', 'last_updated', 
96                                                          'date_created', 'last_contact', 'pcu_ids', 'nodenetwork_ids'])
97         l_pcus = api.GetPCUs()
98         l_nodenetworks = api.GetNodeNetworks()
99
100         (d_sites,id2lb) = dsites_from_lsites(l_sites)
101         (plcdb, hn2lb, lb2hn) = dsn_from_dsln(d_sites, id2lb, l_nodes)
102         netid2ip = create_netid2ip(l_nodes, l_nodenetworks)
103
104         plcdb_hn2lb = hn2lb
105         plcdb_lb2hn = lb2hn
106         plcdb_netid2ip = netid2ip
107         plcdb_id2lb = id2lb
108         
109         return l_nodes
110
111
112 def create_plcdb():
113
114         # get sites, and stats
115         l_sites = plc.getSites({'peer_id':None}, ['login_base', 'site_id', 'abbreviated_name', 'latitude', 'longitude', 
116                                                                                           'max_slices', 'slice_ids', 'node_ids' ])
117         if len(l_sites) == 0:
118                 print "no sites! exiting..."
119                 sys.exit(1)
120         (d_sites,id2lb) = dsites_from_lsites(l_sites)
121
122         # get nodes at each site, and 
123         l_nodes = plc.getNodes({'peer_id':None}, ['hostname', 'node_id', 'ports', 'site_id', 'version', 
124                                                   'last_updated', 'date_created', 'last_contact', 'pcu_ids', 'nodenetwork_ids'])
125
126         l_nodenetworks = plc.getNodeNetworks()
127         (plcdb, hn2lb, lb2hn) = dsn_from_dsln(d_sites, id2lb, l_nodes)
128         netid2ip = create_netid2ip(l_nodes, l_nodenetworks)
129
130         # save information for future.
131         id2lb = id2lb
132         hn2lb = hn2lb
133         db = plcdb
134
135         if ('cachenodes' in dir(config) and config.cachenodes) or \
136                 'cachenodes' not in dir(config):
137                 database.dbDump("plcdb_hn2lb", hn2lb)
138                 database.dbDump("plcdb_lb2hn", lb2hn)
139                 database.dbDump("plcdb_netid2ip", netid2ip)
140                 database.dbDump("l_plcnodenetworks", l_nodenetworks)
141                 database.dbDump("l_plcnodes", l_nodes)
142                 database.dbDump("l_plcsites", l_sites)
143         
144         return l_nodes
145
146 if __name__ == '__main__':
147         create_plcdb()
148 else:
149         print "calling plccache init()"
150         init()