Generating hosts

One of the possible problems that we need to solve when  importing  hosts in Zabbix from XML is creating a larger number of hosts. We could use a hackish script like this to generate a Zabbix host XML out of a CSV file:

#!/bin/bash 
 
split="%" 
agent_port=10050 
useip=1 
 
[[ -s "$1" ]] || { 
        echo "Usage: pass an input CSV file as the first parameter 
File should contain data in the following format: hostname,dns,ip,hostgroup,linked_template,agent_port 
agent_port is optional 
For groups and templates multiple entries are separated with % 
First line is ignored (assuming a header)" 
        exit 1 
} 
 
echo "<?xml version="1.0" encoding="UTF-8"?> 
<zabbix_export> 
    <version>4.0</version> 
    <date>$(date "+%Y-%m-%dT%H:%M:%SZ")</date> 
    <hosts>" 
while read line; do 
        hostname=$(echo $line | cut -d, -f1) 
        dns=$(echo $line | cut -d, -f2) 
        ip=$(echo $line | cut -d, -f3) 
        group=$(echo $line | cut -d, -f4) 
        template=$(echo $line | cut -d, -f5) 
        port=$(echo $line | cut -d, -f6) 
 
        hostname1=${hostname%"} 
        dns1=${dns%"} 
        ip1=${ip%"} 
        group1=${group%"} 
        template1=${template%"} 
        port1=${port%"} 
 
        hostgroups=$(echo $group1 | tr "$split" "
") 
        templates=$(echo $template1 | tr "$split" "
") 
 
        echo "        <host> 
            <host>$(echo ${hostname1#"})</host> 
            <name>$(echo ${hostname1#"})</name> 
            <status>0</status> 
            <description/> 
            <proxy/> 
            <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/> 
            <interfaces> 
                <interface> 
                    <default>1</default> 
                    <type>1</type> 
                    <useip>$useip</useip> 
                    <ip>${ip1#"}</ip> 
                    <dns>${dns1#"}</dns> 
                    <port>${port1:-$agent_port}</port> 
                    <bulk>1</bulk> 
                    <interface_ref>if1</interface_ref> 
                </interface> 
            </interfaces>" 
        echo "            <groups>" 
        while read hostgroup; do 
                echo "                <group> 
                    <name>${hostgroup#"}</name> 
                </group>" 
        done < <(echo "$hostgroups") 
        echo "            </groups> 
            <templates>" 
        while read hosttemplate; do 
                echo "                <template> 
                    <name>${hosttemplate#"}</name> 
                </template>" 
        done < <(echo "$templates") 
        echo "            </templates>" 
        echo "        </host>" 
done < <(tail -n +2 $1) 
 
echo "    </hosts> 
</zabbix_export>" 

Save this script as csv_to_zabbix_xml.sh and make it executable:

$ chmod 755 csv_to_zabbix_xml.sh
Some people say that the shell isn't an appropriate tool to handle XML files. The shell is a great tool for anything and perfectly fine for our simple, quick host generation.

This script takes a CSV file as the input, ignores the first line, and uses all other lines as host entries. We must specify the hostname, DNS, IP, and agent port. Additionally, for each host, we can specify multiple host groups and templates the host should be linked to by delimiting multiple entries with a percent sign. The useip parameter defaults to 1; setting it to 0 will use DNS instead. Notice how we're generating all kinds of fields we aren't interested in at this time, all of the IPMI and TLS fields, setting the bulk parameter for the agent interface. Unfortunately, Zabbix XML exports are unnecessarily verbose, and it expects the same verbosity back. For a larger number of hosts, this will significantly increase the size of the XML file.

Quoting in the CSV file allows us to use commas in host group names.

To use this file, let's create a simple CSV file called test.csv:

"Host name","Host DNS","Host IP","Host groups","Templates","port" 
"test-xml-import","dns.name","1.2.3.4","Linux servers%Zabbix servers","Template Module ICMP Ping" 

We used a header line here, as the first line's always excluded—a single line in a file wouldn't do anything at all. Now, let's run our script:

$ ./csv_to_zabbix_xml.sh test.csv > zabbix_generated_hosts.xml  

In the frontend, navigate to Configuration | Hosts, click on Import in the upper-right corner, choose the zabbix_generated_hosts.xml file in the Import file field, and click on Import. The import should be successful—verify that back in Configuration | Hosts. As this host isn't very useful right now, feel free to delete it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194