教你使用zabbix api批量添加數(shù)百臺監(jiān)控主機(jī)的方法
在公司規(guī)模很龐大的時候,每次都手動添加監(jiān)控主機(jī)將會很麻煩,我們可以利用zabbix的api去批量添加監(jiān)控主機(jī)
本次我們將實現(xiàn)用一臺主機(jī)虛擬出100臺主機(jī),并通過api的方式自動添加監(jiān)控主機(jī)
掌握本次方法,無需要了解python,也不需要寫python腳本
1.獲取批量添加主機(jī)的api
可以從官網(wǎng)取到
{ "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”, #主機(jī)名稱
“interfaces”: [
{
“type”: 1, #使用agent客戶端
“main”: 1, #默認(rèn)
“useip”: 1, #ip地址
“ip”: “192.168.81.160”, #agent的地址
“dns”: “”,
“port”: “10050” #agent端口
}
],
“groups”: [
{
“groupid”: “15” #主機(jī)群組的id
}
],
“templates”: [
{
“templateid”: “10271” #模板id
}
]
2.創(chuàng)建一百臺服務(wù)器
我們雖然沒有一百臺服務(wù)器,但是我們可以創(chuàng)建100個網(wǎng)卡,且都在一臺機(jī)器上,有一百個ip即可
[root@k8s-master ~]# for i in {100..200}doifconfig ens33:$i 192.168.81.$iifconfig ens33 updone
3.編寫批量添加主機(jī)的腳本
3.1.將一百臺機(jī)器的ip寫到文件中
[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt
3.2.在機(jī)器上安裝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.編寫批量添加主機(jī)的腳本
[root@k8s-master ~]# vim zabbix_host_creates.sh #!/bin/bash#批量添加zabbix主機(jī)#登陸token=`echo $json | grep result | awk -F""" "{print $10}"`#批量添加主機(jī)for ip in `cat /root/host.txt`docurl -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.tooldone
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)控主機(jī)是否批量創(chuàng)建成功
全部為有效狀態(tài)
到此這篇關(guān)于利用zabbix api批量添加數(shù)百臺監(jiān)控主機(jī)的文章就介紹到這了,更多相關(guān)abbix api批量添加主機(jī)內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
