zabbix.py 1.05 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn

"""
删除zabbix configura里指定ip的host
此操作不可回滚,确认ip无误后再执行

"""

import json,sys,argparse
from zabbix_api import ZabbixAPI
server = "http://zabbix.yohops.com"
username = "yohoops"
password = "rknTpkVa@6ItBP"
zapi = ZabbixAPI(server=server, path="", log_level=0)
zapi.login(username, password)
 
def get_host_id(hosts):
    get_host_id = zapi.host.get(
        {
            "output": "hostid",
            "filter": {
                "host": hosts.split(",")
                }
        }
    )
    host_id = []
    host_id.append([host["hostid"] for host in get_host_id])
    if host_id:
        return host_id[0]
    else:
        return False
     
def delete_host_by_id(hosts_id):
    if hosts_id:
        hosts_delete = zapi.host.delete(hosts_id)
        return "Zabbix host {0} 已成功删除!".format(hosts_id)
    else:
        return "Host 不存在"


def delete_host_by_ip(private_ip):
    hosts_id = get_host_id(private_ip)
    print delete_host_by_id(hosts_id)