Exemple pratique de Centreon-Clapi
19/11/17 17:34 Classé dans: Techniques

syntaxe :
./show_hosts_services.sh -h
Usage: show_hosts_services.sh -u-p -d
This program show hosts with associated services
-u User Centreon.
-p Password Centreon
-d show disabled hosts/services
-h help
affichage des hôtes et services actif :
./show_hosts_services.sh -u admin -p password
affichage de tous les hôtes et services :
./show_hosts_services.sh -u admin -p password -d
Et voici le code
#!/bin/bash
# show_hosts_services.sh
# version 1.00
# date 19/11/2017
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} -u-p -d
This program show hosts with associated services
-u User Centreon.
-p Password Centreon
-d show disabled hosts/services
-h help
EOF
exit 2
}
SHOW_DISABLED=0
while getopts 'u:p:dh' OPTION
do
case $OPTION in
u)
USER_CENTREON="$OPTARG"
;;
p)
PWD_CENTREON="$OPTARG"
;;
d) SHOW_DISABLED=1
;;
?|h) show_help
;;
esac
done
shift $(($OPTIND - 1))
# Check for missing parameters
if [ -z "${USER_CENTREON}" ] || [ -z "${PWD_CENTREON}" ]; then
echo "Missing parameters!"
show_help
fi
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
TAIL=/usr/bin/tail
CLAPI=/usr/share/centreon/bin/centreon
# read host
function read_host ()
{
$CLAPI -u $USER_CENTREON -p $PWD_CENTREON -o HOST -a SHOW | $TAIL -n+2 |
#lecture param
while read line
do
ID_HOST=`echo $line | cut -d ";" -f1 `
NAME_HOST=`echo $line | cut -d ";" -f2 `
ACTIVATE_HOST=`echo $line | cut -d ";" -f5 `
if [ "$ACTIVATE_HOST" == "0" ] && [ "$SHOW_DISABLED" == "1" ]; then
echo -e "$RED""Hosts Disabled : "$NAME_HOST"$NORMAL"
else
if [ "$ACTIVATE_HOST" == "1" ]; then
echo "Hosts : "$NAME_HOST
fi
fi
if [ "$ACTIVATE_HOST" == "1" ] || [ "$SHOW_DISABLED" == "1" ]; then
read_service $ID_HOST $ACTIVATE_HOST
fi
done
}
# read service
function read_service ()
{
$CLAPI -u $USER_CENTREON -p $PWD_CENTREON -o SERVICE -a SHOW | $TAIL -n+2 |
#lecture param
while read line
do
ID_HOST=`echo $line | cut -d";" -f1 `
if [ "$1" == "$ID_HOST" ]; then
NAME_SERVICE=`echo $line | cut -d ";" -f4 `
ACTIVATE_SERVICE=`echo $line | cut -d ";" -f12 `
if ([ "$ACTIVATE_SERVICE" == "0" ] || [ "$2" == "0" ]) && [ "$SHOW_DISABLED" == "1" ]; then
echo -e "$RED"" Service : "$NAME_SERVICE - disabled"$NORMAL"
else
if [ "$ACTIVATE_SERVICE" == "1" ]; then
echo " Service : "$NAME_SERVICE
fi
fi
fi
done
}
read_host
Et voici un aperçu du résultat

Bon scripting !
blog comments powered by Disqus