Script to add a host with the centreon command line api
#!/bin/bash -e
#
# centreon_add_host
#
# Quickly add a host to centreon
#
#
# Author: Jean-Louis Girard
#
#
clapi="/usr/share/centreon/www/modules/centreon-clapi/core/centreon"
#Edit the following 4 fields
centreonAdmin=""
centreonTemplate=""
centreonPoller=""
centreonGroup=""
HNAME="NULL"
HALIAS="NULL"
HIP="NULL"
ADMINP="NULL"
HFILE="NULL"
addedHost="FALSE"
#This program must be run as root
if [[ "$(whoami)" != "root" ]]
then
echo "Must be root to execute ..."
exit
fi
usage()
{
echo "Usage: `basename $0` -h -a -i "
echo " `basename $0` -f "
echo "-p "
echo "If -p is not specified it will be entered during execution."
echo "File format: Each line contains ,, each field separated by a ,"
}
while getopts h:a:i:f:p: OPTION
do
case $OPTION in
h)
HNAME=$OPTARG
;;
a)
HALIAS=$OPTARG
;;
i)
HIP=$OPTARG
;;
f)
HFILE=$OPTARG
;;
p)
ADMINP=$OPTARG
;;
?)
usage
exit
;;
esac
done
#Ask for password until valid
while ! $clapi -u $centreonAdmin -p $ADMINP -a POLLERLIST > /dev/null
do
read -s -p "Admin Password: " ADMINP
echo ""
done
function addHost
{
#The function must receive at least 3 parameters Host IP, HostName, Host Alias
if [[ $# -ne 3 ]]
then
echo "Missing information to add $1 $2 $3"
exit
fi
F_HIP=$1
F_HNAME=$2
F_HALIAS=$3
# Only add the host if there is a valid dns entry
if nslookup $F_HIP 2>/dev/null | grep $F_HNAME > /dev/null 2>&1
then
echo "Adding $F_HIP $F_HNAME $F_HALIAS..."
# add the server to centreon and only apply the template if successful ( host doesn't already exist on centreon )
if $clapi -u $centreonAdmin -p $ADMINP -o HOST -a ADD -v "$F_HNAME;$F_HALIAS;$F_HIP;$centreonTemplate;$centreonPoller;$centreonGroup"
then
echo "Applying template $centreonTemplate to $F_HNAME"
$clapi -u $centreonAdmin -p $ADMINP -o HOST -a applytpl -v "$F_HNAME"
addedHost="TRUE"
fi
else
echo "The DNS entry for the ip [[ $F_HIP ]] does not match the name [[ $F_HNAME ]]. Not adding to centreon..."
fi
}
#Verify that only a host or a file is passed as arguments and not both
if [[ "$HIP" != "NULL" ]] || [[ "$HNAME" != "NULL" ]] || [[ "$HALIAS" != "NULL" ]] && [[ "$HFILE" != "NULL" ]]
then
echo "Please only specify a host's information or a file, not both."
exit
fi
#add a host if the ip hostname and host alias were passed as arguments
if [[ "$HIP" != "NULL" ]] && [[ "$HNAME" != "NULL" ]] && [[ "$HALIAS" != "NULL" ]]
then
addHost "$HIP" "$HNAME" "$HALIAS"
#if a host file argument was passed and the file exist then add the hosts found in the file.
elif [[ "$HFILE" != "NULL" ]]
then
if [[ -f $HFILE ]]
then
while read line
do
#Get each variables delimited by a , and remove any leading and trailing spaces
HIP="$(echo $line | cut -d ',' -f 1 | sed -e 's/^ *//' -e 's/ *$//')"
HNAME="$(echo $line | cut -d ',' -f 2 | sed -e 's/^ *//' -e 's/ *$//')"
HALIAS="$(echo $line | cut -d ',' -f 3 | sed -e 's/^ *//' -e 's/ *$//')"
addHost "$HIP" "$HNAME" "$HALIAS"
done < $HFILE
else
echo "ERROR: File $HFILE does not exist."
fi
else
usage
exit
fi
# if a new host was successfully added then Generate, move the configuration file and restart the poller
if [[ "$addedHost" == "TRUE" ]]
then
echo "Generating configs ..."
$clapi -u $centreonAdmin -p $ADMINP -a POLLERGENERATE -v 1 2>&1 | grep -v "date"
echo "Moving configs ..."
$clapi -u $centreonAdmin -p $ADMINP -a CFGMOVE -v 1
echo "Restarting poller ... "
$clapi -u $centreonAdmin -p $ADMINP -a POLLERRESTART -v 1
fi