Bulk Clone from Command line
-
peter.zanetti
- Posts: 90
- Joined: Wed Oct 01, 2014 8:34 am
Bulk Clone from Command line
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
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.
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
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
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.
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
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.
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
Reason: Moved post to XI section, out of Core
Re: Bulk Clone from Command line
How is this file formatted, exactly? If we saw the formatting, we should be able to help you along here.We are pulling an up to date file from our centralized database and we would like to use that file for the bulk upload.
-
peter.zanetti
- Posts: 90
- Joined: Wed Oct 01, 2014 8:34 am
Re: Bulk Clone from Command line
We've got it formatted as:
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
....
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
Name, Address, Description, Hostgroup,
....
Re: Bulk Clone from Command line
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:
Copy each of the files below files to the /tmp/automate folderand then run:
Syntax:
To run this scratch:
automate.sh:
automate.template:
automate.csv:
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.
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/automateCode: Select all
chmod +x /tmp/automate/automate.sh
cd /tmp/automate/Code: Select all
./automate <template> To run this scratch:
Code: Select all
./automate.sh automate.templateCode: 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
Code: Select all
define host {
host_name $hostname
alias $alias
use xiwizard_generic_host
address $address
hostgroups $hostgroups
register 1
}Code: Select all
Name, Address, Description, Hostgroup,
Name1, Address1, Description1, Hostgroup1,
Name2, Address2, Description2, Hostgroup2,
Name3, Address3, Description3, Hostgroup3,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.
"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.
Re: Bulk Clone from Command line
As an alternative, you can try the following:
1. Create a csv file that you would like to use in the following format:
2. Create a template with a host/service check and name it "base-template.cfg", i.e.
3. Create a bash script, named "cfg-generator.sh", i.e.
Note: Place all three files in the "/usr/local/nagiosxi/scripts" directory and make the script executable:
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).
Hope this helps.
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, Hostgroup3Code: 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%
}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.shCode: Select all
chmod +x /usr/local/nagiosxi/scripts/cfg-generator.shCode: Select all
crontab -e
05 0 * * * /usr/local/nagiosxi/scripts/cfg-generator.shBe 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
Thanks! Will let you know how it goes!