教你使用zabbix api批量添加数百台监控主机的方法

2025-05-27 0 19

在公司规模很庞大的时候,每次都手动添加监控主机将会很麻烦,我们可以利用zabbix的api去批量添加监控主机

本次我们将实现用一台主机虚拟出100台主机,并通过api的方式自动添加监控主机

掌握本次方法,无需要了解python,也不需要写python脚本

1.获取批量添加主机的api

可以从官网取到

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必要字段说明

解释:
“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
}
]

2.创建一百台服务器

我们虽然没有一百台服务器,但是我们可以创建100个网卡,且都在一台机器上,有一百个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

教你使用zabbix api批量添加数百台监控主机的方法

3.编写批量添加主机的脚本

3.1.将一百台机器的ip写到文件中

?

1
[root@k8s-master ~]# echo 192.168.81.{100..200} | xargs -n1 > /root/host.txt

3.2.在机器上安装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.编写批量添加主机的脚本

?

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主机

#登陆

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.执行脚本

?

1

2
[root@k8s-master ~]# chmod a+x zabbix_host_creates.sh

[root@k8s-master ~]# sh zabbix_host_creates.sh

脚本输出

教你使用zabbix api批量添加数百台监控主机的方法

3.5.查看监控主机是否批量创建成功

全部为有效状态

教你使用zabbix api批量添加数百台监控主机的方法

到此这篇关于利用zabbix api批量添加数百台监控主机的文章就介绍到这了,更多相关abbix api批量添加主机内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://jiangxl.blog.csdn.net/article/details/111380904

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 教你使用zabbix api批量添加数百台监控主机的方法 https://www.kuaiidc.com/63108.html

相关文章

发表评论
暂无评论