def get_default_server(cfg, realm, site_id): """ [description] retrieves and returns the default server, user, and pass for zabbix in whatever the realm.site_id is its given [parameter info] required: cfg: the config object. useful everywhere realm: realm to return server data for site_id: site_id to return server data for [return value] returns zs_unqdn, zs_user, zs_pass as a list """ serv = cfg.dbsess.query(Server).\ filter(Server.tag=="zabbix_server").\ filter(Server.realm==realm).\ filter(Server.site_id==site_id).\ first() zs_unqdn = '.'.join([serv.hostname,realm,site_id]) discard,zs_user = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_user")).split('=') discard,zs_pass = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_pass")).split('=') retval = [zs_unqdn, zs_user, zs_pass] return retval
def get_default_server(cfg, realm, site_id): """ [description] retrieves and returns the default server, user, and pass for zabbix in whatever the realm.site_id is its given [parameter info] required: cfg: the config object. useful everywhere realm: realm to return server data for site_id: site_id to return server data for [return value] returns zs_unqdn, zs_user, zs_pass as a list """ serv = cfg.dbsess.query(Server).\ filter(Server.tag=="zabbix_server").\ filter(Server.realm==realm).\ filter(Server.site_id==site_id).\ first() zs_unqdn = '.'.join([serv.hostname, realm, site_id]) discard, zs_user = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_user")).split('=') discard, zs_pass = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_pass")).split('=') retval = [zs_unqdn, zs_user, zs_pass] return retval
def status(cfg, host, realm, site_id): """ [description] get status for a server's port on the management switch [parameter info] required: cfg: the config object. useful everywhere host: the server whose port we're checking realm: the realm the server lives in site_id: the site_id the server lives in [return value] no explicit return """ # get management vlan web control panel credentials discard,user = str(kv_select(cfg, '', key="mgmt_vlan_web_control_user")).split('=') discard,password = str(kv_select(cfg, '', key="mgmt_vlan_web_control_password")).split('=') # gather server info s, n, h = mothership.snh(cfg, host, realm, site_id) for iface in n: if iface.interface == cfg.mgmt_vlan_interface: switch_port = iface.switch_port else: pass # decide what method to use, based on /etc/mothership.yaml if cfg.mgmt_vlan_style == 'snmp': #need to write some method for snmp control print "snmp management vlan control has not yet been implemented" elif cfg.mgmt_vlan_style == 'curl': # build our command line cmdln = "curl" upass = user + ":" + password cmdurl = cfg.mgmt_vlan_status_url # fire off a curl to hit the web control panel and enable the port status = subprocess.Popen([cmdln, '-k', '-u', upass, cmdurl], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] # a bit of sanity if status: statuses = status.split('\n') else: print "Error, status check did not return data! Maybe something is wrong with your switch(es)?" return # search for our port for i in statuses: try: if i.index(switch_port): print i break except ValueError: pass else: print "An odd failure occurred in the mgmt_vlan section. please check your config"
def disable(cfg, unqdn, zs_unqdn): """ [description] disable a server within zabbix [parameter info] required: cfg: the config object. useful everywhere unqdn: unqualified domain name of the server to disable zs_unqdn: unqdn of the zabbix server [return value] no explicit return """ # just making sure something is assigned to it... hstatus = None # check to see if zabbix is enabled in the config if cfg.zab_active == False: print "Zabbix is not active, skipping. Zabbix can be enabled in the mothership config file." return else: pass # stitch together some info about our host host, realm, site_id = mothership.split_fqdn(unqdn) if zs_unqdn == None: zs_unqdn, zs_user, zs_pass = get_default_server(cfg, realm, site_id) else: zs_user = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_user")).split('=')[1] zs_pass = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_pass")).split('=')[1] zs_host, zs_realm, zs_site_id = mothership.split_fqdn(zs_unqdn) zs_fqdn = '.'.join([zs_unqdn, cfg.domain]) # set the zabbix host and url path, log in try: # uncomment to debug #zapi = ZabbixAPI(zs_fqdn,'/', debug_level=6) zapi = ZabbixAPI(zs_fqdn, '/') zapi.login(zs_user, zs_pass) # uncomment to debug #print "Zabbix API Version: %s" % zapi.api_version() #print "Logged in: %s" % str(zapi.test_login()) except ZabbixAPIException, e: sys.stderr.write(str(e) + '\n')
def disable(cfg, unqdn, zs_unqdn): """ [description] disable a server within zabbix [parameter info] required: cfg: the config object. useful everywhere unqdn: unqualified domain name of the server to disable zs_unqdn: unqdn of the zabbix server [return value] no explicit return """ # just making sure something is assigned to it... hstatus = None # check to see if zabbix is enabled in the config if cfg.zab_active == False: print "Zabbix is not active, skipping. Zabbix can be enabled in the mothership config file." return else: pass # stitch together some info about our host host,realm,site_id = mothership.split_fqdn(unqdn) if zs_unqdn==None: zs_unqdn, zs_user, zs_pass = get_default_server(cfg, realm, site_id) else: zs_user = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_user")).split('=')[1] zs_pass = str(kv_select(cfg, zs_unqdn, key="zabbix_admin_pass")).split('=')[1] zs_host,zs_realm,zs_site_id = mothership.split_fqdn(zs_unqdn) zs_fqdn = '.'.join([zs_unqdn,cfg.domain]) # set the zabbix host and url path, log in try: # uncomment to debug #zapi = ZabbixAPI(zs_fqdn,'/', debug_level=6) zapi = ZabbixAPI(zs_fqdn,'/') zapi.login(zs_user, zs_pass) # uncomment to debug #print "Zabbix API Version: %s" % zapi.api_version() #print "Logged in: %s" % str(zapi.test_login()) except ZabbixAPIException, e: sys.stderr.write(str(e) + '\n')
def disable(cfg, host, realm, site_id): """ [description] shut a server's port on the management switch [parameter info] required: cfg: the config object. useful everywhere host: the server whose port we're disabling realm: the realm the server lives in site_id: the site_id the server lives in [return value] no explicit return """ # get management vlan web control panel credentials discard,user = str(kv_select(cfg, '', key="mgmt_vlan_web_control_user")).split('=') discard,password = str(kv_select(cfg, '', key="mgmt_vlan_web_control_password")).split('=') # get server switch port info s, n, h = mothership.snh(cfg, host, realm, site_id) for iface in n: if iface.interface == cfg.mgmt_vlan_interface: print "disabling mgmt port for %s.%s.%s" % (host, realm, site_id) switch_port = iface.switch_port else: pass # decide what method to use, based on /etc/mothership.yaml if cfg.mgmt_vlan_style == 'snmp': #need to write some method for snmp control print "snmp management vlan control has not yet been implemented" elif cfg.mgmt_vlan_style == 'curl': # build our command line cmdln = "curl" upass = user + ":" + password cmdurl = cfg.mgmt_vlan_disable_url + switch_port # fire off a curl to hit the web control panel and enable the port subprocess.Popen([cmdln, '-k', '-u', upass, cmdurl], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() status(cfg, host, realm, site_id) else: print "An odd failure occurred in the mgmt_vlan section. please check your config"
# uncomment to debug #zapi = ZabbixAPI(zs_fqdn,'/', debug_level=6) zapi = ZabbixAPI(zs_fqdn,'/') zapi.login(zs_user, zs_pass) # uncomment to debug #print "Zabbix API Version: %s" % zapi.api_version() #print "Logged in: %s" % str(zapi.test_login()) except ZabbixAPIException, e: sys.stderr.write(str(e) + '\n') # get server info s, n, h = mothership.snh(cfg, host, realm, site_id) # Construct the tag template name. # All templates descend from the default discard,zab_def_tmpl = str(kv_select(cfg, '', key="zabbix_default_template")).split('=') # uncomment to debug #print 'Default template is: ' + zab_def_tmpl zab_tag_tmpl = zab_def_tmpl + '_' + s.tag # get the templateid from zabbix zab_tid = None t = zapi.template.get(host=zab_tag_tmpl) if t: for k in t.keys(): zab_tid = t[k]['templateid'] else: pass # if we're given a template, try and use that if zabbix_template: t = zapi.template.get(host=zabbix_template)
#zapi = ZabbixAPI(zs_fqdn,'/', debug_level=6) zapi = ZabbixAPI(zs_fqdn, '/') zapi.login(zs_user, zs_pass) # uncomment to debug #print "Zabbix API Version: %s" % zapi.api_version() #print "Logged in: %s" % str(zapi.test_login()) except ZabbixAPIException, e: sys.stderr.write(str(e) + '\n') # get server info s, n, h = mothership.snh(cfg, host, realm, site_id) # Construct the tag template name. # All templates descend from the default discard, zab_def_tmpl = str( kv_select(cfg, '', key="zabbix_default_template")).split('=') # uncomment to debug #print 'Default template is: ' + zab_def_tmpl zab_tag_tmpl = zab_def_tmpl + '_' + s.tag # get the templateid from zabbix zab_tid = None t = zapi.template.get(host=zab_tag_tmpl) if t: for k in t.keys(): zab_tid = t[k]['templateid'] else: pass # if we're given a template, try and use that if zabbix_template: t = zapi.template.get(host=zabbix_template)