Authored by root

Add zabbix.py to decomission hosts

#!/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_args():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hosts", help="host ips")
# 解析所传入的参数
args = parser.parse_args()
if not args.hosts:
args.hosts = raw_input('hosts(ip): ')
return args
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])
return host_id[0]
def delete_host(hosts_id):
if hosts_id:
hosts_delete = zapi.host.delete(hosts_id)
return "host delete success!"
else:
return "host not found!"
if __name__ == "__main__":
args = get_args()
hosts_id = get_host_id(args.hosts)
print delete_host(hosts_id)
... ...