Création de macros personnalisées avec Clapi
19/02/15 16:56 Classé dans: Techniques

Voici le mode opératoire :
Nous voulons désactiver la macro $_SERVICEWARNING$ avec une commande CLAPI, il faut donc récupérer son ID. Affichons les macros :
./centreon -u admin -p password -o RESOURCECFG -a show
id;name;value;comment;activate;instance
1;$USER1$;/usr/local/plugins/libexec;path to the plugins;1;Central
2;$USER3$;/usr/local/centreon-connector;path to the connectors;1;Central
4;$_HOSTSNMPVERSION$;public;Custom Macro;0;Central
5;$_HOSTSNMPCOMMUNITY$;2c;Custom Macro;0;Central
8;$_SERVICEWARNING$;0;Custom Macro;0;Central
9;$_SERVICECRITICAL$;0;Custom Macro;0;Central
L'ID pour $_SERVICEWARNING$ est le 8, nous utiliserons la commande suivante pour désactiver la ressource :
./centreon -u admin -p password -o RESOURCECFG -a setparam -v "8;activate;0"
Pas très pratique, me direz-vous ? Je vous propose donc la solution suivante, un script bash qui réalisera l'opération en une seule fois.
MAJ 11/04/2014 : Le précédent script avait un bug avec des noms de préfixe identique comme par exemple $_SERVICEMODE$ et $_SERVICEMODE_MYSQL$. Le script ne faisait pas la distinction entre ces deux chaines et donc ne désactivait pas la macro lots de l'exécution du script. Je vous propose cette version corrigée.
MAJ 19/02/2015 : amélioration du code, préconisation bonne pratique du bash
#!/bin/bash
# create_macrocustom.sh
# version 1.2
# date 19/02/2015
# use Centreon_Clapi
# $1 name of RESOURCES
# $2 name of COMMUNITY SNMP
# $3 name of Poller
# $4 Description
CLAPI_DIR=/usr/local/centreon/www/modules/centreon-clapi/core
USER_CENTREON=admin
PWD_CENTREON=password
# create resource
RESULT=$($CLAPI_DIR/centreon -u $USER_CENTREON -p $PWD_CENTREON -o RESOURCECFG -a add -v "$1;$2;$3;$4")
# control result
if [ -n "$RESULT" ]
then
echo "Erreur: "$RESULT
else
# retrieve id
CHAINE="\$$(echo $1 | tr 'a-z' 'A-Z')\$"
$CLAPI_DIR/centreon -u $USER_CENTREON -p $PWD_CENTREON -o RESOURCECFG -a show -v |
while read line
do
ID_NAME=$(echo $line | cut -d";" -f2 )
ID=$(echo $line | cut -d";" -f1 )
if [ "$CHAINE" == "$ID_NAME" ]
then
## desactivate resource
RESULT=$($CLAPI_DIR/centreon -u $USER_CENTREON -p $PWD_CENTREON -o RESOURCECFG -a setparam -v "$ID;activate;0")
if [ -n "$RESULT" ]
then
echo "Erreur desactivate : "$RESULT
else
echo "Create resource $1 Ok"
fi
fi
done
fi
Nous pouvons maintenant créer nos macros personnalisées désactivées par défaut. Nous n'encadrons pas nos macros de $, la commande CLAPI s'en chargera.
./create_macrocustom.sh _HOSTSNMPVERSION public Central "Custom Macro"
./create_macrocustom.sh _HOSTSNMPCOMMUNITY 2c Central "Custom Macro"
./create_macrocustom.sh _SERVICEWARNING 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICECRITICAL 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEPACKETNUMBER 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEPROCESS 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEINTERFACE 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEPASSWORD 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEUSERNAME 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEMODE_MYSQL 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEPORT 0 Central "Custom Macro"
./create_macrocustom.sh _SERVICEDISK 0 Central "Custom Macro"
Ce script peut être exécuté de n'importe quel dossier, car le chemin de la commande CLAPI est enregistré dans le script.

Voici le résultat.
blog comments powered by Disqus