Adding all hosts in subnet to icinga monitoring without specifying hostname/IP

icinganagios

I have a provisioning system setting up new hosts to be in the domain test.domain.local, i.e. client44.test.domain.local, and I have an Icinga server which I would like to automatically monitor all these hosts using regex, like *.test.domain.local.

All the clients will get nagios-nrpe-server (version. 2.13-3) package, which is also configured to allow the icinga-server to fetch data from them, and this has been verified to be working.

We're just for now going to monitor services/things we know all the nodes will have, for example SSH, response to ping etc.

I have looked at this link but I don't quite the relationship between hosts, hostgroups and service classes?

Both the Icinga server and all the clients run Debian.

Best Answer

I've been working with fredmu on this problem, and came up with a working solution inspired by Tim Brigham's answer.

Using the default generic-host definition this could be solved using script that automatically generates host configuration files based off a template. Optionally it could be done as a cronjob to generate these file on a routinely basis.

generate_host_cfg.sh

#!/usr/bin/env bash

icinga_root="/etc/icinga/objects"
subnet="10.0.0.*"
template="template_icinga.cfg"
alias_file="alias.txt"

# navigate to the Icinga configuration directory
cd $icinga_root

# create first server alias if doesn't exist
if [ ! -e $alias_file ]; then
    echo "1" > $alias_file
fi

# iterate through subnet, store "hostname:ip" in associative array
declare -A address

for host in $(nmap -sP $subnet | awk -F '[ ()]' '/for [a-z]+/ {print $5 ":" $7}'); do
    address[$(echo $host | cut -d: -f1)]=$(echo $host | cut -d: -f2)
done

# iterate through hosts, create files if not exist based off template
for host in ${!address[@]}; do
    host_file=${host}_icinga.cfg

    if [ ! -e $host_file ]; then
        # fetch new server alias
        alias=$(cat $alias_file)

        # create the next server alias
        expr $alias + 1 > $alias_file

        # create hostname_icinga.cfg if doesn't exist, based off template
        cp $template $host_file

        # replace contents of new template; hostname, alias and ip
        sed -i -r \
            -e "s/tmp-hostname/$host/" \
            -e "s/tmp-alias/Server$alias/" \
            -e "s/tmp-address/${address[$host]}/" $host_file
    fi

done

template_icinga.cfg

define host{
        use                     generic-host
        host_name               tmp-hostname
        alias                   tmp-alias
        address                 tmp-address
        }

Resulting in files like these:

define host{
        use                     generic-host
        host_name               monitor.company.local
        alias                   Server1
        address                 10.0.0.1
        }
Related Topic