添加一个监控项

import json
import requests
zabbix_url = "http://zabbix.example.com/api_jsonrpc.php"
zabbix_user = "Admin"
zabbix_password = "password"
header = {'Content-Type': 'application/json'}


#登录zabbix,获取用户令牌token
def getUserToken():
    data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": zabbix_user,
                "password": zabbix_password,
            },
            "id": 1,
        }

    result = requests.post(url=zabbix_url,data=json.dumps(data),headers=header)
    # 返回用户令牌
    return json.loads(result.text).get("result")

# 获取被监控主机信息 hostID
def getHostID():
    user_token = getUserToken()
    data= {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 1,
    "auth": user_token
}
    result = requests.post(url=zabbix_url,data=json.dumps(data),headers=header)
    host_id = json.loads(result.text).get("result")[0].get("hostid")
    interfaces_id = json.loads(result.text).get("result")[0].get("interfaces")[0].get("interfaceid")
    return host_id,interfaces_id

#创建监控项 监控根分区剩余空间
def ItemCreate():
    user_token = getUserToken()
    host_id,interface_id = getHostID()
    data = {
            "jsonrpc": "2.0",
            "method": "item.create",
            "params": {
                "name": "Free disk space on $1",
                "key_": "vfs.fs.size[/,free]",
                "hostid": host_id,
                "type": 0,
                "value_type": 3,
                "interfaceid": interface_id,
                "delay": 30
            },
            "auth": user_token,
            "id": 1
        }
    result = requests.post(url=zabbix_url,data=json.dumps(data),headers=header)
    print(result.text)

if __name__ == '__main__':
    ItemCreate()

代码重复率较高,下次优化

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://kkxl95.cn/article/1597824331/