教你使用zabbix api批量添加數(shù)百臺監(jiān)控主機的方法
在公司規(guī)模很龐大的時候,每次都手動添加監(jiān)控主機將會很麻煩,我們可以利用zabbix的api去批量添加監(jiān)控主機
本次我們將實現(xiàn)用一臺主機虛擬出100臺主機,并通過api的方式自動添加監(jiān)控主機
掌握本次方法,無需要了解python,也不需要寫python腳本
1.獲取批量添加主機的api
可以從官網(wǎng)取到
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, #默認(rèn)
“useip”: 1, #ip地址
“ip”: “192.168.81.160”, #agent的地址
“dns”: “”,
“port”: “10050” #agent端口
}
],
“groups”: [
{
“groupid”: “15” #主機群組的id
}
],
“templates”: [
{
“templateid”: “10271” #模板id
}
]
2.創(chuàng)建一百臺服務(wù)器
我們雖然沒有一百臺服務(wù)器,但是我們可以創(chuàng)建100個網(wǎng)卡,且都在一臺機器上,有一百個ip即可
[root@k8s-master ~]# for i in {100..200}
do
ifconfig ens33:$i 192.168.81.$i
ifconfig ens33 up
done

3.編寫批量添加主機的腳本
3.1.將一百臺機器的ip寫到文件中
[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt
3.2.在機器上安裝zabbix-agent
[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.編寫批量添加主機的腳本
[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.執(zhí)行腳本
[root@k8s-master ~]# chmod a+x zabbix_host_creates.sh [root@k8s-master ~]# sh zabbix_host_creates.sh
腳本輸出

3.5.查看監(jiān)控主機是否批量創(chuàng)建成功
全部為有效狀態(tài)

到此這篇關(guān)于利用zabbix api批量添加數(shù)百臺監(jiān)控主機的文章就介紹到這了,更多相關(guān)abbix api批量添加主機內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信