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 new(fqdn, key, value): """ Constructor that takes a host.realm.site style fqdn. """ hostname, realm, site_id = mothership.split_fqdn(fqdn) kv = KV(key, value, hostname, realm, site_id) return kv
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 add(cfg, fqdn, key, value): """ Add a new value with set semantics. """ # make sure we have a value if not value: raise KVError('No value specified!') # if we're adding a tag, make sure it exists, first! if key == 'tag': tag = cfg.dbsess.query(Tag).filter(Tag.name == value).first() if not tag: print "Trying to apply a nonexistent tag. Abort! Abort!" raise KVError('Try "ship tag --help" for more info') # make sure the serverpath exists hostname, realm, site_id = mothership.split_fqdn(fqdn) if hostname != None: results = cfg.dbsess.query(Server) results = results.\ filter(Server.site_id==site_id).\ filter(Server.realm==realm).\ filter(Server.hostname==hostname).\ first() if not results: raise KVError( 'Trying to apply a tag to a nonexistent host. Abort!') # Check for duplicate key=value. kv = select(cfg, fqdn, key, value) if kv: print "that key=value pair exists already!" ans = raw_input("Do you want to update it? (y/n): ") if ans == "Y" or ans == "y": print "updating key=value" kv = upsert(cfg, fqdn, key, value) return kv else: print "key=value unchanged, exiting." return kv print "no existing key=value found, inserting." if fqdn == '': print "this will apply %s=%s globally" % (key, value) ans = raw_input("are you sure you want to do this? (y/n): ") if ans == "Y" or ans == "y": kv = new(fqdn, key, value) cfg.dbsess.add(kv) cfg.dbsess.commit() return kv kv = new(fqdn, key, value) cfg.dbsess.add(kv) cfg.dbsess.commit() return kv
def add(cfg, fqdn, key, value): """ Add a new value with set semantics. """ # make sure we have a value if not value: raise KVError("No value specified!") # if we're adding a tag, make sure it exists, first! if key == "tag": tag = cfg.dbsess.query(Tag).filter(Tag.name == value).first() if not tag: print "Trying to apply a nonexistent tag. Abort! Abort!" raise KVError('Try "ship tag --help" for more info') # make sure the serverpath exists hostname, realm, site_id = mothership.split_fqdn(fqdn) if hostname != None: results = cfg.dbsess.query(Server) results = ( results.filter(Server.site_id == site_id) .filter(Server.realm == realm) .filter(Server.hostname == hostname) .first() ) if not results: raise KVError("Trying to apply a tag to a nonexistent host. Abort!") # Check for duplicate key=value. kv = select(cfg, fqdn, key, value) if kv: print "that key=value pair exists already!" ans = raw_input("Do you want to update it? (y/n): ") if ans == "Y" or ans == "y": print "updating key=value" kv = upsert(cfg, fqdn, key, value) return kv else: print "key=value unchanged, exiting." return kv print "no existing key=value found, inserting." if fqdn == "": print "this will apply %s=%s globally" % (key, value) ans = raw_input("are you sure you want to do this? (y/n): ") if ans == "Y" or ans == "y": kv = new(fqdn, key, value) cfg.dbsess.add(kv) cfg.dbsess.commit() return kv kv = new(fqdn, key, value) cfg.dbsess.add(kv) cfg.dbsess.commit() return kv
def select(cfg, fqdn, key, value=None): """ Returns a specific KV object. """ hostname, realm, site_id = mothership.split_fqdn(fqdn) results = cfg.dbsess.query(KV).\ filter(KV.hostname==hostname).\ filter(KV.realm==realm).\ filter(KV.site_id==site_id).\ filter(KV.key==key) if value: results = results.filter(KV.value==value) kv = results.first() return kv
def select(cfg, fqdn, key, value=None): """ Returns a specific KV object. """ hostname, realm, site_id = mothership.split_fqdn(fqdn) results = cfg.dbsess.query(KV).\ filter(KV.hostname==hostname).\ filter(KV.realm==realm).\ filter(KV.site_id==site_id).\ filter(KV.key==key) if value: results = results.filter(KV.value == value) kv = results.first() return kv
def collect(cfg, fqdn, key=None, value=None): """ Returns a list of all matches. """ results = cfg.dbsess.query(KV) if fqdn != None: hostname, realm, site_id = mothership.split_fqdn(fqdn) results = results.\ filter(or_(KV.site_id==site_id, KV.site_id==None)).\ filter(or_(KV.realm==realm, KV.realm==None)).\ filter(or_(KV.hostname==hostname, KV.hostname==None)).\ order_by(desc(KV.hostname), desc(KV.realm), desc(KV.site_id)) if key: results = results.filter(KV.key==key) if value: results = results.filter(KV.value==value) kvs = results.all() return kvs
def collect(cfg, fqdn, key=None, value=None): """ Returns a list of all matches. """ results = cfg.dbsess.query(KV) if fqdn != None: hostname, realm, site_id = mothership.split_fqdn(fqdn) results = results.\ filter(or_(KV.site_id==site_id, KV.site_id==None)).\ filter(or_(KV.realm==realm, KV.realm==None)).\ filter(or_(KV.hostname==hostname, KV.hostname==None)).\ order_by(desc(KV.hostname), desc(KV.realm), desc(KV.site_id)) if key: results = results.filter(KV.key == key) if value: results = results.filter(KV.value == value) kvs = results.all() return kvs