check_asterisk/check_sip_peers
2018-11-05 18:50:42 +01:00

150 lines
3.1 KiB
Bash
Executable file

#!/bin/bash
function usage() {
[ -n "$1" ] && echo "$1" && echo
echo "Usage : $0 [-cX] [-wX] [-CX] [-WX] [-d] [peer1] [peer2] ..."
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 of connected peers (in ms)"
echo " -W[X] Specify warning latency of connected peers (in ms)"
echo " [peer] Specify the peers name"
}
function debug() {
[ $DEBUG -eq 1 ] && echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $1"
}
function checkPeer() {
PEER=$1
state=$( /usr/bin/sudo -u root /usr/sbin/rasterisk -rnx "sip show peer $PEER"|grep "Status"|cut -d ":" -f 2 )
if [ ! -n "$state" ]
then
export MSG="$PEER : not found"
return 3
fi
debug "Peer $PEER state : $state"
ok=$( echo $state|grep -c OK )
if [ $ok -eq 0 ]
then
export MSG="$PEER :$state"
return 3
else
latency=$( echo "$state"|sed 's/.*(\([0-9]*\) ms.*/\1/g' )
debug "Peer $PEER latency : $latency ms"
if [ $latency -gt $CRITICAL_LATENCY ]
then
st='CRITICAL LATENCY'
ec=2
elif [ $latency -gt $WARNING_LATENCY ]
then
st='WARNING LATENCY'
ec=1
else
st=OK
ec=0
fi
MSG="$PEER : $st (Latency : $latency ms)"
return $ec
fi
}
CRITICAL_LATENCY=100
WARNING_LATENCY=70
CRITICAL_PEER_NUMBER=1
WARNING_PEER_NUMBER=2
PEERS=""
DEBUG=0
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
else
PEERS="$PEERS $arg"
fi
done
[ -z "$PEERS" ] && usage "You must specify at least one peer name" && exit 1
debug "Specified peers name : $PEERS"
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 peers : $WARNING_PEER_NUMBER / $CRITICAL_PEER_NUMBER"
[ $WARNING_LATENCY -gt $CRITICAL_LATENCY ] && usage "Warning peers number must be lower than critical one" && exit 1
states=""
peers=""
nbok=0
nbcrit=0
nbwarn=0
nbunk=0
nbpeers=O
for PEER in $PEERS
do
let nbpeers=nbpeers+1
checkPeer "$PEER"
res=$?
states="$states - $MSG"
case $res in
0)
let nbok=nbok+1
;;
1)
let nbwarn=nbwarn+1
;;
2)
let nbcrit=nbcrit+1
;;
3)
let nbunk=nbunk+1
;;
esac
peers="$peers $PEER"
done
STATE=OK
MSG="$nbok peer(s) OK of $nbpeers peers $states|'available peers':$nbok;$WARNING_PEER_NUMBER;$CRITICAL_PEER_NUMBER;0;$nbpeers 'critical peers':$nbcrit;$WARNING_PEER_NUMBER;$CRITICAL_PEER_NUMBER;0;$nbpeers 'unknown peers':$nbunk;$WARNING_PEER_NUMBER;$CRITICAL_PEER_NUMBER;0;$nbpeers 'warning peers':$nbwarn;$WARNING_PEER_NUMBER;$CRITICAL_PEER_NUMBER;0;$nbpeers"
EXITC=0
if [ $nbok -le $CRITICAL_PEER_NUMBER ]
then
STATE=CRITICAL
EXITC=2
elif [ $nbok -le $WARNING_PEER_NUMBER ]
then
STATE=WARNING
EXITC=1
fi
echo "$STATE - $MSG"
exit $EXITC