185 lines
5.3 KiB
Bash
185 lines
5.3 KiB
Bash
|
#!/bin/sh
|
||
|
|
||
|
# Note needed in this version of the script
|
||
|
#set -e
|
||
|
|
||
|
PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' )
|
||
|
REVISION="0.1"
|
||
|
|
||
|
. $PROGPATH/utils.sh
|
||
|
|
||
|
#
|
||
|
# Fonction d'aide
|
||
|
#
|
||
|
usage() {
|
||
|
cat <<EOF
|
||
|
Usage :
|
||
|
$0 -h
|
||
|
$0 [-w warning_monitored_offline_range] [-c critical_monitored_offline_range] [-W warning_total_peers] [-C critical_total_peers]
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
#
|
||
|
# Gestion des paramètres
|
||
|
#
|
||
|
while getopts hw:c:b:B: f; do
|
||
|
case "$f" in
|
||
|
'h')
|
||
|
usage
|
||
|
exit
|
||
|
;;
|
||
|
|
||
|
#'a')
|
||
|
# WARNING_MONITORED_ONLINE_RANGE="$OPTARG"
|
||
|
# ;;
|
||
|
|
||
|
#'A')
|
||
|
# CRITICAL_MONITORED_ONLINE_RANGE="$OPTARG"
|
||
|
# ;;
|
||
|
|
||
|
'b')
|
||
|
WARNING_MONITORED_OFFLINE_RANGE="$OPTARG"
|
||
|
;;
|
||
|
|
||
|
'B')
|
||
|
CRITICAL_MONITORED_OFFLINE_RANGE="$OPTARG"
|
||
|
;;
|
||
|
|
||
|
'w')
|
||
|
WARNING_TOTAL_RANGE="$OPTARG"
|
||
|
;;
|
||
|
|
||
|
'c')
|
||
|
CRITICAL_TOTAL_RANGE="$OPTARG"
|
||
|
;;
|
||
|
|
||
|
\?)
|
||
|
usage
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
|
||
|
|
||
|
#
|
||
|
# Lancement de la commande
|
||
|
#
|
||
|
# Note : on lance les traitements "sûrs" (décompte) ensuite pour
|
||
|
# bien capturer un éventuel échec de la commande
|
||
|
# principale.
|
||
|
# En outre, grep retourne un code d'erreur si aucune
|
||
|
# occurrence n'est trouvée.
|
||
|
RAW_OUTPUT="$( asterisk -rx "sip show peers" 2>&1 )"
|
||
|
|
||
|
# Si la commande ne s'est pas correctement executée,
|
||
|
# on renvoie unknown
|
||
|
if [ "$?" -ne 0 ]; then
|
||
|
echo "UNKNOWN : error at command launch : $RAW_OUTPUT"
|
||
|
exit $STATE_UNKNOWN
|
||
|
fi
|
||
|
# Décompte
|
||
|
RESULT="$( printf "%s" "$RAW_OUTPUT" | tail -n 1 | sed 's/^\([0-9]\+\) sip peers \[Monitored: \([0-9]\+\) online, \([0-9]\+\) offline Unmonitored: \([0-9]\+\) online, \([0-9]\+\) offline\]/\1\t\2\t\3\t\4\t\5/g' )"
|
||
|
VALUE_TOTAL_PEERS="$( printf "%s" "$RESULT" | cut -f 1 )"
|
||
|
VALUE_MONITORED_ONLINE_PEERS="$( printf "%s" "$RESULT" | cut -f 2 )"
|
||
|
VALUE_MONITORED_OFFLINE_PEERS="$( printf "%s" "$RESULT" | cut -f 3 )"
|
||
|
VALUE_UNMONITORED_ONLINE_PEERS="$( printf "%s" "$RESULT" | cut -f 4 )"
|
||
|
VALUE_UNMONITORED_OFFLINE_PEERS="$( printf "%s" "$RESULT" | cut -f 5 )"
|
||
|
# On extrait les lignes qui n'ont pas OK comme statut. Explication des sed :
|
||
|
# - on supprime la première et la dernière ligne
|
||
|
# - on supprime celles qui ont 'OK' en 95 position
|
||
|
# - on ne garde que le premier champ
|
||
|
# - on regroupe sur une ligne en séparant par des virgules
|
||
|
PROBLEMATIC_LINES="$( printf "%s" "$RAW_OUTPUT" | sed '1d;$d' | sed '/.\{94\}OK/d' | sed 's/[[:space:]].*//' | sed -e :a -e 'N; s/\n/, /; ta' )"
|
||
|
|
||
|
|
||
|
# Ventilation selon valeur
|
||
|
RETURN_STATUS=$STATE_OK
|
||
|
RETURN_OUTPUT="OK"
|
||
|
|
||
|
# Warning checks
|
||
|
# - total
|
||
|
if [ -n "$WARNING_TOTAL_RANGE" ]; then
|
||
|
check_range "$VALUE_TOTAL_PEERS" "$WARNING_TOTAL_RANGE"
|
||
|
RET="$?"
|
||
|
if [ "$RET" -eq "2" ]; then
|
||
|
echo "ERROR with WARNING_TOTAL_RANGE"
|
||
|
exit $STATE_UNKNOWN
|
||
|
elif [ "$RET" -eq "0" ]; then
|
||
|
TMP="$VALUE_TOTAL_PEERS total peers"
|
||
|
if [ "$RETURN_STATUS" -ne "$STATE_WARNING" ]; then
|
||
|
RETURN_OUTPUT="$TMP"
|
||
|
else
|
||
|
RETURN_OUTPUT="$RETURN_OUTPUT, $TMP"
|
||
|
fi
|
||
|
RETURN_STATUS=$STATE_WARNING
|
||
|
fi
|
||
|
fi
|
||
|
# - monitored offline
|
||
|
if [ -n "$WARNING_MONITORED_OFFLINE_RANGE" ]; then
|
||
|
check_range "$VALUE_MONITORED_OFFLINE_PEERS" "$WARNING_MONITORED_OFFLINE_RANGE"
|
||
|
RET="$?"
|
||
|
if [ "$RET" -eq "2" ]; then
|
||
|
echo "ERROR with WARNING_MONITORED_OFFLINE_RANGE"
|
||
|
exit $STATE_UNKNOWN
|
||
|
elif [ "$RET" -eq "0" ]; then
|
||
|
TMP="$VALUE_MONITORED_OFFLINE_PEERS monitored offline peers"
|
||
|
if [ "$RETURN_STATUS" -ne "$STATE_WARNING" ]; then
|
||
|
RETURN_OUTPUT="$TMP"
|
||
|
else
|
||
|
RETURN_OUTPUT="$RETURN_OUTPUT, $TMP"
|
||
|
fi
|
||
|
RETURN_STATUS=$STATE_WARNING
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
# Critical checks (copy-paste from warning + regexp s/CRITICAL/CRITICAL/g)
|
||
|
# - total
|
||
|
if [ -n "$CRITICAL_TOTAL_RANGE" ]; then
|
||
|
check_range "$VALUE_TOTAL_PEERS" "$CRITICAL_TOTAL_RANGE"
|
||
|
RET="$?"
|
||
|
if [ "$RET" -eq "2" ]; then
|
||
|
echo "ERROR with CRITICAL_TOTAL_RANGE"
|
||
|
exit $STATE_UNKNOWN
|
||
|
elif [ "$RET" -eq "0" ]; then
|
||
|
TMP="$VALUE_TOTAL_PEERS total peers"
|
||
|
if [ "$RETURN_STATUS" -ne "$STATE_CRITICAL" ]; then
|
||
|
RETURN_OUTPUT="$TMP"
|
||
|
else
|
||
|
RETURN_OUTPUT="$RETURN_OUTPUT, $TMP"
|
||
|
fi
|
||
|
RETURN_STATUS=$STATE_CRITICAL
|
||
|
fi
|
||
|
fi
|
||
|
# - monitored offline
|
||
|
if [ -n "$CRITICAL_MONITORED_OFFLINE_RANGE" ]; then
|
||
|
check_range "$VALUE_MONITORED_OFFLINE_PEERS" "$CRITICAL_MONITORED_OFFLINE_RANGE"
|
||
|
RET="$?"
|
||
|
if [ "$RET" -eq "2" ]; then
|
||
|
echo "ERROR with CRITICAL_MONITORED_OFFLINE_RANGE"
|
||
|
exit $STATE_UNKNOWN
|
||
|
elif [ "$RET" -eq "0" ]; then
|
||
|
TMP="$VALUE_MONITORED_OFFLINE_PEERS monitored offline peers"
|
||
|
if [ "$RETURN_STATUS" -ne "$STATE_CRITICAL" ]; then
|
||
|
RETURN_OUTPUT="$TMP"
|
||
|
else
|
||
|
RETURN_OUTPUT="$RETURN_OUTPUT, $TMP"
|
||
|
fi
|
||
|
RETURN_STATUS=$STATE_CRITICAL
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
|
||
|
# Affichage final
|
||
|
# Petit ajout dans l'indication
|
||
|
RETURN_OUTPUT_ADDENDUM=""
|
||
|
if [ -n "$PROBLEMATIC_LINES" ]; then
|
||
|
RETURN_OUTPUT_ADDENDUM="$( printf " (not ok peers : %s)" "$PROBLEMATIC_LINES" )"
|
||
|
fi
|
||
|
printf "%s%s | total=%d;%s;%s monitored_online=%d;%s;%s monitored_offline=%d;%s;%s unmonitored_online=%d;%s;%s unmonitored_offline=%d;%s;%s\n" "$RETURN_OUTPUT" "$RETURN_OUTPUT_ADDENDUM" \
|
||
|
"$VALUE_TOTAL_PEERS" "$WARNING_TOTAL_RANGE" "$CRITICAL_TOTAL_RANGE" \
|
||
|
"$VALUE_MONITORED_ONLINE_PEERS" "$WARNING_MONITORED_ONLINE_RANGE" "$CRITICAL_MONITORED_ONLINE_RANGE" \
|
||
|
"$VALUE_MONITORED_OFFLINE_PEERS" "$WARNING_MONITORED_OFFLINE_RANGE" "$CRITICAL_MONITORED_OFFLINE_RANGE" \
|
||
|
"$VALUE_UNMONITORED_ONLINE_PEERS" "$WARNING_UNMONITORED_ONLINE_RANGE" "$CRITICAL_UNMONITORED_ONLINE_RANGE" \
|
||
|
"$VALUE_UNMONITORED_OFFLINE_PEERS" "$WARNING_UNMONITORED_OFFLINE_RANGE" "$CRITICAL_UNMONITORED_OFFLINE_RANGE"
|
||
|
exit $RETURN_STATUS
|