2012-10-09 13:24:55 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-11-05 18:50:42 +01:00
|
|
|
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"
|
|
|
|
}
|
2012-10-09 13:24:55 +02:00
|
|
|
|
2018-11-05 18:50:42 +01:00
|
|
|
function debug() {
|
|
|
|
[ $DEBUG -eq 1 ] && echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $1"
|
|
|
|
}
|
2012-10-09 13:24:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-11-05 18:50:42 +01:00
|
|
|
debug "Peer $PEER state : $state"
|
|
|
|
|
2012-10-09 13:24:55 +02:00
|
|
|
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' )
|
2018-11-05 18:50:42 +01:00
|
|
|
debug "Peer $PEER latency : $latency ms"
|
2012-10-09 13:24:55 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:50:42 +01:00
|
|
|
CRITICAL_LATENCY=100
|
|
|
|
WARNING_LATENCY=70
|
|
|
|
|
|
|
|
CRITICAL_PEER_NUMBER=1
|
|
|
|
WARNING_PEER_NUMBER=2
|
|
|
|
|
2012-10-09 13:24:55 +02:00
|
|
|
PEERS=""
|
|
|
|
DEBUG=0
|
|
|
|
|
|
|
|
for arg in $@
|
|
|
|
do
|
2018-11-05 18:50:42 +01:00
|
|
|
if [ $( echo $arg|egrep -c '^-[cwCWdh][0-9]*$' ) -gt 0 ]
|
2012-10-09 13:24:55 +02:00
|
|
|
then
|
2018-11-05 18:50:42 +01:00
|
|
|
a=$( echo $arg|sed 's/^-\([cwCWdh]\).*$/\1/' )
|
|
|
|
v=$( echo $arg|sed 's/^-[cwCWdh]//' )
|
2012-10-09 13:24:55 +02:00
|
|
|
case $a in
|
|
|
|
c)
|
|
|
|
CRITICAL_PEER_NUMBER=$v
|
|
|
|
;;
|
|
|
|
w)
|
|
|
|
WARNING_PEER_NUMBER=$v
|
|
|
|
;;
|
|
|
|
C)
|
|
|
|
CRITICAL_LATENCY=$v
|
|
|
|
;;
|
|
|
|
W)
|
|
|
|
WARNING_LATENCY=$v
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
DEBUG=1
|
|
|
|
;;
|
2018-11-05 18:50:42 +01:00
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2012-10-09 13:24:55 +02:00
|
|
|
esac
|
|
|
|
else
|
|
|
|
PEERS="$PEERS $arg"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2018-11-05 18:50:42 +01:00
|
|
|
[ -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
|
|
|
|
|
2012-10-09 13:24:55 +02:00
|
|
|
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
|