zabbix.py
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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)