Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api

 

1、向 api_jsonrpc.php 发送HTTP_POST 登录请求,获取身份验证令牌

# -*- coding:utf-8 -*-
import json
import requests

url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
    "jsonrpc" : "2.0",
    "method" : "user.login",
    "params" : {
        "user" : "Admin",
        "password" : "zabbix"
    },
    "id" : 1
}

ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)

 输出结果:

 {
  "jsonrpc":"2.0",
  "result":"da336b04d376d914bf06bd2192c4ce3f",  #身份验证令牌
  "id":1
}

 

2、查询所有主机的信息

url = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 2,
    "auth": "da336b04d376d914bf06bd2192c4ce3f"  #这是第一步获取的身份验证令牌
}

ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
print(ret.text)

  输出结果:

{
    "jsonrpc": "2.0", 
    "result": [
        {
            "hostid": "10084", 
            "host": "Zabbix server", 
            "interfaces": [
                {
                    "interfaceid": "1", 
                    "ip": "127.0.0.1"
                }
            ]
        }
    ], 
    "id": 2
}
View Code

相关文章: