def delete_regex_hostname(range_hostgroup=None, re_name=None): ### set range hostgroup and regex of hostname to delete if range_hostgroup is not None and re_name is not None: try: ### create an opsview api object check_hostgroup = Opsviewhost(server=options.host, token=key, user=options.username) ### with each of hostgroup id do digest data of host in hostgroupid for groupid in range_hostgroup: results = check_hostgroup.opsview_search_host(hostgroupid=groupid, hostname=re_name) if results is not None: for each in results: m = re.match(r"(?P<host_name>%s)" % re_name, each["name"], re.IGNORECASE) if m: print "HOSTGROUP:" + each["hostgroup"]["name"] + "::name:" + each["name"] + "::ipaddress:" + each["ip"] + "::ref:" + each["ref"] +"::id:" + each["id"] # delete host on opsview del_status = check_hostgroup.opsview_delete_host(id=each["id"]) print del_status print "All Done" return True except: return False else: print "Either hostgroup or regex of hostname not exist, please try again!" return False
def range_hostgroup_delete(range_hostgroup=None): ### set range hostgroup to delete if range_hostgroup is not None: try: ### create an opsview api object check_hostgroup = Opsviewhost(server=options.host, token=key, user=options.username, group='2.Projects') ### with each of hostgroup id do digest data of host in hostgroupid for groupid in range_hostgroup: results = check_hostgroup.opsview_search_host(hostgroupid=groupid) if results is not None: for each in results: print "HOSTGROUP:" + each["hostgroup"]["name"] + "::name:" + each["name"] + "::ipaddress:" + each["ip"] + "::ref:" + each["ref"] +"::id:" + each["id"] # delete host on opsview #del_status = check_hostgroup.opsview_delete_host(id=each["id"]) #print del_status print "All Done" return True except: return False