Quick nagios host config generator

Here is a quick script I have cobbled to generate nagios host configs it may need to be changed for template etc but you get the gist.

It includes support for padding of hostname numbers e.g. padding of 3 = web001, web002….web045…web999.

#!/bin/bash
# Nagios host config generater.
# Bruce Tonge
#
#If you need to gen a config for a large number of sequentialy numbered hosts
#just run:
#nagios.sh web example.com 1 50 3
#
#This will create host entries for machines 1-50 but named web001 - web050
#The output should be redirected to a file.

if [ “$#” -lt “4” ]
then
echo “usage: $0 <hostprefix> <fqdn> <first node number> <last node number> <host number padding>”
exit 0
fi

host_str=$1 #host prefix
fqdn_str=$2 #domain name
fnode_num=$3 #first node number
lnode_num=$4 #last node number
pad_num=$5 #charicters to pad

for hostnum in `seq -f”%0$5g” $3 $4`;
do
shorthostname=$host_str$hostnum
longhostname=$host_str$hostnum.$fqdn_str
printf “#Host Entry for Node $shorthostname.\n”
printf “define host{\n”
printf “\tuse\tbasic-host\n”
printf “\thost_name\t$shorthostname\n”
printf “\talias\t$shorthostname\n”
printf “\taddress\t$longhostname\n”
printf “\t}\n”
printf “\n”;
done
exit 0