zabbix高级玩法之通过xml文件一次性导入

author:headsen  chen

date: 2018-11-10  10:08:38

背景:zabbix的硬件监控架构已经部署完成,接下来就是创建主机这一步,2400台主机如果采用一台台的创建和关联模板就太慢了。硬件监控不用于agent方式,agent的方式可以采用添加自动发现的规则来自动注册。

          硬件监控采用 server端通过SNMP服务去各个物理机的管理卡上去抓相应的硬件信息。所以无法通过自动发现规则来添加主机

 

1,手动添加一台主机,导出该主机的xml配置信息:

<zabbix_export>
    <version>4.0</version>
    <date>2018-11-09T11:12:04Z</date>
    <groups>
        <group>
            <name>Linux servers</name>
        </group>
        <group>
            <name>北京dell服务器组</name>
        </group>
    </groups>
    <hosts>
        <host>
            <host>1.1.1.1</host>
            <name>1.1.1.1</name>
            <description/>
            <proxy>
                <name>10-20-42-2</name>
            </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>Template Dell iDrac SNMPV2</name>
                </template>
            </templates>
            <groups>
                <group>
                    <name>Linux servers</name>
                </group>
            </groups>
            <interfaces>
                <interface>
                    <default>1</default>
                    <type>2</type>
                    <useip>1</useip>
                    <ip>1.1.1.1</ip>
                    <dns/>
                    <port>161</port>
                    <bulk>1</bulk>
                    <interface_ref>if1</interface_ref>
                </interface>
            </interfaces>
            <applications/>
            <items/>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <inventory/>
        </host>
    </hosts>
</zabbix_export>

2,分析该xml文件,解析结构分为以下几个标签

<zabbix_export>
 <version>4.0</version>
    <date>2018-11-09T11:12:04Z</date>
    <groups>
                <group>  ...
                </group>
    </groups>
    <hosts> 
                    ...
    </hosts>   
</zabbix_export>

其中:hosts 标签就每台添加的主机的信息,我们每次添加的主机,都是再写一个 <hosts> 标签

3,将2400台主机的IP写入dell-ip-list 文件里,并放入centos6.8的系统里,以便通过shell进行操作

4,构思:利用for循环实现生成2400台主机的xml文件

     第一步:文件头部不动的部分:   

# cat  >> dell.xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>4.0</version>
    <date>2018-11-09T11:12:04Z</date>
    <groups>
        <group>
            <name>Linux servers</name>
        </group>
        <group>
            <name>北京dell服务器组</name>
        </group>
    </groups>
    <hosts>
EOF

第二步:写一个单 <hosts>标签的模板文件:

# cat >> plate.xml <<
       <host>
            <host>1.1.1.1</host>
            <name>1.1.1.1</name>
            <description/>
            <proxy>
                <name>10-20-42-2</name>
            </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>Template Dell iDrac SNMPV2</name>
                </template>
            </templates>
            <groups>
                <group>
                    <name>Linux servers</name>
                </group>
            </groups>
            <interfaces>
                <interface>
                    <default>1</default>
                    <type>2</type>
                    <useip>1</useip>
                    <ip>1.1.1.1</ip>
                    <dns/>
                    <port>161</port>
                    <bulk>1</bulk>
                    <interface_ref>if1</interface_ref>
                </interface>
            </interfaces>
            <applications/>
            <items/>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <inventory/>
        </host>
EOF

第三步:利用for循环生成2400个物理机的hosts标签文件的脚本: xml.sh

#!/bin/bash
for i in `cat dell-ip-list`
do
   cat plate.xml > dd
   sed -i "s/1.1.1.1/$i/g" dd
   cat dd >> dell.xml
done
echo "     </hosts>
</zabbix_export>" >> dell.xml

第四步:运行该脚本,生成dell.xml 的文件

# sh xml.sh

5,将dell.xml文件导入

静等15分钟。之后刷新页面,得到如下页面证明配置成功:

posted @ 2018-11-10 10:08  开始认识  阅读(2193)  评论(0编辑  收藏  举报