-
ansible -i discount_web -a "grep -oP '(?<=Hostname=)\S+' /etc/zabbix/zabbix_agentd.conf" all | grep [0-9]$ >> discount_web-Name
[root@ansible-53_26 discount_web]# cat discount_web-Name
discount_web-79-187
discount_web-79-186
discount_web-79-189
discount_web-79-188
discount_web-79-192
discount_web-79-190
discount_web-79-198
discount_web-79-191
discount_web-79-195
discount_web-79-193
discount_web-79-197
discount_web-79-194
discount_web-79-199
拿到列表,步骤一 完美实现.
setp 2 查看api json串构建格式
-
获取主机信息,方法 : host.get
-
-
Request:
-
-
{
-
"jsonrpc": "2.0",
-
"method": "host.get",
-
"params": {
-
"output": "extend",
-
"filter": {
-
"host": [
-
"Zabbix server",
-
"Linux server"
-
]
-
}
-
},
-
"auth": "038e1d7b1735c6a5436ee9eae095879e",
-
"id": 1
-
}
删除主机 方法 : host.delete
request
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"13",
"32"
],
"auth": "038e1d7b1735c6a5436ee9eae095879e",
"id": 1
}
params 那里显示了,当我们删除多个主机时,只需要提交一个 hostid list ,就可以删除,列表内的所有 agent.
setp 3 python 搓脚本,着急实现功能,拿以前的脚本改了改
-
-
#!/usr/bin/env python2.7
-
-
import json
-
import requests
-
-
-
def delete(username,passwd):
-
auth = json.dumps(
-
{
-
"jsonrpc": "2.0",
-
"method": "user.login",
-
"params": {
-
"user": username,
-
"password": passwd
-
},
-
"id": 0
-
}
-
)
-
-
try:
-
req = requests.post(url,auth,headers=header)
-
except requests.exceptions.ConnectionError,e:
-
print "Auth Failed, Please Check Your zabbix domain:",e
-
else:
-
authtoken = req.json()['result']
-
-
-
hostlist = []
-
with open("/root/syhphp") as f:
-
for host in f.readlines():
-
host = host.strip("\n")
-
-
-
query = json.dumps(
-
{
-
"jsonrpc": "2.0",
-
"method": "host.get",
-
"params": {
-
"output": ["hostid"],
-
"filter": {
-
"host": host
-
}
-
},
-
"auth": authtoken,
-
"id": 0
-
}
-
)
-
info = requests.post(url,query,headers=header)
-
-
hostid=info.json()['result'][0]['hostid'].encode('utf8')
-
hostlist.append(hostid)
-
-
print hostlist
-
query = json.dumps(
-
{
-
"jsonrpc": "2.0",
-
"method": "host.delete",
-
"params": hostlist,
-
"auth": authtoken,
-
"id": 0
-
}
-
)
-
-
-
requests.post(url,query,headers=header)
-
if __name__ == '__main__':
-
url = ""
-
header = {"Content-Type": "application/json"}
-
delete("Username","Password")
wepiao_Cail
参考资料: https://www.zabbix.com/documentation/2.2/manual/api/reference/host