小编给大家分享一下如何使用zabbix api批量添加数百台监控主机,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
在公司规模很庞大的时候,每次都手动添加监控主机将会很麻烦,我们可以利用zabbix的api去批量添加监控主机
本次我们将实现用一台主机虚拟出100台主机,并通过api的方式自动添加监控主机
掌握本次方法,无需要了解python,也不需要写python脚本
可以从官网取到
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必要字段说明
解释:
“host”: “192.168.81.160”, #主机名称
“interfaces”: [
{
“type”: 1, #使用agent客户端
“main”: 1, #默认
“useip”: 1, #ip地址
“ip”: “192.168.81.160”, #agent的地址
“dns”: “”,
“port”: “10050” #agent端口
}
],
“groups”: [
{
“groupid”: “15” #主机群组的id
}
],
“templates”: [
{
“templateid”: “10271” #模板id
}
]
我们虽然没有一百台服务器,但是我们可以创建100个网卡,且都在一台机器上,有一百个ip即可
[root@k8s-master ~]# for i in {100..200}
do
ifconfig ens33:$i 192.168.81.$i
ifconfig ens33 up
done
[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt
[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
[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
[root@k8s-master ~]# chmod a+x zabbix_host_creates.sh
[root@k8s-master ~]# sh zabbix_host_creates.sh
脚本输出
全部为有效状态
看完了这篇文章,相信你对“如何使用zabbix api批量添加数百台监控主机”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。