在公司規(guī)模很龐大的時(shí)候,每次都手動(dòng)添加監(jiān)控主機(jī)將會(huì)很麻煩,我們可以利用zabbix的api去批量添加監(jiān)控主機(jī)
本次我們將實(shí)現(xiàn)用一臺(tái)主機(jī)虛擬出100臺(tái)主機(jī),并通過(guò)api的方式自動(dòng)添加監(jiān)控主機(jī)
掌握本次方法,無(wú)需要了解python,也不需要寫(xiě)python腳本
1.獲取批量添加主機(jī)的api
可以從官網(wǎng)取到
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/create
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
{ "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必要字段說(shuō)明
解釋?zhuān)?br /> “host”: “192.168.81.160”, #主機(jī)名稱(chēng)
“interfaces”: [
{
“type”: 1, #使用agent客戶(hù)端
“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)建一百臺(tái)服務(wù)器
我們雖然沒(méi)有一百臺(tái)服務(wù)器,但是我們可以創(chuàng)建100個(gè)網(wǎng)卡,且都在一臺(tái)機(jī)器上,有一百個(gè)ip即可
1
2
3
4
5
|
[root@k8s-master ~] # for i in {100..200} do ifconfig ens33:$i 192.168.81.$i ifconfig ens33 up done |
3.編寫(xiě)批量添加主機(jī)的腳本
3.1.將一百臺(tái)機(jī)器的ip寫(xiě)到文件中
1
|
[root@k8s-master ~] # echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt |
3.2.在機(jī)器上安裝zabbix-agent
1
2
3
4
|
[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.編寫(xiě)批量添加主機(jī)的腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
[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` 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í)行腳本
1
2
|
[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ù)百臺(tái)監(jiān)控主機(jī)的文章就介紹到這了,更多相關(guān)abbix api批量添加主機(jī)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://jiangxl.blog.csdn.net/article/details/111380904