Nagios – check_service.sh

Nov 01
2008

Un petit script qui permet de surveiller la présence (ou non) d’un process sous Linux.

Le fichier paramètre ressemble à celui-ci.

check_services.sh

#!/bin/sh
#
# Check de daemon
#
# utilisation :
#  sh ./check_service.sh [ADRESSE_MACHINE]
#
# Necessaire :
# fichier parametre nomme [ADRESSE_MACHINE].cfg
#
#######################
# VARIABLES       #
#######################
ADRESSE_MACHINE=$1
CONF_FILE="/srv/scripts/$ADRESSE_MACHINE.cfg"
TMP_FILE="/tmp/service_$ADRESSE_MACHINE.tmp"
UTILISATEUR_LINUX=`whoami`
EXIT_I=0
MSG_TEXT=""
EXIT_CODE="0"
SERVICE_MAX="0"
SERVICE=""
NB_PROCESS=""
PROCESS_ERREUR="0"
#######################
# Avant de commencer, on verifie la  #
# presence du fichier de configuration #
#et les paramètres #
#######################
if [ ! -e "$CONF_FILE" ] ; then
echo "--- Fichier de configuration $CONF_FILE : Absent. ---"
EXIT_CODE="3"
exit $EXIT_CODE
fi
if [ ! "$1" ] ; then
echo "--- Parametre manquant ---"
EXIT_CODE="3"
exit $EXIT_CODE
fi
ssh nagios@$ADRESSE_MACHINE ps -e > $TMP_FILE
while read aLine ;do
LIGNE_SERVICE=`echo $aLine | grep "service=" |sed "s/service=//g"`
SERVICE=`echo $LIGNE_SERVICE | cut -d " " -f1`
PROCESS=`echo $LIGNE_SERVICE | cut -d " " -f2`
if [ "$SERVICE" != "" ] ; then
SERVICE_MAX=$((SERVICE_MAX+1))
NB_PROCESS=`grep -c -i $SERVICE $TMP_FILE`
fi
case $PROCESS in
0)
if [ "$NB_PROCESS" != "0" ] ; then
MSG_TEXT=$MSG_TEXT"\n\r$NB_PROCESS/0 $SERVICE"
PROCESS_ERREUR=$((PROCESS_ERREUR+1))
fi;;
1)
if [ "$NB_PROCESS" != "1" ] ; then
MSG_TEXT=$MSG_TEXT"\n\r$NB_PROCESS/1 $SERVICE"
PROCESS_ERREUR=$((PROCESS_ERREUR+1))
fi;;
2)
if [ "$NB_PROCESS" == "0" ] || [ "$NB_PROCESS" == "1" ] ; then
MSG_TEXT=$MSG_TEXT"\n\r$NB_PROCESS/X $SERVICE"
PROCESS_ERREUR=$((PROCESS_ERREUR+1))
fi;;
esac
SERVICE=""
NB_PROCESS=""
done < $CONF_FILE
if [ "$PROCESS_ERREUR" != "0" ] ; then
MSG_TEXT="$PROCESS_ERREUR / $SERVICE_MAX Process KO : "$MSG_TEXT
EXIT_CODE="2"
else
MSG_TEXT="$SERVICE_MAX Process OK"
EXIT_CODE="0"
fi
#rm $TMP_FILE
echo $MSG_TEXT | sed -e 's#\\n\\r#<br>#'
echo -e $MSG_TEXT
exit $EXIT_CODE

Comments are closed.