SUGGESTIONS Email Alert On Trunk Failure

LesD

Member
Joined
Nov 8, 2009
Messages
408
Reaction score
15
I have tried the script and only changed the email address. The email I get shows:

A call from channel [SIP/210-00001b91] With CID : [unknown] - [020xxxxxxxx] has failed on host [pbx.local] at 09-16-2014 21:23 Number that was dialed : [07968yyyyyy] on trunk [VITELITY] with error : [] and hangup cause is : []

So it is much better but the error details do not come through for me.

In my original post, the log showed codes for the failure which would be useful if they could be captured.

"Dial failed for some reason with DIALSTATUS = CHANUNAVAIL and HANGUPCAUSE = 21"
"Dial failed for some reason with DIALSTATUS = CONGESTION and HANGUPCAUSE = 19"

The first relates to authentication failure while the second to an invalid number, which should really be ignored as far as sending an email goes.
 

LesD

Member
Joined
Nov 8, 2009
Messages
408
Reaction score
15
From the thread noted above: To eliminate the delay, the last line of the scripts should be replaced with
Code:
(echo -e "$MSG" | mail -s " VITELITY - Failed call on host [$HOST] - dialing [$DNID] - " "$EMAIL" &) | nohup
 

lcsneil

Member
Joined
Sep 21, 2008
Messages
47
Reaction score
4
Help! - I'm trying to adapt this script to check for extensions being on line (as I have a couple of extensions on different continents and they drop out sometimes when the remote site is unaware their internet/modem is down).

Anyway its going well but I have one bit that isnt working. Can someone tell me what I am doing wrong.
note the values of $extensntype = sip and $_ = 101 (read from a file)

Code:
my $cmnd = "$extensntype show peer $_";
 
my $siptrunks = `/usr/sbin/asterisk -rx "$cmnd" | grep \"Status\" | awk '{
 
print \$3
 
}
 
'`;


If I print the value of $cmnd it comes out as sip show peer 101

However the above script doesnt work. The $3 variable doesn't give the value "OK"

IF however I use the following script then the $3 variable is outputted correctly. i.e. does equal "OK"

Code:
my $cmnd = "$extensntype show peer $_";
my $siptrunks = `/usr/sbin/asterisk -rx "sip show peer 101" | grep \"Status\" | awk '{
 print \$3
 }
 '`;

Any ideas why the $cmnd variable isnt substituting properly in the my $siptrunks line?

TIA

Neil
 

scurry7

Member
Joined
Jun 29, 2010
Messages
49
Reaction score
1
Here is a script I created...

This is created to run at 6AM from a cron job and it will stop running at 6PM. It checks every 15 minutes to see is sip show peers has any unreachable peers.

crontab -e
0 6 * * 1-5 /root/sip_peers_unreachable.sh


nano /root/sip_peers_unreachable.sh

Code:
#!/bin/bash
COUNTER=0
DATE=`date "+%d.%m.%Y. %H:%M"`
while [ "$COUNTER" != 48 ]
        do
        test=`/usr/sbin/asterisk -rx "sip show peers"|grep UNREACHABLE`
        if [ -z "$test" ]; then
        sleep 900
        else
        top=`top -bn1 | tail`
echo -e "$test\n$top" | mail -s "SIP Peer Unreachable `hostname` $DATE" [email protected]
        sleep 300
        fi
let "COUNTER++"
done


chmod +x /root/sip_peers_unreachable.sh
 

lcsneil

Member
Joined
Sep 21, 2008
Messages
47
Reaction score
4
Well I finally got it working although not overly elegantly. I would have used the script by scurry7 above but I only wanted to test for certain extensions not all on my system as some of them are sometimes deliberately down.

I also run travelin man 3 and found some issues with my iptables so also restart them for good measure as well if I get a fail (as the remote end isnt fixed IP) - comment that out if you dont need it.

For anyone interested ......

I couldn't get the variables to concatenate properly hence the full command line in the .conf file rather than just the extension numbers.

The two config files are in /etc/asterisk/ and they are used to monitor my 3 SIP extensions (101,102 & 103) and my one IAX2 extension (197)

They are extensnalerts_iax2.conf
(with contents)
iax2 show peer 197


AND extensnalerts_sip.conf
(with contents)
sip show peer 101
sip show peer 102
sip show peer 103

I then run the script using crontab every 15 minutes (which means when the intercontinental extension goes down over night (due to the remote dodgy internet) I wake up to 30+ emails. :)

The script is called extensnalerts.pl and located in /usr/sbin/

Code:
#!/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 = "me\@email.com" ;
my $pbxdomain = "mydomain.org" ;
#### comment out whichever of the following two lines are not relevant for your server
checkextensn("sip");
checkextensn("iax2");
##################################################
##################################################
####
#### extensn 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 extensns 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 extensn 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 extensn alert file.
##################################################
##################################################
####
#### Create the following files in /etc/asterisk (if you want to only check sip extensns don’t create the other file etc)
#### - /etc/asterisk/extensnalerts_sip.conf
#### - /etc/asterisk/extensnalerts_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 extensns 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 checkextensn {
# the next line gets the extensntype parameter passed to the subroutine
my $extensntype=shift;
print $extensntype;
####
print "=============================================\n";
print "$extensntype extensn information\n";
print "extensnalerts_$extensntype.conf\n";
print "=============================================\n";
open(EXTENSNLIST,"/etc/asterisk/extensnalerts_$extensntype.conf");
while (<EXTENSNLIST>) {
chomp;
my $recheckcounter = 1;
my $upcounter = 5;
my $dncounter = 5;
until ($upcounter == 0) {
my $extn = `/usr/sbin/asterisk -rx "$_" | grep \"Status\" | awk '{print \$3}'`;
my $cmnd = "$extensntype show peer $_";
# print "the cmnd variable is - $cmnd\n";
# print "the awk command outputs $3\n\n";
# print "ppppppp $extensntype extn $_ = $extn\n"; #Remove remark to see output from "sip show peer" command
if ($extn =~ "OK") {
$upcounter = $upcounter - 1;
# print $upcounter,"\n"; #Remove remark to see counter decrement
# print "$_ is up\n" ; #Remove remark to see notification of extensn being "UP"
}
else {
$dncounter = $dncounter - 1;
# print $dncounter,"\n"; #Remove remark to see counter decrement once extensn is found to be down
# print "$_ is down\n" ; #Remove remark to see notification of extensn being "DOWN"
if ($dncounter == 0) {
if ($recheckcounter == 1) {
# we now reload the extn and force the check to occur again
`asterisk -rx "module reload"`;
# print "Reloading extn (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
# extensn connections after a reload
`sleep 20`;
`/usr/bin/iptables-restart`; # You dont need this if you dont have problems with Travelin' Man3 or you dont run it
`sleep 20`;
$recheckcounter = 0;
$dncounter = 5;
}
else
{
mailalert();
$upcounter = 0;
# print $upcounter,"\n"; #Remove remark to see counter decrement once extensn is found to be down
print "houston we have a problem\n";
print "$_ extensn is not registering\n";
$recheckcounter = 1;
}
}
}
}
}
#end of while loop (read extensn 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 $digi = substr($_, -3);
my $subject = "Subject: Extension $digi is DOWN!!!!\n";
my $content = "PBX extension $_ 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";
}
 
Last edited:

GreenLantern

New Member
Joined
Dec 19, 2019
Messages
1
Reaction score
0
Here is a script I created...

This is created to run at 6AM from a cron job and it will stop running at 6PM. It checks every 15 minutes to see is sip show peers has any unreachable peers.

crontab -e
0 6 * * 1-5 /root/sip_peers_unreachable.sh


nano /root/sip_peers_unreachable.sh


Code:
#!/bin/bash
COUNTER=0
DATE=`date "+%d.%m.%Y. %H:%M"`
while [ "$COUNTER" != 48 ]
        do
        test=`/usr/sbin/asterisk -rx "sip show peers"|grep UNREACHABLE`
        if [ -z "$test" ]; then
        sleep 900
        else
        top=`top -bn1 | tail`
echo -e "$test\n$top" | mail -s "SIP Peer Unreachable `hostname` $DATE" [email protected]
        sleep 300
        fi
let "COUNTER++"
done


chmod +x /root/sip_peers_unreachable.sh

I've been using scurry7's script for years. Thank you so much.

I recently tried to set it up on a new FreePBX 14 system, but can't get it to work.

I think maybe something is different with FreePBX 14 that will require an updated script, maybe just new syntax or something.

Is anyone still using this, and has it working on FreePBX 14?

Here are some other notes from over the years...

1. I had to change the script name from "sip_peers_unreachable.sh" to "sip_peers_alert.sh" because the script name itself was triggering "unreachable" alerts.

2. I changed the 2nd sleep to 900 seconds, instead of 300 seconds, because I was getting duplicate triggers on the same event.

3. I'd like to try tweaking the sleep delays to the minimum time possible, but not sure how to calculate it, or even to "triail by error".

4. At some point, my postfix email stopped sending, and I had to search for a fix. Found it at link below.

Basically, had to edit /etc/postfix/main.cf and change the "inet_interfaces = all" to "inet_interfaces = 127.0.0.1, [pbx ip address]" where pbx ip address is the actual ip address of the system. That got email working again.

Thanks in advance for any help to get this great script running again.
 

Members online

Forum statistics

Threads
25,824
Messages
167,826
Members
19,249
Latest member
jetest
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top