Created
May 13, 2014 23:37
-
-
Save timss/7ae16fa7b7390ac54524 to your computer and use it in GitHub Desktop.
Script for generating Icinga host definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# generates a valid [host] configuration file for Icinga | |
# based off a template, for all the hosts in the subnet | |
icinga_root="/etc/icinga/objects" | |
subnet="10.0.0.1.*" | |
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 | |
# http://www.tldp.org/LDP/abs/html/bashver4.html | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define host{ | |
use generic-host | |
host_name tmp-hostname | |
alias tmp-alias | |
address tmp-address | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment