Monitoring your Peers (Asterisk extensions) and Trunks

As an admin for a telephone system, possibly one of the most useful things you can do is monitoring your peers and trunks. There are 2 great reasons you should do so:

1. You can respond to and resolve issues with your system before your users know about it, and you can be in the know if someone reportsĀ “none of the phones are working” when in fact only 1 or 2 are not working

2. You can actually know when there is a problem with the system – where you otherwise might not know there is a problem until someone calls on your mobile to say your office number is not working

 

I have 2 scripts running every 15 minutes to email me with the details of any down extensions and trunks. This is done in Crontab with the line:

*/15 8-18 * * Mon-Fri /usr/Peermonitor.sh

and similar for Trunkmonitor.sh. This line says to run every 15 minutes between 08:00 and 18:00 every Monday-Friday

DISCLAIMER: I cannot claim ownership of the code here, it was most likely found on the web, maybe tweaked, I can’t remember it was a long time ago.

Here is Peermonitor.sh – It is based on you having qualify=60 or similar in your extensions definitions, IE not qualify=no otherwise it will be ignored. You may wish to qualify=no for eg a remote extension or softphone so you don’t get alerts when it isn’t online.

#!/bin/sh
Offlinepeers=`/usr/sbin/asterisk -rx 'sip show peers' | grep "UNKNOWN"`
if [ "$Offlinepeers" null ]
then
echo $Offlinepeers | mail -r "My PBX" -s "Asterisk Extensions offline" "[email protected]"
fi

 

And here is a link to a discussion about what I call Trunkmonitor.sh:

http://pbxinaflash.com/community/index.php?threads/email-alert-on-trunk-failure.1968/

I do remember I had to tweak it so here is my script, and we register SIP trunks but using IAX trunks is also discussed in the script:

#!/usr/bin/perl -w
use strict;
use warnings;
#no warnings; #Remark out to suppress ALL warnings, also add remark to above line.
#### email address must have \@ as perl needs to escape the @ symbol. Obviously you.ll need to edit these email addresses
my $alertemail = "you\@yourdomain.com" ;
my $pbxdomain = "mypbx.local" ;
#### comment out whichever of the following two lines are not relevant for your server
checktrunk("sip");
# checktrunk("iax2");
##################################################
##################################################
####
#### Trunk Alerts script written by Jim Hribnak Oct 7th 2007
#### if there is any questions please feel free to drop me an email at jimh at domain nucleus.com
####
##################################################
##################################################
####
#### Script modified by Charles Pippin Feb 21st 2009 - Added counters for loop retry.
#### if there is any questions please feel free to drop me an email at cwpippin at domain gmail.com
####
##################################################
##################################################
####
#### Script modified by Stephen Wilkey Mar 18th 2010
#### - moved email addressing details to the top of the script so that they are easier to customise
#### - original script assumed both iax2 and sip trunks and would fail if you didn.t have both . now the one you don.t want can be easily commented out at the top
#### - improved the documentation below about what files to create and what to include in those files in order to customise best to individual environments
#### - simplified the code to use the same code for both SIP and IAX2. PLEASE NOTE: the iax trunk alert file has now changed to having iax2 in the name. If you are upgrading from an older version you will need to rename the original iax trunk alert file.
##################################################
##################################################
####
#### Create the following files in /etc/asterisk (if you want to only check sip trunks don.t create the other file etc)
#### - /etc/asterisk/trunkalerts_sip.conf
#### - /etc/asterisk/trunkalerts_iax2.conf
####
#### in the files below add the any unique entry from asterisk -rx "sip show registry" and
#### from asterisk -rx "iax2 show registry". The original specification was to use
#### the hostname, however if you have multiple trunks from the same supplier it is more
#### likely that the username might be unique. You don't need to be consistent either
#### - you can use hostname for some and username for others - as long as each
#### line of the show registry result is specified uniquely once by something
#### unique in that line for this script to grep on.
####
##################################################
##################################################
####
sub checktrunk {
# the next line gets the trunktype parameter passed to the subroutine
my $trunktype=shift;
####
print "=============================================\n";
print "$trunktype Trunk information\n";
print "=============================================\n";
open(TRUNKLIST,"</etc/asterisk/trunkalerts_$trunktype.conf");
while (<TRUNKLIST>) {
chomp;
my $recheckcounter = 1;
my $upcounter = 5;
my $dncounter = 5;
until ($upcounter == 0) {
my $trunks = `/usr/sbin/asterisk -rx "$trunktype show registry" | grep "$_" | awk '{
print \$5
}
'`;
#comment next line if you don't want to see the output
#print "$trunktype trunks = $trunks\n"; #Remove remark to see output from "sip show registry" command
if ($trunks =~ "Registered") {
$upcounter = $upcounter - 1;
#print $upcounter,"\n"; #Remove remark to see counter decrement
print "$_ is up\n" ; #Remove remark to see notification of trunk being "UP"
}
else {
$dncounter = $dncounter - 1;
#print $dncounter,"\n"; #Remove remark to see counter decrement once trunk is found to be down
print "$_ is down\n" ; #Remove remark to see notification of trunk being "DOWN"
if ($dncounter == 0) {
if ($recheckcounter == 1) {
# we now reload the trunks and force the check to occur again
`asterisk -rx "module reload"`;
print "Reloading trunks (will wait for reconnection before checking again\n";
# adjust the length of time on the next line (seconds) if you find it takes longer to re-establish
# trunk connections after a reload
`sleep 20`;
$recheckcounter = 0;
$dncounter = 5;
}
else
{
mailalert();
$upcounter = 0;
#print $upcounter,"\n"; #Remove remark to see counter decrement once trunk is found to be down
print "houston we have a problem\n";
print "$_ trunk is not registering\n";
$recheckcounter = 1;
}
}
}
}
}
#end of while loop (read trunk file)
}
##################################################
##################################################
####
#### Email Subroutines
#### Change anywhere below where there is an email address an email address
#### must have \@ as perl needs to escape the @ symbol
####
##################################################
##################################################
sub mailalert {
my $sendmail = "/usr/sbin/sendmail -t";
my $from= "FROM: <pbx\@$pbxdomain>\n";
my $reply_to = "Reply-to: <pbx\@$pbxdomain\n";
my $subject = "Subject: $_ is DOWN!!!!\n";
my $content = "PBX TRUNK $_ is DOWN!!!!\n";
my $send_to = "To:<$alertemail>\n";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $from;
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $send_to;
print SENDMAIL $content;
close(SENDMAIL);
print "An email has been sent!\n\n";
}

Remember that to get it working you need to create the file as specified in /etc/asterisk either trunkalerts_sip.conf and/or trunkalerts_iax2.conf

*Note: I am pretty sure that if you don’t register your trunks this script won’t work; at the very least you’ll want to qualify=60 your trunks. If you have incoming numbers delivered directly to you via SIP URI (eg [email protected]) then you still won’t know if incoming calls stop working! I have an idea to setup an automated test system which dials out to your PSTN number, waits for your system to answer and then plays a DTMF tone sequence, at which point your system returns a confirmation or even a status DTMF tone, and the test system logs this as a successful test. Anyone fancy helping with that, please feel free to leave a comment – Thanks!