I use a script to monitor failed outgoing calls when a trunk goes down. It works fine but I’d like to know if it’s possible to be alerted as soon as a trunk goes offline. For now, if a trunk is down, I only know it when someone tries to place an outbound call.
Here is the script I use (took it somewhere else, I am really not a script person !)
#!/bin/bash
DATE=`date "+%d.%m.%Y. %H:%M"`
HOST=`hostname`
# initializing agi variables
declare -a array
while read -e ARG && [ "$ARG" ] ; do
array=(` echo $ARG | sed -e 's/://'`)
export ${array[0]}=${array[1]}
done
MESSAGE="Incident happened $DATE.\n$agi_calleridname [$agi_callerid] tried to dial number $agi_dnid.\nCall could not be established on channel $agi_channel."
# write log
echo "$DATE Call could not be established. $agi_calleridname [$agi_callerid] --> $agi_dnid" >> /var/log/asterisk/trunk
# send email
echo -e "$MESSAGE" | mail -s "TRUNK IS DOWN $HOST IP-PBX" [email protected] -- -F "AsteriskAlert"
This will then output a list of trunks which aren’t registered or have been rejected. You could expand it to count the lines and therefore show how many trunks have failed, for example.
Don’t know if that’s what you’re looking for, but maybe it helps in some way!
In retrospect and given the way cron works, I would better replace the “&” ( send to background) with a more appropriate " >/dev/null 2>&1" (send all output to the bit bucket)
( and looking at what I actually do, that is exactly what I came up with a long time ago (I suffer from CRS) )
Best to only open that file once
The shopt means use a case insensitive regex ( =~ )
If you are only interested in the one endpoint xxxx then then “$line =~ xxxx.*reach” will, match both Reachable. and UNREACHABLE!.
To put the meat in the subject just cut the juicy bits out and use it as the subject “mail -s “$(cut -d’:’ -f5,6 <<< $line)” [email protected]”