Python calls the zabbix api

Prerequisite preparation:

1. Use python requests module

2. Learn about json

3. The specific call of ZABBIX API is recommended to browse the official website first


First code:

import requests,json
#
#The url must be correct. Change the IP address to the one of your own zabbix server
zbx_url = "http://192.168.60.130:3080/zabbix/api_jsonrpc.php"

#There must be 'content type' in the post request header: 'application / JSON RPC'
headers = {'Content-Type': 'application/json-rpc'}

#Passing json data to api; login
login = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1
}
#The first login does not need to write "auth" in the json field, otherwise there will be an error

#Send data to api
ret = requests.post(zbx_url, data=json.dumps(login), headers=headers)

#Serialize results
ret = ret.json()
auth = ret['result']

#Get the problem host json
data = {
    "jsonrpc": "2.0",
    "method":"trigger.get",
    "params": {
        #Output indicates which parameters are included in the output result
        "output": [
            "triggerid",
            "description",
            "status",
            "value",
            "priority",
            "lastchange",
            "recovery_mode",
            "hosts",
            "state",
        ],
        "selectHosts": "hosts",  #Need to include host ID information to facilitate querying host information according to host ID
        "selectItems":"items",
        "filter": {
            #Filter criteria
             "value": 1,#A value of 1 indicates a problem
             "status": 0#A status of 0 indicates an enabled trigger
        },
    },
    "auth":auth,#The auth here is obtained after login
    'id':'1'#This id is optional
}

#Send query data to ZABBIX server
ret = requests.post(zbx_url,data=json.dumps(data),headers=headers)

respone_result = ret.json()['result']#json serialization of results

print(respone_result)


Here is a brief description of the appeal Code:

To call the zabbix api to get data, first of all, you need to get the auth string of characters for subsequent content acquisition. Auth can be seen as a "secret code" between you and ZABBIX server;

The reason why the login json content is written like this Official regulations of zabbix , do not use the tab key in the json string.

login = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",          #Fill in according to your own situation
        "password": "zabbix"      #Fill in according to your own conditions
    },
    "id": 1
}


Get the json of the problem host It is recommended to browse the description of the official website first. It is important to emphasize the two key s of output and filter. Output is the content returned by the zabbix api. The filter is equivalent to the filter condition:

"filter": {
            #Filter criteria
             "value": 1,              #A value of 1 indicates a problem
             "status": 0              #A status of 0 indicates an enabled trigger
        },

The appeal code indicates value=1 and status=0, which is a kind of relationship with. It is very similar to the filtering operation when querying database tables.


It is strongly recommended to have a general look first Official website documents

Tags: Linux JSON Zabbix Python PHP

Posted on Fri, 06 Dec 2019 05:22:43 -0800 by harvey