Adding Batch Hosts To op5 (Nagios) from CSV

Even with the lovely GUI that op5 comes with, sometimes you need to enter in a large set of hosts into the system.  op5 gives you the ability to use the GUI as well as the historical nagios (hosts.cfg) files.

I wanted a way to not only create a hosts file but include some other items as well like the parent of the device, hostgroups and  image to use.  The input file should be in csv (comma separated format).

So I grabbed some old code on the net that and edited to fit my needs.  There are 3 parts

  • hosts.skel
  • inputfile.csv

[cc escaped=”true” nowrap=”true” lang=”bash”]

# convert csv input file into hosts.cfg
cat $file |while read line;
echo “${line}”
NAME=`echo ${line}|cut -d, -f1`
ADDRESS=`echo ${line}|cut -d, -f2`
IMAGE=`echo ${line}|cut -d, -f3`
PARENTS=`echo ${line}|cut -d, -f4`
# Since hostgroups may contain spaces let’s put at end and grab everything from field 5 on
HOSTGROUPS=`echo ${line}|cut -d, -f5- | sed -s ‘s/”//g’`

cat hosts.skel | sed -e “s/TEMP_NAME/$NAME/” -e “s/TEMP_IP/$ADDRESS/” -e “s/TEMP_HOSTGROUPS/$HOSTGROUPS/” -e “s/TEMP_IMG/$IMAGE/” -e “s/TEMP_PARENTS/$PARENTS/” >>hosts.cfg



[cc escaped=”true” nowrap=”true” lang=”text”]
define host{
use default-host-template
host_name TEMP_NAME
address TEMP_IP
icon_image TEMP_IMG
statusmap_image TEMP_IMG


[cc escaped=”true” nowrap=”true” lang=”text”]
oracle-prod1,,redhat.png,ny-router-1,”All Servers, NY DataCenter, Everything, Prod Servers, Linux Servers”
oracle-prod2,,redhat.png,ny-router-1,”All Servers, NY DataCenter, Everything, Prod Servers, Linux Servers”
oracle-prod3,,redhat.png,ny-router-1,”All Servers, NY DataCenter, Everything, Prod Servers, Linux Servers”
win-exchange,,windows.png,ny-router-1,”All Servers, NY DataCenter, Everything, Prod Servers, Window Servers”

Your CSV file should use the following columns:

Name, IP, Image_Name, Parent_Name, HostGroup(s)

Once you have done this simply run the command (outside of your etc dir):

[cc escaped=”true” nowrap=”true” lang=”bash”]
./ inputfile.csv

Now look at the newly created file hosts.cfg

If everything looks good, simply cat >> this onto the end of the hosts.cfg file.  Within op5, goto config and it will notice you have a NEWER config.  Click save and all is well.  If not, go back and fix the errors.

BTW: You might want to create a backup within op5 before doing this just in case you lay down some wrong hosts.