Use zabbix api para agregar hosts de monitoreo en lotes
Cuando la empresa es muy grande, será problemático agregar manualmente hosts de monitoreo cada vez. Podemos usar zabbix api para agregar hosts de monitoreo en lotes.
Esta vez usaremos un host para virtualizar 100 hosts y agregaremos automáticamente hosts de monitoreo a través de api.
Para dominar este método, no es necesario comprender Python y no es necesario escribir scripts de Python
1. Obtenga la API para agregar hosts en lotes
Puede obtenerlo en el sitio web oficial
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 descripción de campo obligatoria
Explicación:
"host": "192.168.81.160", # Nombre de host
"interfaces": [
{ "type": 1, #Use agent client "main": 1, #default "useip": 1, #ipaddress "ip" : "192.168.81.160", # dirección del agente "dns": "", "puerto": "10050" # puerto del agente } ], "grupos": [ { "groupid": "15" #host group Id } ], "templates": [ { "templateid": "10271" #templateid } ]
2. Crea cien servidores
Aunque no tenemos cien servidores, podemos crear 100 tarjetas de red, todas las cuales están en una máquina, con cien ips.
[root@k8s-master ~]# for i in {100..200}
do
ifconfig ens33:$i 192.168.81.$i
ifconfig ens33 up
done
3. Escriba una secuencia de comandos para agregar hosts en lotes.
3.1. Escribe la ip de cien máquinas en un archivo
[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt
3.2. Instale zabbix-agent en la máquina
[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. Escribir una secuencia de comandos para agregar hosts en lotes
[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. Ejecutar secuencia de comandos
[root@k8s-master ~]# chmod a+x zabbix_host_creates.sh
[root@k8s-master ~]# sh zabbix_host_creates.sh
Salida de script
3.5. Verifique si los hosts de monitoreo se crearon con éxito en lotes
Todo activo