您的位置:首页 > 其它

Zabbix批量导入主机

2018-10-11 20:07 190 查看

Zabbix批量导入主机

在实际工作环境中我们一个集群里面可能有十几上百台一摸一样的主机,需要监控的内容也是一摸一样的,这个时候我们就可以使用下面的方式批量导入主机了

1,我们先在Zabbix-web里面配置好一台主机,然后把配置的结果导出来,我们通过脚本批量替换一下就行

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.4</version>
<date>2018-10-11T09:54:14Z</date>
<groups>
<group>
<name>Web_Server</name>
</group>
</groups>
<hosts>
<host>
//配置里面的主机名称
<host>web01-7</host>
//配置里面的可见的名称
<name>web01-7</name>
//描述信息
<description>这是一个动态的web服务器</description>
<proxy/>
<status>0</status>
<ipmi_authtype>-1</ipmi_authtype>
<ipmi_privilege>2</ipmi_privilege>
<ipmi_username/>
<ipmi_password/>
<tls_connect>1</tls_connect>
<tls_accept>1</tls_accept>
<tls_issuer/>
<tls_subject/>
<tls_psk_identity/>
<tls_psk/>
<templates>
//引用的模版,我们需要提前给模版定义好
<template>
<name>TCP Status</name>
</template>
//每一个模版是一个</template>标签
<template>
<name>Template OS Linux</name>
</template>
</templates>
<groups>
<group>
//属于的那个群组
<name>Web_Server</name>
</group>
</groups>
<interfaces>
<interface>
<default>1</default>
<type>1</type>
<useip>1</useip>
//agent端的IP地址
<ip>172.16.1.7</ip>
<dns/>
//agent端的端口
<port>10050</port>
<bulk>1</bulk>
<interface_ref>if1</interface_ref>
</interface>
</interfaces>
<applications/>
<items/>
<discovery_rules/>
<httptests/>
<macros/>
<inventory/>
</host>
</hosts>
</zabbix_export>

2,但是我们如何更改的。

其实我们只需要更改<host>标签里面的值即可,编写一个脚本

[root@zabbix tmp]# cat test_zbx_host.sh
#!/bin/bash
export PATH=$PATH
cat >> zbx_host_medium.xml <<EOF
<host>
<host>$1</host>
<name>$1</name>
<description>$3</description>
<proxy/>
<status>0</status>
<ipmi_authtype>-1</ipmi_authtype>
<ipmi_privilege>2</ipmi_privilege>
<ipmi_username/>
<ipmi_password/>
<tls_connect>1</tls_connect>
<tls_accept>1</tls_accept>
<tls_issuer/>
<tls_subject/>
<tls_psk_identity/>
<tls_psk/>
<templates>
<template>
<name>TCP Status</name>
</template>
<template>
<name>Template OS Linux</name>
</template>
</templates>
<groups>
<group>
<name>Web_Server</name>
</group>
</groups>
<interfaces>
<interface>
<default>1</default>
<type>1</type>
<useip>1</useip>
<ip>$2</ip>
<dns/>
<port>10050</port>
<bulk>1</bulk>
<interface_ref>if1</interface_ref>
</interface>
</interfaces>
<applications/>
<items/>
<discovery_rules/>
<httptests/>
<macros/>
<inventory/>
</host>
EOF
//脚本执行说明:
// sh test_zbx_host.sh Name Agent_ip comment
// 前两个参数必须有。后面说明信息可以没有

我们只需要把这些host主体追加到一个新的文件里面,然后再给这个文件加上头和尾导入就行

3,如果我们一条一条的执行命令还是很累,我们写一个批量执行的命令,先生存一个需要管理的agent端,和名称

[root@zabbix tmp]# cat host_ip.txt
web01-7 172.16.1.7 这是第一个动态web服务器
web02-8 172.16.1.8 这是第二个动态web服务器
web03-9 172.16.1.9 这是第一个静态web服务器
web04-10 172.16.1.10 这是二个静态web服务器

4,然后命令拼接一下

[root@zabbix tmp]# awk '{print "sh test_zbx_host.sh "$1,$2,$3}' host_ip.txt
sh test_zbx_host.sh web01-7 172.16.1.7 这是第一个动态web服务器
sh test_zbx_host.sh web02-8 172.16.1.8 这是第二个动态web服务器
sh test_zbx_host.sh web03-9 172.16.1.9 这是第一个静态web服务器
sh test_zbx_host.sh web04-10 172.16.1.10 这是二个静态web服务器

5,真正的执行,然后拼接头部尾部

[root@zabbix tmp]# awk '{print "sh test_zbx_host.sh "$1,$2,$3}' host_ip.txt  | bash
[root@zabbix tmp]# cat zbx_host_head.xml zbx_host_medium.xml zbx_host_tail.xml  >> zbx_host_input.xml
//然后把文件导入到客户机上



目前自己编写的能掌握的,后期应该有更方便的方法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Zabbix 批量 导入