#!/bin/bash # Verifie la latence d'une connexion SIP Asterisk DEBUG=0 PEER="" CRITICAL_LATENCY=100 WARNING_LATENCY=40 function usage() { [ -n "$1" ] && echo "$1" && echo echo "Usage : $0 [-cX] [-wX] [-d] [peer]" echo " -h Show this help message" echo " -d Enable debug mode" echo " -c[X] Specify critical latency of peer (in ms)" echo " -w[X] Specify warning latency of peer (in ms)" echo " [peer] Specify the peer name" } function debug() { [ $DEBUG -eq 1 ] && echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $1" } # Parse arguments for arg in $@ do if [ $( echo $arg|egrep -c '^-[cwdh][0-9]*$' ) -gt 0 ] then a=$( echo $arg|sed 's/^-\([cwdh]\).*$/\1/' ) v=$( echo $arg|sed 's/^-[cwdh]//' ) case $a in c) CRITICAL_LATENCY=$v ;; w) WARNING_LATENCY=$v ;; d) DEBUG=1 ;; h) usage exit 0; ;; esac elif [ -z "$PEER" ] then PEER="$arg" else echo "Only one peer can be specified (or invalid '$arg' parameter)" fi done [ -z "$PEER" ] && usage "You must specify peer name" && exit 1 debug "Specified peer name : $PEER" debug "Warning/Critical latency : $WARNING_LATENCY / $CRITICAL_LATENCY" [ $WARNING_LATENCY -gt $CRITICAL_LATENCY ] && usage "Warning latency must be lower than critical one" && exit 1 latency=`/usr/bin/sudo -u root /usr/sbin/rasterisk -rnx "sip show peer $PEER"|grep Status|sed 's/.*(\([0-9]*\) ms.*/\1/g'` if [ ! -n "$latency" ] then echo "UNKNOWN - SIP peer $PEER unknown" exit 3 fi debug "Latency of peer $PEER : $latency ms" details="Latency : $latency ms" if [ $latency -gt $CRITICAL_LATENCY ] then nstatus="CRITICAL" exitcode=2 elif [ $latency -gt $WARNING_LATENCY ] then nstatus="WARNING" exitcode=1 else nstatus="OK" exitcode=0 fi echo "${nstatus} - SIP Latency is ${nstatus}: $details | latency=${latency}ms" exit $exitcode