Bulk Clone from Command line

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

Bulk Clone from Command line

Post by peter.zanetti »

Is there a way to run a bulk clone from a command line grabbing the new devices from a CSV?
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Bulk Clone from Command line

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

Re: Bulk Clone from Command line

Post 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.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Bulk Clone from Command line

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Re: Bulk Clone from Command line

Post 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.
Last edited by tmcdonald on Fri May 22, 2015 9:03 am, edited 1 time in total.
Reason: Moved post to XI section, out of Core
jolson
Attack Rabbit
Posts: 2560
Joined: Thu Feb 12, 2015 12:40 pm

Re: Bulk Clone from Command line

Post 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.
Twits Blog
Show me a man who lives alone and has a perpetually clean kitchen, and 8 times out of 9 I'll show you a man with detestable spiritual qualities.
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Re: Bulk Clone from Command line

Post 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,
....
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Bulk Clone from Command line

Post 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.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Bulk Clone from Command line

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
peter.zanetti
Posts: 90
Joined: Wed Oct 01, 2014 8:34 am

Re: Bulk Clone from Command line

Post by peter.zanetti »

Thanks! Will let you know how it goes!
Locked