How to call Zabbix API to obtain host information

Since the introduction of Zabbix 1.8 version, Zabbix API has begun to play an increasingly important role, providing programmable interfaces for batch operations, third-party software integration, and other applications.
In operation and maintenance practice, Zabbix API has more clever applications.
Faced with a large-scale monitoring equipment, it may happen that a certain machine fails but no alarm is issued. The reason may be that the Zabbix client of this machine is not monitored by the server for some reason.
Solving this problem is also very simple. In order to prevent some machines from not being monitored and managed by the system, the simplest solution is to obtain a list of all monitored hosts by calling the Zabbix API, and then compare it with the operation and maintenance asset database. The specific operations are as follows:

#!/usr/bin/evn python

import requests
import json

ZABIX_ADD = 'http://10.0.1.29/zabbix'
url = ZABIX_ADD + '/api_jsonrpc.php'

# user.login
payload = {
    "jsonrpc" : "2.0",
    "method" : "user.login",
    "params" : {
        'user' : 'Admin',
        'password' :'',
        },
    "auth" : None,
    "id" : 0,
}

headers = {
    'content-type' : 'application/json',
}

req = requests.post(url, json=payload, headers=headers)
auth = req.json()

# host.get
payload = {
    "jsonrpc" : "2.0",
    "method" : "host.get",
    "params" : {
        'output' : [
            'hostid',
            'name'
        ],
    },
    "auth" : auth['result'],
    "id" : 2,
}

res2 = requests.post(url, data=json.dumps(payload), headers=headers)
res2 = res2.json()

for host in res2['result'] :
    with open('host.txt', 'a+') as f:
        f.write(host['name'] + '\n')

Insert image description here
The above script is divided into two parts. The first part is user login; the second part is to obtain the host list after user login, and finally write it to a file. The result of running the entire script is to generate a list of all monitored IPs. This list can be compared with the asset database information to discover unmonitored hosts.
The above is the technology sharing in this issue.
Hello everyone, my name is Lele. Follow me and learn more tips on how to use Zabbix. If you encounter problems while using Zabbix, you can also go to the Lewei community to leave a message and ask questions.

Guess you like

Origin blog.csdn.net/weixin_43631631/article/details/132737994