6 from monitor.wrapper import plc, plccache
7 from monitor import database
10 from monitor.database.dborm import zab_session as session
11 from monitor.database.zabbixapi.model import confirm_ids, HostGroup
14 plcdb = plccache.l_sites # database.dbLoad("l_plcsites")
15 netid2ip = plccache.plcdb_netid2ip # database.dbLoad("plcdb_netid2ip")
16 lb2hn = plccache.plcdb_lb2hn # database.dbLoad("plcdb_lb2hn")
18 def get_site_iplist(loginbase):
19 node_list = lb2hn[loginbase]
21 # TODO: ip_list string cannot be longer than 255 characters.
22 # TODO: if it is, then we need to break up the discovery rule.
24 for node in node_list:
25 if len(node['interface_ids']) > 0:
26 ip = netid2ip[node['interface_ids'][0]]
27 if len(ip_list) > 0: ip_list += ","
32 def add_loginbase(loginbase):
34 techs = plc.getTechEmails(loginbase)
35 pis = plc.getPIEmails(loginbase)
36 iplist = get_site_iplist(loginbase)
38 os.system("""echo '%s' | tr ',' '\n' >> /usr/share/monitor/nodelist.txt""" % iplist )
40 print "zabbixsite.setup_site('%s', %s, %s, '%s')" % (loginbase,techs, pis, iplist)
41 zabbixsite.setup_site(loginbase, techs, pis, iplist)
43 if __name__=="__main__":
45 from monitor import parser as parsermodule
46 parser = parsermodule.getParser(['cacheset'])
47 parser.set_defaults( setupglobal=False, syncsite=True, site=None, sitelist=None, setupids=False)
48 parser.add_option("", "--setupids", action="store_true", dest="setupids",
49 help="Setup global IDs.")
50 parser.add_option("", "--setupglobal", action="store_true", dest="setupglobal",
51 help="Setup global settings.")
52 parser.add_option("", "--nosite", action="store_false", dest="syncsite",
53 help="Do not sync sites.")
54 parser.add_option("", "--site", dest="site",
55 help="Sync only given site name.")
56 parser.add_option("", "--sitelist", dest="sitelist",
57 help="Sync only given site names in the list.")
58 opts = parsermodule.parse_args(parser)
60 os.system("""echo '' > /usr/share/monitor/nodelist.txt""")
63 # Not sure why, but this doesn't work if we continue. so exit.
64 # This step only needs to be called once, but there is no harm in
65 # calling it multiple times.
71 zabbixsite.setup_global()
75 api = plc.getCachedAuthAPI()
76 query = {'peer_id' : None}
78 query.update({'login_base' : opts.site})
81 sites = api.GetSites(query, ['login_base'])
82 site_api_list = [ site['login_base'] for site in sites ]
83 for site in sites: # [:20]:
84 add_loginbase(site['login_base'])
88 # NOTE: for all sites in DB but not API, call zabbixsite.delete_site()
89 hg_list = filter(lambda x: '_hostgroup' in x.name, HostGroup.query.all() )
90 site_db_list = [ hg.name[:-len('_hostgroup')] for hg in hg_list ]
91 in_db_not_plc = set(site_db_list) - set(site_api_list)
92 for login_base in in_db_not_plc:
93 print "Deleting %s" % login_base
94 zabbixsite.delete_site(site['login_base'])