I was at the office today and had to create more than 150 hosts and to create it one by one even with a text editor or WUIs, it can be very taxing. So being the lazy guy that i am, i created this script to generate as many hosts and as an addon, create hostgroup list so that you just need to copy and paste into the hostgroup definition.
This is to create the first of steps when defining Nagios hosts. It of course can be used to generate similar items if they are repeated.
Pre-requisite:
1) You need csvtool (apt-get install csvtool or yum)
So, in essence here are the steps
1) Create a CSV file (in excel or in text editor) with at least 3 fields, hostname, description and IP address. This is probably what your customer had given or your existing inventory.
Example:
MIS-M-3-ASW,MIS Admin Switch 3,192.168.199.103
Where, MIS-M-3-ASW is the hostname, MIS Admin Switch 3 is the description and of course 192.168.199.103 is that hosts IP address
2) Create a script as seen below #nano ngenerator.sh and make it executable #chmod +x ngenerator.sh
###START ###
#!/bin/bash
# [email protected]
#
hosttemplate=switches-host # nagios template to use
inputfile=master.csv # inputfile in csv format only
outputhost=hosts.txt # the output for hosts definition which you can copy paste into nagios
outputservices=services.txt # the output for services (all these hosts) definition which you can copy paste into nagios
columns=”1-3″ #define like 3-4 to read column 3-4 etc
#start check tool
testcsvtool=which csvtool
if [[ $? == “1” ]]; then
clear
echo “ERROR”
echo “No csvtool software found, do a ‘apt-get install csvtool’ or ‘yum install csvtool'”
exit 1
fi
#start script
listdata=csvtool -t ',' col $columns $inputfile
IFS=echo -en "nb"
echo “” > $outputhost
to add all this in hosts.cfg
# modify accordingly
for i in $listdata; do
#set these
field1=echo $i | cut -d "," -f 1
field2=echo $i | cut -d "," -f 2
field3=echo $i | cut -d "," -f 3
echo “” >> $outputhost
echo “# $field1” >> $outputhost
echo “#———————————————————————-” >> $outputhost
echo “” >> $outputhost
echo “define host{” >> $outputhost
echo ” use $hosttemplate” >> $outputhost
echo ” host_name $field1″ >> $outputhost
echo ” alias $field2″ >> $outputhost
echo ” display_name $field2″ >> $outputhost
echo ” address $field3″ >> $outputhost
echo “}” >> $outputhost
echo “#———————————————————————-” >> $outputhost
echo “” >> $outputhost
done
to add all this in services.cfg
echo “” > $outputservices
for i in $listdata; do
field1=echo $i | cut -d "," -f 1
echo -n “$field1,” >> $outputservices
done
exit 0
###END###
3) Define settings like the hosttemplate name you already created, inputfile file name (the csv file), the outputhost file name (will be the file you use to copy and paste into Nagios, outputservices file where you can use this that lists all the hosts that were imported from your CSV file (you can use e.g. to add to hostgroup or a minimum ping for instance) and the columns in the csv. If you have more than 3 values in your CSV you need to adjust this value and also the amount of fields you wish to import (field1, field2, field3 is currently defined).
4) Run the script and it will take the input csv file and generate your output files hosts.txt and services.txt
5) Paste into your actual nagios host configuration directly, restart nagios daemon.
You can of course define a lot of other stuff if you wish. Just get a little creative. But this script should already help you a lot a little with the initial script creation.
Good stuff! You just saved me hours. I amended it to add parents too. Thanks.