zabbix批量添加主机监控-zabbix api调用
2021-07-20 14:36
标签:cad json version python字典 group 类型 host templates mat 使用python调用zabbix api zabbix批量添加主机监控-zabbix api调用 标签:cad json version python字典 group 类型 host templates mat 原文地址:http://www.cnblogs.com/xiewenming/p/8038281.html#!/usr/bin/env python
# Version = 3.6.1
# __auth__ = ‘warren‘
import json
from urllib import request, parse
ZABBIX_URL = ‘http://192.168.22.22/zabbix‘
ZABBIX_USERNAME = "Admin"
ZABBIX_PASSWORD = "123456"
#以列表形式定义要添加的主机信息
hostlist=["192.168.22.111","192.168.22.112"]
#遍历主机列表定义要添加的主机组id和模板id
for host in hostlist:
url = "{}/api_jsonrpc.php".format(ZABBIX_URL)
header = {"Content-Type": "application/json"}
# auth user and password
data = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": host,
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": host,
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "19"
}
],
"templates": [
{
"templateid": "10081"
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "9afc764edb5b6bbd09369f7028231b70",
"id": 1
}
# 由于API接收的是json字符串,故需要转化一下
value = json.dumps(data).encode(‘utf-8‘)
# 对请求进行包装
req = request.Request(url, headers=header, data=value)
# 验证并获取Auth ID
try:
# 打开包装过的url
result = request.urlopen(req)
except Exception as e:
print("Auth Failed, Please Check Your Name And Password:", e)
else:
response = result.read()
# 上面获取的是bytes类型数据,故需要decode转化成字符串
page = response.decode(‘utf-8‘)
# 将此json字符串转化为python字典
page = json.loads(page)
result.close()
#打印信息
print("Create host Successful. The host ID Is: {}".format(page.get(‘result‘)))
#如果重复添加了 会返回The host ID Is: None
文章标题:zabbix批量添加主机监控-zabbix api调用
文章链接:http://soscw.com/essay/106641.html