Bulk Clone from Command line
Posted: Tue May 19, 2015 1:52 pm
Is there a way to run a bulk clone from a command line grabbing the new devices from a CSV?
Support for Nagios products and services
https://support.nagios.com/forum/
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.
Code: Select all
mkdir /tmp/automateCode: Select all
chmod +x /tmp/automate/automate.sh
cd /tmp/automate/Code: Select all
./automate <template> 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,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.sh