zabbix扫描与zabbix-API比较
zabbix扫描:占用zabbix-server端的大量资源,同时添加主机时只有ip地址,这样分不清是什么业务
zabbix-API:添加方便,通过命令即可添加主机
官方网址
https://www.zabbix.com/documentation/4.0/zh/manual/api
略
任意一台能ping通zabbix-server执行
栗子:( 注意json格式 )
查询用户信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
上面的命令获取出来的token需要记录起来,后面要用到
35755d0fba51a9eb2851c852ba62af5b
get用户信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "35755d0fba51a9eb2851c852ba62af5b",
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
获取单个模板信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"NGINX_Check_Statuc"
]
}
},
"auth": "35755d0fba51a9eb2851c852ba62af5b",
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
获取多个模板信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"tomcat-template-m44",
"mysql-m44"
]
}
},
"auth": "35755d0fba51a9eb2851c852ba62af5b",
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
如果需要获取template和组还有代理的id
组id
template-id
代理id
通过脚本调用API快速添加:
root@zabbix-server:~# cat zabbix-add-host.sh
#!/bin/bash
IP="
172.31.10.105
172.31.10.106
172.31.10.107
172.31.10.108
"
for node_ip in ${IP};do
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'${node_ip}'",
"name": "m44-tomcat_'${node_ip}'",
"proxy_hostid": "10275",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001" #添加基础模板
}
]
},
"auth": "35755d0fba51a9eb2851c852ba62af5b",
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
done
扩展:调用其他文件执行
[root@zabbix-web02 ~]# cat number_ip_host.sh
#!/bin/bash
#
#********************************************************************
#Author: xuanlv
#QQ: 360956175
#Date: 2021-08-05
#FileName: number_ip_host.sh
#URL: https://www.cnblogs.com/xuanlv-0413/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
IP=$1
for node_ip in `cat ${IP}`;do #这里用cat命令才能for循环
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'${node_ip}'",
"name": "m44-tomcat_'${node_ip}'",
"proxy_hostid": "10275",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "35755d0fba51a9eb2851c852ba62af5b",
"id": 1
}' http://172.31.1.20/zabbix/api_jsonrpc.php | python3 -m json.tool
echo ${node_ip}
done
执行
[root@zabbix-web02 ~]# bash number_ip_host.sh ip.txt