check_asterisk/check_sip_zombie_channels
2022-05-19 11:36:51 +02:00

128 lines
3.1 KiB
Bash
Executable file

#!/bin/bash
DEBUG=0
WARNING_HOURS=3
CRITICAL_HOURS=6
function usage() {
[ -n "$1" ] && echo "$1" && echo
echo "Usage : $0 [-cX] [-wX] [-d]"
echo " -h Show this help message"
echo " -d Enable debug mode"
echo " -c[X] Specify critical call duration threshold"
echo " -w[X] Specify warning call duration threshold"
}
function debug() {
[ $DEBUG -eq 1 ] && echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $1"
}
CRITICAL=10
WARNING=8
DEBUG=0
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_HOURS=$v
;;
w)
WARNING_HOURS=$v
;;
d)
DEBUG=1
;;
h)
usage
exit 0
;;
esac
else
usage "Invalid argument '$arg'"
fi
done
let WARNING_SECONDS=WARNING_HOURS*3600
let CRITICAL_SECONDS=CRITICAL_HOURS*3600
STATE=OK
EXITCODE=
CRITICAL=0
CRITICAL_CHANNELS=""
WARNING=0
WARNING_CHANNELS=""
OK=0
debug "Retreive current channels info using rasterisk..."
## Output example for asterisk cli commande 'core show channels concise' :
## format :
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14
## chanid!context!exten!prio!state!app!appdata!callerid![?]![?]![?]!duration!bridgedtochan![?]
## SIP/trunk-00010bc5!incoming!02XXXXXX19!1!Down!AppDial!(Outgoing Line)!02XXXXXX19!!!3!0!(None)!1447335911.168247
## SIP/trunk-00010bc0!macro-callqueue!s!4!Up!Queue!natlrecouvrement,,,,120!09XXXXXX68!!!3!19!(None)!1447335892.168238
CURRENT_CHANNELS=$( /usr/bin/sudo -u root /usr/sbin/rasterisk -rnx 'core show channels concise' | grep -v '^CBAnn/' | grep -Ev "^Setting\s+max\s+files")
debug "Current channels:\n$CURRENT_CHANNELS"
IFS="
"
for line in $CURRENT_CHANNELS
do
if [ $(echo $line | grep -c "^Message/ast_msg_queue") -eq 1 ]
then
debug "Info Message/ast_msg_queue channel (line: $line)"
continue
fi
channel=$( echo -e "$line"|cut -d'!' -f1 )
duration=$( echo -e "$line"|cut -d'!' -f12 )
debug "Channel '$channel' duration: '$duration'"
if [ $duration -gt $CRITICAL_SECONDS ]
then
debug "Channel '$channel' duration is CRITICAL"
let CRITICAL=CRITICAL+1
CRITICAL_CHANNELS="$CRITICAL_CHANNELS $channel"
elif [ $duration -gt $WARNING_SECONDS ]
then
debug "Channel '$channel' duration is WARNING"
let WARNING=WARNING+1
WARNING_CHANNELS="$WARNING_CHANNELS $channel"
else
debug "Channel '$channel' duration is OK"
let OK=OK+1
fi
done
debug "Critical channels count: $CRITICAL ($CRITICAL_CHANNELS)"
debug "Warning channels count: $WARNING ($WARNING_CHANNELS)"
debug "OK channels count: $OK"
if [ $CRITICAL -gt 0 ]
then
STATE=CRITICAL
EXITCODE=2
MSG="$CRITICAL channels open since more than $CRITICAL_HOURS hours"
elif [ $WARNING -gt 0 ]
then
STATE=WARNING
EXITCODE=1
MSG="$WARNING channels open since more than $WARNING_HOURS hours"
else
STATE=OK
EXITCODE=0
MSG="$OK channels open since less than $WARNING_HOURS hours"
fi
echo "$STATE - $MSG|ok=$OK,warning=$WARNING,critical=$CRITICAL"
[ -n "$CRITICAL_CHANNELS" ] && echo "Critical channels :$CRITICAL_CHANNELS"
[ -n "$WARNING_CHANNELS" ] && echo "Warning channels :$WARNING_CHANNELS"
exit $EXITCODE