Asterisk TRUNK Monitor

Posted on November 4, 2012 by admin

For a one time edit, launch the terminal and type:
EDITOR=nano crontab -e

If you want to set nano as your default editor in general, you use this command:
export EDITOR=nano

Now when you go to edit crontab, nano will be the default editor than vi. You can test this by typing:
crontab -e

*** Script to check SIP trunnk on Asterisk Voice server
/etc/asterisk/trunkalerts_sip.conf
/etc/asterisk/trunkalerts_iax2.conf
contains usernames of SIP or IAX2 trunks

1 2 3 4 5 /path/to/command arg1 arg2
Where,

1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command – Script or command name to schedule

Easy to remember format:

* * * * * command to be executed
– – – – –
| | | | |
| | | | —– Day of week (0 – 7) (Sunday=0 or 7)
| | | ——- Month (1 – 12)
| | ——— Day of month (1 – 31)
| ———– Hour (0 – 23)
————- Minute (0 – 59)

Your cron job looks as follows for system jobs:

1 2 3 4 5 USERNAME /path/to/command arg1 arg2
OR
1 2 3 4 5 USERNAME /path/to/script.sh

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

* * * * * /usr/sbin/trunkalerts.sh

Create file ###############trunkalerts.sh

#!/bin/sh
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
/usr/bin/perl /usr/sbin/trunkalerts.pl

Create file ###############trunkalerts.pl

#!/usr/bin/perl -w
use strict;
use warnings;
my $trunks = ”;
##################################################
##################################################
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 = “info\@nextgvoip.com” ;
my $pbxdomain = “nextgvoip.com” ;
##################################################
##################################################
#### 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 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 = 1;
my $dncounter = 5;
until ($upcounter == 0) {
$trunks = `asterisk -rx “$trunktype show registry” | grep \”$_\” | awk ‘{print $4}’`;
##################################################
##################################################
#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 “$trunktype$_ 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 20sec 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: <spyvision\@$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”;
}