Adding hosts to Nagios can sometimes be a pain, so this should help.
This script takes input from a .csv file that can be made in a program such as Microsoft Excel. This is useful for adding a large number of hosts at a time.
First, create the main script, make sure this is not in the same directory as your production Nagios configuration, as it will overwrite any existing hosts.cfg file in the same directory:
nagios_import.sh
#!/bin/sh # nagios_import.sh file=$1 cat $file | while read line; do echo "${line}" NAME=`echo ${line} | cut -d\; -f1` ALIAS=`echo ${line} | cut -d\; -f2` DISNAME=`echo ${line} | cut -d\; -f3` IP=`echo ${line} | cut -d\; -f4` GROUP=`echo ${line} | cut -d\; -f5` CHECK=`echo ${line} | cut -d\; -f6` CONTACT=`echo ${line} | cut -d\; -f7` cat hosts.template | sed -e "s/TEMP_NAME/$NAME/" -e "s/TEMP_ALIAS/$ALIAS/" -e "s/TEMP_DISNAME/$DISNAME/" -e "s/TEMP_IP/$IP/" -e "s/TEMP_GROUP/$GROUP/" -e "s/TEMP_CHECK/$CHECK/" -e "s/TEMP_CONTACT/$CONTACT/" >> hosts.cfg done
We also need a template for our output file:
hosts.template
############################################################################### define host { host_name TEMP_NAME alias TEMP_ALIAS display_name TEMP_DISNAME address TEMP_IP hostgroups TEMP_GROUP check_command TEMP_CHECK initial_state o max_check_attempts 3 check_interval 5 retry_interval 1 active_checks_enabled 1 passive_checks_enabled 0 check_period 24x7 check_freshness 1 process_perf_data 1 contact_groups TEMP_CONTACT notification_interval 15 notification_period 24x7 notification_options d,u,r notifications_enabled 1 register 1 } ###############################################################################
.
input.csv (Example)
NAME;ALIAS;DISNAME;IP;HOSTGROUP;CHECK;CONTACT ALMBH02;Altiris, div. beheer tools;ALMBH02;172.20.4.244;;check-host-alive;Standby_pool2 ALMSANS01;Storageworks 8/8 SANswitch;ALMSANS01;172.20.4.13;;check-host-alive;Standby_pool2 ALMSANS02;Storageworks 8/8 SANswitch;ALMSANS02;172.20.4.14;;check-host-alive;Standby_pool2
Now we can add our hosts in a spreadsheet and generate the hosts.cfg file simply by running:
./nagios_import.sh input.csv
This will parse the information from your input.csv file and write it to the hosts.cfg file based on our hosts.template file.
This data can then be imported to Nagios by copying the file (hosts.cfg in the current dirtectory) to the “/etc/nagios/” directory and importing the file using NagiosQL: