Page 1 of 4

Bulk Clone from Command line

Posted: Tue May 19, 2015 1:52 pm
by peter.zanetti
Is there a way to run a bulk clone from a command line grabbing the new devices from a CSV?

Re: Bulk Clone from Command line

Posted: Tue May 19, 2015 1:54 pm
by tmcdonald
There's certainly a way, but we don't have any script specifically for this.

I would create a base config file, then wherever you are going to replace data put "PLACEHOLDER_ADDRESS" or whatever you are replacing and use sed to do it.

Shouldn't be more than 50 lines tops for a bash script.

Re: Bulk Clone from Command line

Posted: Wed May 20, 2015 2:05 pm
by peter.zanetti
I'm fairly new to the IT world and don't have alot of experience with command line or scripting so I'm not to sure what you mean exactly.

Re: Bulk Clone from Command line

Posted: Wed May 20, 2015 3:49 pm
by lmiltchev
I found the following script on the Nagios Exchange:

https://exchange.nagios.org/directory/C ... es/details

I haven't tried it, so I am not sure if it works. There are no reviews, either. You can however give it a try to see if this is going to get the job done.

Re: Bulk Clone from Command line

Posted: Fri May 22, 2015 8:09 am
by peter.zanetti
Currently we are manually running the bulk clone through the GUI of Nagios XI. We are trying to automate the process of adding new devices to Nagios for monitoring.

We would like to do this every night without human intervention. We are pulling an up to date file from our centralized database and we would like to use that file for the bulk upload.

Re: Bulk Clone from Command line

Posted: Fri May 22, 2015 1:14 pm
by jolson
We are pulling an up to date file from our centralized database and we would like to use that file for the bulk upload.
How is this file formatted, exactly? If we saw the formatting, we should be able to help you along here.

Re: Bulk Clone from Command line

Posted: Tue May 26, 2015 10:04 am
by peter.zanetti
We've got it formatted as:
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
....

Re: Bulk Clone from Command line

Posted: Tue May 26, 2015 1:01 pm
by abrist
Alright, I whipped up a scratch script quick. You will need to adjust this script as you see fit. A couple of notes:
1. It makes use of 2 files, the script itself and the template to use.
2. The csv below is just for my testing. You should flesh out the get_data() function in the shell script to fetch your csv.
3. The template can be altered, but you should be aware that it needs to remain valid (other than the $vars).
4. Also, the template gets evaluated line by line, so if you ever send more vars in your csv, you will need to edit the AWKs, local vars, and the template.
5. This assumes only one hostgroup. If you pass multiple hostgroups for each host, you will have to alter the AWKs.
6. I used AWK, though I guess I could have used cut or even a bash array. Whatever. I think you will get the idea.

Overview:

The script will read in the csv data, and then iterate through each line, loading the values into local vars. It will then eval the $TEMPLATE, replacing the vars with values and dumping them to the $OUTPUT file. One this has been performed for all lines in the csv, it will then prepare the files for import, copy them to the prepared files to the XI import folder, and then run ./reconfigure_nagios.sh.

Preparation:

Code: Select all

mkdir /tmp/automate
Copy each of the files below files to the /tmp/automate folderand then run:

Code: Select all

chmod +x /tmp/automate/automate.sh
cd /tmp/automate/
Syntax:

Code: Select all

./automate <template> 

To run this scratch:

Code: Select all

./automate.sh automate.template
automate.sh:

Code: Select all

#!/bin/bash
TEMPLATE="$1"
TEMP="/tmp/automate/tmp_automate"
OUTPUT="/tmp/automate/output.cfg"

function import_cfg() {
  cd $TEMP
  /usr/local/nagiosxi/tools/xiprepimport.php $OUTPUT
  cp $TEMP/* /usr/local/nagios/etc/import
  cd /usr/local/nagiosxi/scripts/
  ./reconfigure_nagios.sh
}

function parse_line() {
  local line="$1"
  local hostname=`echo $line | awk 'BEGIN { FS = "," } ; { print $1 }'`
  local address=`echo $line | awk 'BEGIN { FS = "," } ; { print $2 }'`
  local alias=`echo $line | awk 'BEGIN { FS = "," } ; { print $3 }'`
  local hostgroups=`echo $line | awk 'BEGIN { FS = "," } ; { print $4 }'`

  while read line; do
    eval echo "$line" >> $OUTPUT
  done < $TEMPLATE
}

function prepare_stuff(){
  #empty output file if it exists
  echo "" > $OUTPUT
  #create temp directory if it does not exist
  mkdir -p $TEMP

}

function cleanup_stuff() {
  rm -fr $TEMP
  rm -f $OUTPUT
  #add another line here to remove the fetched csv file
}

function get_data() {
  #add your code here to get the csvdata from the server
  #save the csv to a file and load the path to the file in the $HOSTLIST var
  #we will just use a static file for now:

  HOSTLIST="/tmp/automate/automate.csv"
}

function read_data(){
  while read line; do
    parse_line "$line"
  done < $HOSTLIST
}

function main() {
  prepare_stuff
  get_data
  read_data
  import_cfg
  cleanup_stuff
}

main
automate.template:

Code: Select all

define host {
  host_name      $hostname
  alias          $alias
  use            xiwizard_generic_host
  address        $address
  hostgroups     $hostgroups
  register       1
}
automate.csv:

Code: Select all

Name, Address, Description, Hostgroup,
Name1, Address1, Description1, Hostgroup1,
Name2, Address2, Description2, Hostgroup2,
Name3, Address3, Description3, Hostgroup3,
Please take caution and test/rework/flesh this out in a TEST ENVIRONMENT. Tis' but a scratch and was minimally tested. You should be able to get started on this project with what I have posted above as long as you have a competent scripter on staff.

Re: Bulk Clone from Command line

Posted: Tue May 26, 2015 1:44 pm
by lmiltchev
As an alternative, you can try the following:

1. Create a csv file that you would like to use in the following format:

Code: Select all

Name1, Address1, Description1, Hostgroup1
Name2, Address2, Description2, Hostgroup2
Name3, Address3, Description3, Hostgroup3
2. Create a template with a host/service check and name it "base-template.cfg", i.e.

Code: Select all

# Original (base) template

define host{
        use                     xiwizard_generic_host
        host_name               AAAA
        address                 BBBB
        alias                   CCCC
        hostgroups                              DDDD
        }


define service{
        use                             xiwizard_generic_service
        host_name                       AAAA
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }
3. Create a bash script, named "cfg-generator.sh", i.e.

Code: Select all

#!/bin/bash

/bin/cat  /usr/local/nagiosxi/scripts/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`
        Hostgroup=`/bin/echo $LINE | /bin/cut -d, -f4`
        sed -e "s/AAAA/$Name/g; s/BBBB/$Address/g; s/CCCC/$Description/g; s/DDDD/$Hostgroup/g" /usr/local/nagiosxi/scripts/base-template.cfg > /usr/local/nagios/etc/import/$Name.cfg
done
cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.sh
Note: Place all three files in the "/usr/local/nagiosxi/scripts" directory and make the script executable:

Code: Select all

chmod +x /usr/local/nagiosxi/scripts/cfg-generator.sh
It's a good idea to test the script with a few hosts/services to start with. Once you make sure it works fine, you can set a cron job to run once a day (for example 5 min after midnight every day).

Code: Select all

crontab -e
05 0 * * * /usr/local/nagiosxi/scripts/cfg-generator.sh
Hope this helps.

Re: Bulk Clone from Command line

Posted: Thu May 28, 2015 8:30 am
by peter.zanetti
Thanks! Will let you know how it goes!