Issues with a Script for automation

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Issues with a Script for automation

Post 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?
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues with a Script for automation

Post 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
You do not have the required permissions to view the files attached to this post.
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Re: Issues with a Script for automation

Post 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.
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Re: Issues with a Script for automation

Post 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!
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Issues with a Script for automation

Post by ssax »

Glad it's working for you, marking this as resolved and locking the topic.
Locked