Using API interface to view, create and delete monitoring host in zabbix monitoring system

Keywords: Zabbix JSON PHP curl

What is API:
API (Application Programming Interface) is a pre-defined function, which aims to provide the ability of application program and developers to access a set of routines based on a certain software or hardware without accessing the source code or understanding the details of internal working mechanism. In short, API is the external interface, using API can directly reach the software and hardware to operate .
The role of API in zabbix:
The Zabbix API provides programmable interfaces for batch operations, third-party software integration, and other functions.
The general workflow of API is as follows:
(1) The API is implemented by JSON-RPC. This means that to call any function, you need to send a POST request, and the input and output data are in JSON format
(2) Prepare a JSON object that describes what you want to do (create a host, get images, update monitoring items, etc.).
(3) Get the JSON format response.

1. Check the api interface of zabbix monitoring system:
vim zabbix-api.sh

curl -s -X POST -H 'Content-Type:application/json' -d '  
# Using curl command to simulate post request in json format
{
    "jsonrpc": "2.0",            # This is the standard JSON RPC parameter to indicate the protocol version. All requests will remain the same
    "method": "user.login",      # Getting API by user login
    "params": {
        "user": "Admin",
        "password": "jay"       # User name and password of zabbix monitoring system
    },
    "id": 1,    # This field is used to bind JSON requests and responses. The response will have the same "id" as the request. Useful for sending multiple requests at once, and these do not need to be unique or sequential
    "auth": null
}' http://172.25.1.1/zabbix/api_jsonrpc.php | python -m json.tool
# Send this JSON object to http://example.com/zabbix/api_jsonrpc.php by POST method. http://example.com/zabbix/ is the front-end address of Zabbix.
# api_jsonrpc.php is a PHP script that calls the API. It can be found in the directory where the visualization front end is installed.

Give the script permission to execute: chmod +x zabbix-api.sh

2. View zabbix host and monitored host information

curl -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.get",        # host.get method
    "params": {
        "output": ["host"]
    },
    "auth": "8491f6ca509122f6eee354c541f925b4",     # zabbix monitoring API
    "id": 1
}' http://172.25.1.1/zabbix/api_jsonrpc.php | python -m json.tool


3. Delete the host in zabbix monitoring

curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.delete",    # Call the host.delete method
    "params": [
        "10256"                          # Host number of the monitored host returned in the previous step
    ],
    "auth": "8491f6ca509122f6eee354c541f925b4",
    "id": 1
}' http://172.25.1.1/zabbix/api_jsonrpc.php | python -m json.tool


4. Create monitored host in zabbix monitoring

curl -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.create",    # Call the host.create method
    "params": {
        "host": "server4.example.com",     # Monitored host name
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.25.1.4",     # ip of monitored host
                "dns": "",
                "port": "10050"         # Port number of ZABBIX agent
            }
        ],
        "groups": [
            {
                "groupid": "2"         # Created host joined group
                # How to get the groupid and templateid? We can open a group and template in the web interface of zabbix, and we can get
            }
        ],
        "templates": [
            {
                "templateid": "10001"   # Template used by the established host
            }
        ]
    },
    "auth": "8491f6ca509122f6eee354c541f925b4",
    "id": 1
}' http://172.25.1.1/zabbix/api_jsonrpc.php | python -m json.tool

At this time, check the web interface of zabbix, and the server4 host has appeared

Posted by richo89 on Fri, 03 Jan 2020 17:28:46 -0800