Débutant Shell
tatane42
-
jisisv Messages postés 3678 Statut Modérateur -
jisisv Messages postés 3678 Statut Modérateur -
Bonjour,
J'ai installé une plateforme Nagios/Centreon depuis un moment jusqu'a maintenant je me servais de plugin déjà écrit.
Ne pouvant plus faire exactement ce que je souhaiteje vaux créer mes plugins moi même.
J'ai écrit un bout de code mais je ne connais pas assez de commande qui pourrait me faciliter la tache
mon code
#!/bin/bash
pluginpath="/usr/local/nagios/libexec"
pluginname=`basename $0`
while getopts "H:C:Q:w:c:" options; do
case $options in
H)hostname=$OPTARG;;
C)community=$OPTARG;;
Q)query=$OPTARG;;
w)warn=$OPTARG;;
c)crit=$OPTARG;;
*)
echo "$pluginname Help:"
echo "-----------------"
echo "-H <Hostname> : Hostname/IP"
echo "-C <communty> : SNMP Community. Default: public"
echo "-Q query : the part to query:"
echo " ddraid1/2/3/ "
echo " "
echo " "
echo "Usage: $pluginname -H <HOSTADDRESS> -C <community> -Q "
exit 3
;;
esac
done
if [ -z $community ]; then
community=public;
fi;
SNMPWALK_BIN='/usr/bin/snmpwalk'
DD1='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.1'
DD2='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.2'
DD3='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.3'
DD4='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.4'
case $query in
ddraid1)
ETAT1=`$SNMPWALK_BIN -v 1 -c $community -On $hostname $DD1 | awk '{print $4}' | tr -d '\n'`;
if [ $ETAT1 -eq 3 ]; then
echo " OK: En ligne "
exit 0
fi;
if [ $ETAT1 -eq 1 ]; then
echo " WARNING: Prêt "
exit 1
fi;
if [ $ETAT1 -eq 2 ]; then
echo " CRITICAL: En panne "
exit 2
fi;
if [ $ETAT1 -eq 4 ]; then
echo " CRITICAL: Hors Ligne "
exit 2
fi;
if [ $ETAT1 -eq 6 ]; then
echo " CRITICAL: Dégradé "
exit 2
fi;
if [ $ETAT1 -eq 7 ]; then
echo " CRITICAL: Récupération "
exit 2
fi;
if [ $ETAT1 -eq 11 ]; then
echo " CRITICAL: Retirer "
exit 2
fi;
if [ $ETAT1 -eq 15 ]; then
echo " CRITICAL: Resynchronisation "
exit 2
fi;
if [ $ETAT1 -eq 24 ]; then
echo " CRITICAL: Reconstruction "
exit 2
fi;
if [ $ETAT1 -eq 25 ]; then
echo " WARNING: Emplacement Vide "
exit 1
fi;
if [ $ETAT1 -eq 26 ]; then
echo " WARNING: Formattage "
exit 1
fi;
if [ $ETAT1 -eq 28 ]; then
echo " WARNING: Diagnostique "
exit 1
fi;
if [ $ETAT1 -eq 35 ]; then
echo " WARNING: Initialisation "
exit 1
fi;
echo " UNKNOWN: Inconnu "
exit 3
;;
*)
echo "UNKNOWN: not implemented. Use $pluginname -h"
exit 3
;;
esac
Ce plugin me permet de contrôler mes disque Raid sur mes serveurs.
Je sais ce que vous allez dire c'est le bordel mais je ne voulais pas me perdre dans les sinon en plus ça marche impec comme ça.
Donc ma question: est ce qu'il y a une commande qui pourrait me permettre lorsque j'ai récupéreé la valeur qui m'interesse d'assigner directement un commantaire et une valeur de sortie sans que j'ai besoin à chaque foi de faire un si pour comparer chaque valeur. Cela me permetterait également d'ajouter la valeur et le texte dans une variable
Merci pour votre aide
J'ai installé une plateforme Nagios/Centreon depuis un moment jusqu'a maintenant je me servais de plugin déjà écrit.
Ne pouvant plus faire exactement ce que je souhaiteje vaux créer mes plugins moi même.
J'ai écrit un bout de code mais je ne connais pas assez de commande qui pourrait me faciliter la tache
mon code
#!/bin/bash
pluginpath="/usr/local/nagios/libexec"
pluginname=`basename $0`
while getopts "H:C:Q:w:c:" options; do
case $options in
H)hostname=$OPTARG;;
C)community=$OPTARG;;
Q)query=$OPTARG;;
w)warn=$OPTARG;;
c)crit=$OPTARG;;
*)
echo "$pluginname Help:"
echo "-----------------"
echo "-H <Hostname> : Hostname/IP"
echo "-C <communty> : SNMP Community. Default: public"
echo "-Q query : the part to query:"
echo " ddraid1/2/3/ "
echo " "
echo " "
echo "Usage: $pluginname -H <HOSTADDRESS> -C <community> -Q "
exit 3
;;
esac
done
if [ -z $community ]; then
community=public;
fi;
SNMPWALK_BIN='/usr/bin/snmpwalk'
DD1='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.1'
DD2='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.2'
DD3='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.3'
DD4='1.3.6.1.4.1.674.10893.1.20.130.4.1.4.4'
case $query in
ddraid1)
ETAT1=`$SNMPWALK_BIN -v 1 -c $community -On $hostname $DD1 | awk '{print $4}' | tr -d '\n'`;
if [ $ETAT1 -eq 3 ]; then
echo " OK: En ligne "
exit 0
fi;
if [ $ETAT1 -eq 1 ]; then
echo " WARNING: Prêt "
exit 1
fi;
if [ $ETAT1 -eq 2 ]; then
echo " CRITICAL: En panne "
exit 2
fi;
if [ $ETAT1 -eq 4 ]; then
echo " CRITICAL: Hors Ligne "
exit 2
fi;
if [ $ETAT1 -eq 6 ]; then
echo " CRITICAL: Dégradé "
exit 2
fi;
if [ $ETAT1 -eq 7 ]; then
echo " CRITICAL: Récupération "
exit 2
fi;
if [ $ETAT1 -eq 11 ]; then
echo " CRITICAL: Retirer "
exit 2
fi;
if [ $ETAT1 -eq 15 ]; then
echo " CRITICAL: Resynchronisation "
exit 2
fi;
if [ $ETAT1 -eq 24 ]; then
echo " CRITICAL: Reconstruction "
exit 2
fi;
if [ $ETAT1 -eq 25 ]; then
echo " WARNING: Emplacement Vide "
exit 1
fi;
if [ $ETAT1 -eq 26 ]; then
echo " WARNING: Formattage "
exit 1
fi;
if [ $ETAT1 -eq 28 ]; then
echo " WARNING: Diagnostique "
exit 1
fi;
if [ $ETAT1 -eq 35 ]; then
echo " WARNING: Initialisation "
exit 1
fi;
echo " UNKNOWN: Inconnu "
exit 3
;;
*)
echo "UNKNOWN: not implemented. Use $pluginname -h"
exit 3
;;
esac
Ce plugin me permet de contrôler mes disque Raid sur mes serveurs.
Je sais ce que vous allez dire c'est le bordel mais je ne voulais pas me perdre dans les sinon en plus ça marche impec comme ça.
Donc ma question: est ce qu'il y a une commande qui pourrait me permettre lorsque j'ai récupéreé la valeur qui m'interesse d'assigner directement un commantaire et une valeur de sortie sans que j'ai besoin à chaque foi de faire un si pour comparer chaque valeur. Cela me permetterait également d'ajouter la valeur et le texte dans une variable
Merci pour votre aide
A voir également:
- Débutant Shell
- Classic shell - Télécharger - Personnalisation
- Logiciel de programmation pour débutant - Guide
- Logiciel montage vidéo débutant - Guide
- Classic shell c'est quoi ✓ - Forum Windows 10
- Shell addition ✓ - Forum Shell
2 réponses
Salut,
J'ai pas trop compris ce que tu voulais optimiser, mais plutôt que de faire un :
J'ai pas trop compris ce que tu voulais optimiser, mais plutôt que de faire un :
case $query in
ddraid1) blabla
;;
*) blabla
;;
esacJe verrai plus un :
if [ "$query" = "ddraid1" ]
then
ETAT1=$(${SNMPWALK_BIN} -v 1 -c ${community} -On ${hostname} ${DD1} | awk '{print $4}' | tr -d '\n')
case "$ETAT1" in
3) echo "blabla"
;;
1) echo "blabla"
;;
2) echo "blabla"
;;
etc.
esac
else
blabla
fiNon ?
Au risque de ne pas saisir ton problème, porquoi ne pas utiliser un case ?
johand@osiris:~/tmp$ cat mestests.sh
#! /bin/sh
function dispatch()
{
theopt=$1;
shift;
case $theopt in
"a"|"b")
echo $theopt;
return 1;;
"c")
echo $theopt;
return 2;;
*)
echo $theopt
return 255
esac
}
param=${1-missing}
dispatch $param
johand@osiris:~/tmp$ val=$(./mestests.sh a) ; echo $? ; echo $val
1
a
johand@osiris:~/tmp$ val=$(./mestests.sh c) ; echo $? ; echo $val
2
c
johand@osiris:~/tmp$ val=$(./mestests.sh ) ; echo $? ; echo $val
255
missing