Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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
        }
share|improve this answer
    
works nicley, thanks a lot. You do need working DNS and nmap installed. –  fredmu May 14 '14 at 12:31

There are a lot of ways to tackle this.

I have previously assembled bash scripts which would run as a single icinga check. The script would then check all the hosts in the subdomain and return a state based on if or how many failures are found.

More recently I've become a fan of custom facts in puppet. Combined with a well writeb template you can easily add your checks across a large number of nodes using a for loop.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.