How to call zabbix API to get the host

  • 2020-06-03 08:52:04
  • OfStack

preface

zabbix as the enterprise system and network monitoring solution, for small and medium-sized companies, basic can well meet the monitoring requirements of the machine, because of its deployment is convenient, simple operation is popular with many Internet companies now, so now the basic Internet companies to monitor with zabbix very much, after good zabbix server when you install, if the startup automatic registration function, automatically after all client installed on zabbix platform monitored to, but there's a problem, if your machine is very little, if the number of can come the basic no problem, but if your machine is in the thousands, At this time, those machines are being monitored, and those that are not are not monitored, so it is not so convenient to check. Therefore, the problem often found in daily operation and maintenance is that a machine fails to report to the police. After investigation, it is found that the zabbix client of this machine is not monitored by the server for some reason. In fact, the simplest solution is to get the list of all monitored servers from zabbix and then compare it with the operation and maintenance asset database. If the number is 1, it means that the monitoring does not leak the machine. If the number is not matched, send out the IP which is not suitable and then report to the police.

Zabbix API was introduced in version 1.8 and is already widely used. All Zabbix mobile clients are based on API and even the native WEB front end is built on it. Zabbix API middleware makes the architecture more modular and avoids directly manipulating the database. It allows you to create, update, and retrieve Zabbix objects using the JSON RPC protocol and do whatever you like (as long as you have an authenticated account, of course).

Zabbix API offers two main functions:

Manage the Zabbix configuration remotely Remote retrieval of configuration and historical data

We used zabbix api today to get a list of all the machines being monitored

The following code


#!/usr/bin/evn python
 
import requests
import json
 
ZABIX_ROOT = 'http://10.0.1.29/zabbix'
url = ZABIX_ROOT + '/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)

Script 1 is divided into 2 parts, part 1 is the user login, after logging in and then get the host list and finally writes a file, so the result of the script is running is to generate a list of all monitored IP, through this list, you can go with your assets to contrast the library information, and the script with requests, so it seems to be relaxed many, the code is simpler, is 2 post don't do too much explanation.

conclusion

The above is about calling zabbix API to get all the contents of the host. Friends who like API can modify it into a script suitable for their own business on this basis. I hope the content of this article can bring certain help to your study or work.


Related articles: