Use zabbix api to add hundreds of monitoring hosts in batches

Use zabbix api to add monitoring hosts in batches

When the company is very large, it will be troublesome to manually add monitoring hosts every time. We can use zabbix api to add monitoring hosts in batches.

This time we will use one host to virtualize 100 hosts, and automatically add monitoring hosts through api

To master this method, there is no need to understand python, and no need to write python scripts

1. Get the API for adding hosts in batches

You can get it from the official website
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/create

{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "192.168.81.180",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.81.180",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "15"
            }
        ],
        "templates": [
            {
                "templateid": "10271"
            }
        ]
    },
    "auth": "'$token'",
    "id": 1
}

api required field description

Explanation:
"host": "192.168.81.160", #Host name
"interfaces": [
{ "type": 1, #Use agent client "main": 1, #default "useip": 1, #ipaddress " ip": "192.168.81.160", #agent's address "dns": "", "port": "10050" #agent port } ], "groups": [ { "groupid": "15" #host group Id } ], "templates": [ { "templateid": "10271" #templateid } ]

















2. Create a hundred servers

Although we don’t have a hundred servers, we can create 100 network cards, all of which are on one machine, with one hundred ips.

[root@k8s-master ~]# for i in {100..200}
do
ifconfig ens33:$i 192.168.81.$i
ifconfig ens33 up
done

Insert picture description here

3. Write a script to add hosts in batches

3.1. Write the ip of one hundred machines to a file

[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt

3.2. Install zabbix-agent on the machine

[root@k8s-master ~]# yum -y install zabbix-agent
[root@k8s-master ~]# vim /etc/zabbix/zabbix_agentd.conf 
Server=192.168.81.250
[root@k8s-master ~]# systemctl restart zabbix-agent

3.3. Write a script for adding hosts in batches

[root@k8s-master ~]# vim zabbix_host_creates.sh 
#!/bin/bash
#批量添加zabbix主机
#登陆
token=`echo $json | grep result | awk -F'"' '{print $10}'`

#批量添加主机
for ip in `cat /root/host.txt`
do
curl -s -X POST -H 'Content-Type: application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "'$ip'",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "'$ip'",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "15"
            }
        ],
        "templates": [
            {
                "templateid": "10271"
            }
        ]
    },
    "auth": "'$token'",
    "id": 1
}' http://192.168.81.250/zabbix/api_jsonrpc.php | python -m json.tool
done

3.4. Execute script

[root@k8s-master ~]# chmod a+x zabbix_host_creates.sh 
[root@k8s-master ~]# sh zabbix_host_creates.sh

Script output

Insert picture description here

3.5. Check whether the monitoring hosts are successfully created in batches

All active

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/111380904