zabbix批量添加主机监控-zabbix api调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix批量添加主机监控-zabbix api调用相关的知识,希望对你有一定的参考价值。

使用python调用zabbix api

#!/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调用的主要内容,如果未能解决你的问题,请参考以下文章

调用zabbix API实现批量管理主机及个监控项

zabbix批量添加主机

zabbix通过api批量添加web监控

Ansible-playbook批量添加zabbix监控项目同步配置信息

Zabbix 调用API 批量添加主机等

通过python封装zabbix-api批量添加监控项