Monitoring Trunk Failures

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"

I’m monitoring in a really, really simple fashion. Don’t blame me. I only have four and it’s enough for my demand :smiley:

/usr/sbin/asterisk -rx "pjsip show registrations" | grep 'Registered' | awk '{print$2}'

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!

Btw, I’m even less of a script person.

Yes, but there are no built in hooks for this that I am aware of.

You would need to write a script for it.

Trunks going unavailable do write to the Asterisk log, so if you are shipping logs to something, you could set an alert on that system also.

1 Like

Ok I know about the log entries that are created when a trunk fails (XXXX is now unreachable). This is what I use to “document” the outages.

So I would have to find a way to be alerted when this line shows up in the logs ?

tail -f /var/log/asterisk/full|while read line;do [[  $line =~ unreachable ]]&&echo $line|mail -s trunkfail [email protected] ;done &

If you also want to emails when the trunk returns

shopt -s nocasematch;tail -f /var/log/asterisk/full|while read line;do [[  $line =~ reachable ]]&&echo $line|mail -s trunkfail [email protected] ;done &
1 Like

It works, thank you very much ! I really don’t know much about Linux. Can you tell how you would proceed to enable the monitoring full time ?

Cheap and cheerful method, replace -f with -F and add it to root’s cronjobs

@reboot ( shopt -s nocasematch;tail -F /var/log/asterisk/full|while read line;do [[  $line =~ reachable ]]&&echo $line|mail -s trunkfail [email protected] ;done &) 
2 Likes

Will try !

Thank you again !

1 Like

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) )

Ok so the 2 lines in my cron file (/var/spool/cron/root) would look like this ? :

@reboot ( tail -F /var/log/asterisk/full|while read line;do [[  $line =~ "Peer 'xxxx' is now UNREACHABLE" ]]&&echo $line|mail -s "Trunk is down" [email protected] ;done >/dev/null 2>&1)

@reboot ( shopt -s nocasematch;tail -F /var/log/asterisk/full|while read line;do [[  $line =~ "Peer 'xxxx' is now Reachable" ]]&&echo $line|mail -s "Trunk is back online" [email protected] ;done >/dev/null 2>&1)

I customized a bit the lines to be more precise.

That is a little more than you need,

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]

putting all together

@reboot (shopt -s nocasematch ;tail -F  /var/log/asterisk/full|while read line;do [[ $line =~ xxxx.*reach  ]]&& mail -s "$(cut -d':' -f5,6 <<< $line)" [email protected] <<< $line ;done) > /dev/null 2>&1

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.