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
}