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