add a warning when given loginbase returns nothing
[monitor.git] / monitor / generic.py
1 import sys
2
3 def d_from_l(l, key, using=None, key_as=str, using_as=None):
4         d = {}
5         for obj in l:
6                 if not str(obj[key]) in d:
7                         if using is None:
8                                 d[key_as(obj[key])] = obj
9                         else:
10                                 if using_as is None:
11                                         d[key_as(obj[key])] = obj[using]
12                                 else:
13                                         d[key_as(obj[key])] = using_as(obj[using])
14                 else:
15                         print "Two objects have the same %s key %s!" % (key, obj[key])
16                         continue
17         return d
18
19 def dpcus_from_lpcus(l_pcus):
20         d_pcus = d_from_l(l_pcus, 'pcu_id')
21         return d_pcus
22
23 def dnodes_from_lnodes(l_nodes):
24         d_nodes = d_from_l(l_nodes, 'hostname')
25         return d_nodes
26
27 def dsites_from_lsites(l_sites):
28         d_sites = d_from_l(l_sites, 'login_base')
29         return d_sites 
30
31 def dsites_from_lsites_id(l_sites):
32         d_sites = d_from_l(l_sites, 'login_base')
33         id2lb = d_from_l(l_sites, 'site_id', 'login_base', int, str)
34         return (d_sites, id2lb)
35
36 def dsn_from_dsln(d_sites, id2lb, l_nodes):
37         lb2hn = {}
38         dsn = {}
39         hn2lb = {}
40         for id in id2lb:
41                 if id2lb[id] not in lb2hn:
42                         lb2hn[id2lb[id]] = []
43
44         for node in l_nodes:
45                 # this won't reach sites without nodes, which I guess isn't a problem.
46                 if node['site_id'] in id2lb.keys():
47                         login_base = id2lb[node['site_id']]
48                 else:
49                         print >>sys.stderr, "%s has a foreign site_id %s" % (node['hostname'], node['site_id'])
50                         continue
51                         for i in id2lb:
52                                 print i, " ", id2lb[i]
53                         raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
54                 if not login_base in dsn:
55                         lb2hn[login_base] = []
56                         dsn[login_base] = {}
57                         dsn[login_base]['plc'] = d_sites[login_base]
58                         dsn[login_base]['monitor'] = {} # event log, or something
59
60                 hostname = node['hostname']
61                 lb2hn[login_base].append(node)
62                 dsn[login_base][hostname] = {}
63                 dsn[login_base][hostname]['plc'] = node
64                 dsn[login_base][hostname]['comon'] = {}
65                 dsn[login_base][hostname]['monitor'] = {}
66
67                 hn2lb[hostname] = login_base
68         return (dsn, hn2lb, lb2hn)