Page 1 of 1

Issues with a Script for automation

Posted: Mon Jun 15, 2015 1:35 pm
by peter.zanetti
I had recieved some help and a script for a previous thread about the automation of adding new hosts through the command line. Said thread has been linked below.

http://support.nagios.com/forum/viewtop ... 16&t=32968

I just tried to add more than one host from the CSV file and the script ran and added the first device but then it stopped and did not continue onto the second line.

Here is the script:

Code: Select all

#!/bin/bash

/bin/cat  /tmp/new-hosts.csv | while read LINE
do
        Name=`/bin/echo $LINE | /bin/cut -d, -f1`
        Address=`/bin/echo $LINE | /bin/cut -d, -f2`
        Description=`/bin/echo $LINE | /bin/cut -d, -f3`
        Hostgroup1=`/bin/echo $LINE | /bin/cut -d, -f4`
        Hostgroup2=`/bin/echo $LINE | /bin/cut -d, -f5`
        sed -e "s/AAAA/$Name/g; s/BBBB/$Address/g; s/CCCC/$Description/g; s/DDDD/$Hostgroup1/g; s/EEEE/$Hostgroup2/g" /usr/local/nagiosxi/scripts/base-template.cfg > /usr/local/nagios/etc/import/$N
ame.cfg
done
cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.sh
Here is the Template:

Code: Select all

# Original (base) template

define host{
        use                             xiwizard_windowsdesktop_host
        host_name                       AAAA
        address                         BBBB
        alias                           CCCC
        hostgroups                      DDDD,EEEE
        check_command                   check_tcp_445!!!!!!!!
        max_check_attempts              5
        check_interval                  15
        retry_interval                  1
        check_period                    xi_timeperiod_24x7
        notification_interval           60
        notification_period             xi_timeperiod_24x7
        icon_image                      windowsxp.png
        statusmap_image                 windowsxp.png
        _xiwizard                       windowsdesktop
        register                        1
        }
And here is the CSV:

Code: Select all

K4285572,K4285572,Test,LabDISDevices,DIS_RET_Servers,
K4283696,K4263696,Test,LabDISDevices,DIS_RET_Servers,

Is there a way to get this script to run through the whole CSV amd add multiple hosts?

Re: Issues with a Script for automation

Posted: Mon Jun 15, 2015 2:12 pm
by ssax
It's working fine for me with your data (except your script has a mistake in it, ame.cfg should not on it's own line, it's part of the previous command) though I suspect it's just a copy/paste error.

I'm wondering if you're having issues with your line endings again, please use the attached file and try again (don't try modifying it yet, just use it for this test).
new-hosts.zip

Re: Issues with a Script for automation

Posted: Mon Jun 15, 2015 2:28 pm
by peter.zanetti
It worked with your CSV file. Strange. How are you creating your CSV and I will try to create one that way and see what happens.

Re: Issues with a Script for automation

Posted: Mon Jun 15, 2015 2:30 pm
by peter.zanetti
Actually I just realized the problem, In my test I was editing a CSV in /usr/local/nagiosxi/scripts which had two hosts but the script was pointing at /tmp which had a new-hosts.csv with only one host value. Dumb mistake. Thanks for the help though!

Re: Issues with a Script for automation

Posted: Mon Jun 15, 2015 2:47 pm
by ssax
Glad it's working for you, marking this as resolved and locking the topic.