check_asterisk/check_sip_connected_peers

125 lines
3.2 KiB
Bash
Executable file

#!/bin/bash
CRITICAL_LATENCY=100
WARNING_LATENCY=70
CRITICAL_PEER_NUMBER=0
WARNING_PEER_NUMBER=1
IPS=()
SITE_NAME=""
DEBUG=0
function usage() {
[ -n "$1" ] && echo "$1" && echo
echo "Usage : $0 [-cX] [-wX] [-CX] [-WX] [-d] [site name] [IP1] [IP2] ..."
echo " -h Show this help message"
echo " -d Enable debug mode"
echo " -c[X] Specify critical number of connected peers"
echo " -w[X] Specify warning number of connected peers"
echo " -C[X] Specify critical latency average of connected peers (in ms)"
echo " -W[X] Specify warning latency average of connected peers (in ms)"
echo " [site name] Specify the site name (use in return)"
echo " [IP1] [IP2] Specify IP address of the specify site"
}
function debug() {
[ $DEBUG -eq 1 ] && echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $1"
}
function join_by {
local IFS="$1"
shift
echo "$*"
}
# Parse arguments
for arg in $@
do
if [ $( echo $arg|egrep -c '^-[cwCWdh][0-9]*$' ) -gt 0 ]
then
a=$( echo $arg|sed 's/^-\([cwCWdh]\).*$/\1/' )
v=$( echo $arg|sed 's/^-[cwCWdh]//' )
case $a in
c)
CRITICAL_PEER_NUMBER=$v
;;
w)
WARNING_PEER_NUMBER=$v
;;
C)
CRITICAL_LATENCY=$v
;;
W)
WARNING_LATENCY=$v
;;
d)
DEBUG=1
;;
h)
usage
exit 0;
;;
esac
elif [ -z "$SITE_NAME" ]
then
SITE_NAME="$arg"
else
IPS+=("$arg")
fi
done
[ -z "$SITE_NAME" ] && usage "You must specify site name" && exit 1
[ ${#IPS[@]} -eq 0 ] && usage "You must specify at least on ID address" && exit 1
debug "Specified site name : $SITE_NAME"
debug "Specified IP addresses : $( join_by ' ' ${IPS[@]} )"
debug "Warning/Critical latency : $WARNING_LATENCY / $CRITICAL_LATENCY"
[ $WARNING_LATENCY -gt $CRITICAL_LATENCY ] && usage "Warning latency must be lower than critical one" && exit 1
debug "Warning/Critical number of connected peers : $WARNING_PEER_NUMBER / $CRITICAL_PEER_NUMBER"
[ $WARNING_PEER_NUMBER -lt $CRITICAL_PEER_NUMBER ] && usage "Warning number of connected peers must be greater than critical one" && exit 1
state=$( /usr/bin/sudo -u root /usr/sbin/rasterisk -rnx "sip show peers"|grep -E "($( join_by '|' ${IPS[@]} ))" )
debug "Current connected peers connected on specified IP address :\n$state"
nb_connected=0
sum_latency=0
IFS="
"
for line in $state
do
if [ $( echo -e "$line"|grep -Ec ' OK \([0-9]+ ms\)' ) -gt 0 ]
then
let nb_connected=nb_connected+1
let sum_latency=sum_latency+$( echo -e "$line"|sed 's/.* OK (\([0-9]\+\) ms).*$/\1/' )
else
debug "Unmatched line or not connected peer :\n\t$line"
fi
done
debug "Nb connected peers : $nb_connected"
avg_latency=0
if [[ $nb_connected -gt 0 ]]
then
let avg_latency=sum_latency/nb_connected
debug "Average latency of connected peers : $avg_latency ($sum_latency / $nb_connected)"
fi
STATE=OK
MSG="$nb_connected peer(s) connected on $SITE_NAME with an average latency of $avg_latency ms|'connected peers':$nb_connected;$WARNING_PEER_NUMBER;$CRITICAL_PEER_NUMBER;0;0 'average latency':$avg_latency;$WARNING_LATENCY;$CRITICAL_LATENCY;0;0"
EXITC=0
if [[ $nb_connected -le $CRITICAL_PEER_NUMBER || $avg_latency -ge $CRITICAL_LATENCY ]]
then
STATE=CRITICAL
EXITC=2
elif [[ $nb_connected -le $WARNING_PEER_NUMBER || $avg_latency -ge $WARNING_LATENCY ]]
then
STATE=WARNING
EXITC=1
fi
echo "$STATE - $MSG"
exit $EXITC