clearer names for actions, and infer actions better
[monitor.git] / zabbix / zabbixsync.py
index 1c0e405..340b4d8 100755 (executable)
@@ -1,16 +1,21 @@
 #!/usr/bin/python
 
 import sys
+import os
 import site
+from monitor.config import config
+config.zabbix_enabled = True
+
 from monitor.wrapper import plc, plccache
 from monitor import database
 
 import zabbixsite
 from monitor.database.dborm import zab_session as session
+from monitor.database.zabbixapi.model import confirm_ids, HostGroup
 
 
 plcdb = plccache.l_sites # database.dbLoad("l_plcsites")
-netid2ip = plccache.plcdb_netid2ip # database.dbLoad("plcdb_netid2ip")
+#netid2ip = plccache.plcdb_netid2ip # database.dbLoad("plcdb_netid2ip")
 lb2hn = plccache.plcdb_lb2hn # database.dbLoad("plcdb_lb2hn")
 
 def get_site_iplist(loginbase):
@@ -20,9 +25,13 @@ def get_site_iplist(loginbase):
        # TODO: if it is, then we need to break up the discovery rule.
        ip_list = ""
        for node in node_list:
-               ip = netid2ip[node['nodenetwork_ids'][0]]
-               if len(ip_list) > 0: ip_list += ","
-               ip_list += ip
+               if len(node['interface_ids']) > 0:
+                       ifs = plc.api.GetInterfaces({'interface_id' : node['interface_ids'], 'is_primary' : True})
+                       print ifs
+                       #ip = netid2ip[node['interface_ids'][0]]
+                       ip = ifs[0]['ip']
+                       if len(ip_list) > 0: ip_list += ","
+                       ip_list += ip
 
        return ip_list
        
@@ -32,6 +41,8 @@ def add_loginbase(loginbase):
        pis = plc.getPIEmails(loginbase)
        iplist = get_site_iplist(loginbase)
 
+       os.system("""echo '%s' | tr ',' '\n' >> /usr/share/monitor/nodelist.txt""" % iplist )
+
        print "zabbixsite.setup_site('%s', %s, %s, '%s')" % (loginbase,techs, pis, iplist)
        zabbixsite.setup_site(loginbase, techs, pis, iplist)
 
@@ -39,15 +50,29 @@ if __name__=="__main__":
 
        from monitor import parser as parsermodule
        parser = parsermodule.getParser(['cacheset'])
-       parser.set_defaults( setupglobal=False, syncsite=True, site=None)
+       parser.set_defaults( setupglobal=False, syncsite=True, site=None, sitelist=None, setupids=False)
+       parser.add_option("", "--setupids", action="store_true", dest="setupids",
+                                               help="Setup global IDs.")
        parser.add_option("", "--setupglobal", action="store_true", dest="setupglobal",
                                                help="Setup global settings.")
        parser.add_option("", "--nosite", action="store_false", dest="syncsite",
                                                help="Do not sync sites.")
        parser.add_option("", "--site", dest="site",
                                                help="Sync only given site name.")
+       parser.add_option("", "--sitelist", dest="sitelist",
+                                               help="Sync only given site names in the list.")
        opts = parsermodule.parse_args(parser)
 
+       os.system("""echo '' > /usr/share/monitor/nodelist.txt""")
+
+       if opts.setupids:
+               # Not sure why, but this doesn't work if we continue.  so exit.
+               # This step only needs to be called once, but there is no harm in
+               # calling it multiple times.
+               confirm_ids()
+               session.flush()
+               sys.exit(0)
+
        if opts.setupglobal:
                zabbixsite.setup_global()
                session.flush()
@@ -57,11 +82,25 @@ if __name__=="__main__":
                query = {'peer_id' : None}
                if opts.site:
                        query.update({'login_base' : opts.site})
+               elif opts.sitelist:
+                       l = opts.sitelist.split(",")
+                       query.update({'login_base' : l})
 
+               # ADD SITES
                sites = api.GetSites(query, ['login_base'])
-               for site in sites:
+               site_api_list = [ site['login_base'] for site in sites ]
+               for site in sites: # [:20]:
                        add_loginbase(site['login_base'])
                        session.flush()
 
-       # TODO: for any removed site that is in the db, call zabbixsite.delete_site()
+               if not opts.site:
+                       # NOTE: for all sites in DB but not API, call zabbixsite.delete_site()
+                       hg_list = filter(lambda x: '_hostgroup' in x.name, HostGroup.query.all() )
+                       site_db_list = [ hg.name[:-len('_hostgroup')] for hg in hg_list ]
+                       in_db_not_plc = set(site_db_list) - set(site_api_list)
+                       for login_base in in_db_not_plc:
+                               print "Deleting %s" % login_base
+                               zabbixsite.delete_site(login_base)
+                               session.flush()
+