Monitoring trunks

Last week I fiddled around with my home network and just this morning I found out due to a DNS thing my trunks hadn’t been online for the better part of a week. Now I’m looking for a way to monitor this so I get a heads up a next time this happens. Is this what “Monitor Trunk Failures“ is for? Can I point to a simple PHP script that sends me a mail so I can look into this? Or will this only work when using the trunk (making a call)?

As the tooltip explains for that setting. It’s an AGI script that runs on a NOANSWER or CANCEL. It doesn’t monitor if the trunk is “up” or “down”.

Too bad. Any tips for a cronjob?

Nope because you haven’t explained what your scenario is. A trunk “being down” can mean different things to different people. So you want to provide some details someone might be able to provide some suggestions.

The trunks were unregistered. If there’s a way to get this data in let’s say an array (json?), I could work from there. Or some other script I could run every x time sending an email telling me if a trunk is unregistered.

I’m going with the following:

exec(‘/usr/sbin/asterisk -rx “pjsip show registrations” | grep \‘Unregistered\’ | awk \’{print $2}\‘’, $trunks);
if(count($trunks)!=0) mail(‘’, ‘trunk down’, print_r($trunks, true));

The first line executes the following command:

/usr/sbin/asterisk -rx "pjsip show registrations"

This gives an array with trunks that are “Unregistered”. The second line sends a mail if the array is not empty. This could probably be done using just a single command, but hey, PHP is my weapon of choice. But please do teach me.

This sounds like it would be worth including in the distro.

It’s interesting to consider – and comes up from time to time, with this thread perhaps the most recent best fit for OP – but would probably need some additional refinement before making it in to a module/distro/etc. (A Fibonacci Backoff for email alerts would be nice – will try to dust off that code some time…)

If this is going to be considered something added to FreePBX and only this then it’s not “trunk monitoring” it’s “registration monitoring” because not all trunks use registration and being registered doesn’t mean the trunk is “up”.

A registered status means it sent a REGISTER request and got a 200 OK back for it (doesn’t even need to authenticate, a 200 OK means accepted) and that’s it. When it REGISTER’s all it does is provide the far end with the protocol (UDP/TCP/TLS) and the IP and port to send requests to.

  • Trunk could show registered but the far side dropped the location. So no incoming calls.
  • Trunk could show registered but also be in an Unavailable status because it couldn’t qualify the far side.
  • Trunk could show registered but also be Unavailable on the far side because they couldn’t qualify the location.
  • Trunk could show registered and Available but no incoming calls because the firewall/Internet had a blip or the firewall is awful and closed ports. So no incoming calls.

So just because the trunk shows registered doesn’t mean the trunk is “up” or accepting calls properly. Also registration and outbound have nothing to do with each other so even unregistered the trunk could still be sending outgoing calls with no issue.

Again, this as it is now isn’t monitoring the status of a trunk it’s monitoring the status of a registration.

I use this script in a file called trunkcheck.sh in /root. It runs every 5mins as a cronjob. It searches for two terms “unregistered” or “rejected”. It does a perfect job.

#!/bin/bash

email="youremailadress"
/usr/sbin/asterisk -rx 'pjsip show registrations' > /tmp/allTrunks.txt
offlineTrunks=`cat /tmp/allTrunks.txt| grep -e 'Unregistered\|Rejected'`
if [ -z "$offlineTrunks" ]; then 
exit
else
(
echo "Subject: Offline Trunks"
printf 'COMPANY-NAME Trunks that are currently offline...\n''\n'"${offlineTrunks}"
) | /usr/sbin/sendmail ${email}
fi

If anyone of you uses CheckMK - there is an agent plugin for monitoring Asterisk Endpoints (which are Extensions and Trunks).

From here you can do what you want if anything fails or deregisters. I.e. I’m getting a message in Matrix/Element if a phone or trunk is not registered.

1 Like

Trunks have fields called “Monitor Trunk Failures”

grafik

Whaterver script may run from here. Depending on what you want to see.

However my script does not differentiate things BlazeStudios mentioned here.

The script has to be stored in: /var/lib/asterisk/agi-bin/trunk-failure-200.sh

content:

#!/bin/bash
#Pfad /var/lib/asterisk/agi-bin/trunks-failure.sh
#[email protected]
DATE=$(date)
HOST=$(hostname)
#/usr/sbin/asterisk -rx “reload”
SIPTRUNKSF=$(/usr/sbin/asterisk -rx ‘pjsip show registrations’)
fwconsole reload
SIPTRUNKS=$(/usr/sbin/asterisk -rx ‘pjsip show registrations’)
IAXTRUNKS=$(/usr/sbin/asterisk -rx ‘iax2 show peers’)
MESSAGE=“A trunk failure MSN-200 forced a reload on $HOST”
MESSAGE=“$MESSAGE\nDATE: $DATE”
MESSAGE=“$MESSAGE\nCurrent Trunk Status:”
MESSAGE=“$MESSAGE\n\nSIP Trunks before reload:\n$SIPTRUNKSF”
MESSAGE=“$MESSAGE\n\nSIP Trunks after reload:\n$SIPTRUNKS”
MESSAGE=“$MESSAGE\n\nIAX2 Trunks:\n$IAXTRUNKS”
echo -e “$MESSAGE” | mail -s “Trunk Failure MSN-200” “$EMAIL”