AM nagios/plc2nagios.py
[monitor.git] / syncplcdb.py
1 #!/usr/bin/python
2
3 import plc
4 from config import config
5 import soltesz
6 import sys
7
8 config = config()
9
10 def dsites_from_lsites(l_sites):
11         d_sites = {}
12         id2lb = {}
13         for site in l_sites:
14                 if not site['login_base'] in d_sites:
15                         d_sites[site['login_base']] = site
16                         id2lb[site['site_id']] = site['login_base']
17                 else:
18                         #print "Two sites have the same login_base value %s!" % site['login_base']
19                         sys.exit(1)
20         return (d_sites, id2lb)
21
22 def dsn_from_dsln(d_sites, id2lb, l_nodes):
23         lb2hn = {}
24         dsn = {}
25         hn2lb = {}
26         for node in l_nodes:
27                 # this won't reach sites without nodes, which I guess isn't a problem.
28                 if node['site_id'] in id2lb.keys():
29                         login_base = id2lb[node['site_id']]
30                 else:
31                         print "%s has a foreign site_id %s" % (node['hostname'], 
32                                                                                                         node['site_id'])
33                         continue
34                         for i in id2lb:
35                                 print i, " ", id2lb[i]
36                         raise Exception, "Node has missing site id!! %s %d" %(node['hostname'], node['site_id'])
37                 if not login_base in dsn:
38                         lb2hn[login_base] = []
39                         dsn[login_base] = {}
40                         dsn[login_base]['plc'] = d_sites[login_base]
41                         dsn[login_base]['monitor'] = {} # event log, or something
42
43                 hostname = node['hostname']
44                 lb2hn[login_base].append(node)
45                 dsn[login_base][hostname] = {}
46                 dsn[login_base][hostname]['plc'] = node
47                 dsn[login_base][hostname]['comon'] = {}
48                 dsn[login_base][hostname]['monitor'] = {}
49
50                 hn2lb[hostname] = login_base
51         return (dsn, hn2lb, lb2hn)
52
53 def create_netid2ip(l_nodes, l_nodenetworks):
54         netid2ip = {}
55         for node in l_nodes:
56                 for netid in node['nodenetwork_ids']:
57                         found = False
58                         for nn in l_nodenetworks:
59                                 if nn['nodenetwork_id'] == netid:
60                                         found = True
61                                         netid2ip[netid] = nn['ip']
62                         if not found:
63                                 print "ERROR! %s" % node
64
65         return netid2ip
66
67 def create_plcdb():
68
69         # get sites, and stats
70         l_sites = plc.getSites({'peer_id':None}, ['login_base', 'site_id', 'abbreviated_name', 'latitude', 'longitude', 
71                                                                                           'max_slices', 'slice_ids', 'node_ids' ])
72         if len(l_sites) == 0:
73                 sys.exit(1)
74         (d_sites,id2lb) = dsites_from_lsites(l_sites)
75
76         # get nodes at each site, and 
77         l_nodes = plc.getNodes({'peer_id':None}, ['hostname', 'node_id', 'ports', 'site_id', 'version', 
78                                                   'last_updated', 'date_created', 'last_contact', 'pcu_ids', 'nodenetwork_ids'])
79
80         l_nodenetworks = plc.getNodeNetworks()
81         (plcdb, hn2lb, lb2hn) = dsn_from_dsln(d_sites, id2lb, l_nodes)
82         netid2ip = create_netid2ip(l_nodes, l_nodenetworks)
83
84         # save information for future.
85         id2lb = id2lb
86         hn2lb = hn2lb
87         db = plcdb
88
89         if ('cachenodes' in dir(config) and config.cachenodes) or \
90                 'cachenodes' not in dir(config):
91                 soltesz.dbDump("plcdb_hn2lb", hn2lb)
92                 soltesz.dbDump("plcdb_lb2hn", lb2hn)
93                 soltesz.dbDump("plcdb_netid2ip", netid2ip)
94                 soltesz.dbDump("l_plcnodenetworks", l_nodenetworks)
95                 soltesz.dbDump("l_plcnodes", l_nodes)
96                 soltesz.dbDump("l_plcsites", l_sites)
97         
98         return l_nodes
99         
100
101 if __name__ == '__main__':
102         create_plcdb()