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

Posted kaishirenshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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分钟。之后刷新页面,得到如下页面证明配置成功:

技术分享图片


以上是关于zabbix高级玩法之通过xml文件一次性导入的主要内容,如果未能解决你的问题,请参考以下文章

Word 查找替换高级玩法系列之 -- 通配符大全B篇

PassJava 开源项目(二十)之 详解 Elasticsearch 高级检索玩法

JavaScript的类型自动转换高级玩法JSFuck

Postman高级玩法之Pre-request Script

Zabbix番外1:使用Excel转XML来导入设备

zabbix导入模板错误-valuemap