SUGGESTIONS Email Alert On Trunk Failure

edisoninfo

Guru
Joined
Nov 19, 2007
Messages
505
Reaction score
4
Did you create the trunkalerts_iax.conf file? You may not have any IAX trunks but you still need to create the file I think.
 

gbrook

Member
Joined
Dec 6, 2007
Messages
74
Reaction score
0
Contents

Could you please post the contents of the trunkalerts_sip.conf and trunkalerts_iax.conf in /etc/asterisk

My Line 18 is
open(SIPTRUNKS,"/etc/asterisk/siptrunks");
Have you added another line ?
Who owns the iax and sip files?

Cheers
Garry
 

shane

Member
Joined
Dec 19, 2008
Messages
77
Reaction score
1
Could you please post the contents of the trunkalerts_sip.conf and trunkalerts_iax.conf in /etc/asterisk

sip.flowroute.com:5060 - This is what was shown under host when I ran atsterisk -rx "sip show registry"

My Line 18 is
open(SIPTRUNKS,"/etc/asterisk/siptrunks");
Have you added another line ?

I have not added any new lines, I did go through and delete some blank lines and made sure that all the lines were ending correctly. I also ran dos2unix since I copied the file from windows.

Who owns the iax and sip files?

root owns both files

Thanks for helping me try to solve this problem. :smile5:
 

gbrook

Member
Joined
Dec 6, 2007
Messages
74
Reaction score
0
OK

My file shows 202.169.178.10:5060 but that is only because I use hard addresses

OK

Mine are owned by asterisk but that should not be the issue.

Can you post the two lines you have with IAXTRUNKS and SIPTRUNKS Please.
The only issues I ever had was in the email area.

Cheers
Garry
 

shane

Member
Joined
Dec 19, 2008
Messages
77
Reaction score
1
OK

Can you post the two lines you have with IAXTRUNKS and SIPTRUNKS Please.
The only issues I ever had was in the email area.

open(IAXTRUNKS,"/etc/asterisk/trunkalerts_iax.conf");
open(SIPTRUNKS,"</etc/asterisk/trunkalerts_sip.conf");
 

gbrook

Member
Joined
Dec 6, 2007
Messages
74
Reaction score
0
Hi Shane,

Unless the the actual issue is in the following line:-

open(SIPTRUNKS,"</etc/asterisk/trunkalerts_sip.conf");

The "<" should not be there.

If that doesn't fix it, try commenting out the line with a #

Failing that you could pm me the file and I will have a look

Cheers
Garry
 

shane

Member
Joined
Dec 19, 2008
Messages
77
Reaction score
1
Unless the the actual issue is in the following line:-

open(SIPTRUNKS,"</etc/asterisk/trunkalerts_sip.conf");

The "<" should not be there.

I did not think that was supposed to be there either so I had removed it. Which did not make a difference since the file is failing on the line before. When I comment out the IAX line it fails with the same error on the SIP line.

Just to see what would happen I commented out both of the open() lines which then caused the same error but for this line:

while (<SIPTRUNKS>) :crazy:

Is there a possiblity that I am executing the program wrong or using the interpreter? I am not very familiar with bash or perl but I read somewhere about not having the right version of perl may cause a problem like this.

Thank you for taking the time to help me solve this. I will send you the file to see if you can see anything in it.
 

gbrook

Member
Joined
Dec 6, 2007
Messages
74
Reaction score
0
Hi Shane,

Send me the file and I will try it on my system as the file I posted worked on my system.

garry at palamo dot com dot au

I usually test it from cron within Webmin

Cheers
Garry
 

shane

Member
Joined
Dec 19, 2008
Messages
77
Reaction score
1
To help anyone else who is as dense as I am, do not forget the very first line of code #!/usr/bin/perl :banghead:

Thank you for the help Gary. :D
 

cwpippin

New Member
Joined
May 25, 2008
Messages
15
Reaction score
0
*** Updated ***
I updated the code and added a new counter for the down state, called dncounter. It works beautifully now. It will count or decrement from 5 down to 0 when checking trunks. Meaning it will try each trunk 5 times (Which can be adjusted in the upcounter and dncounter settings). If the trunk is found to be down it starts counting with the the dncounter and decrements it by 1. It then steps through the while loop again for the remaining status checks. If it finds that a trunk is down for 5 times (or polls) in a single run of the script, than it sets the upcounter to zero to stop the while loop and goes to the mail routine and sends an email.

Ok.. I am by no means a programmer but I wanted this script to loop as I get some situations where the system will check the trunks and they are online but either my ISP latency or the speed of the system causes a false positve for trunk failure and an email is sent off.

So, here it is if you are interested.

Code:
#!/usr/bin/perl -w
use strict;
use warnings;
#no warnings; #Remark out to suppress ALL warnings, also add remark to above line.
################################################## ##################################################
####
#### 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
####
################################################## ##################################################
####
#### Create the following 2 files in /etc/asterisk
####
#### in the files below add the hosts entry from asterisk -rx "sip show registry" and
#### from asterisk -rx "iax2 show registry".
####
################################################## ##################################################
####
#### SIP Related Code
####
#print "=============================================\n";
#print "SIP Trunk information\n";
#print "=============================================\n";
open(SIPTRUNKS,"</etc/asterisk/trunkalerts_sip.conf");
while (<SIPTRUNKS>) {
    chomp;
    my $upcounter = 5;
    my $dncounter = 5;
    until ($upcounter == 0) {
        my $siptrunks = `/usr/sbin/asterisk -rx "sip show registry" | grep \"$_\" | awk '{
            print \$4
        }
        '`;
        #print "siptrunks = $siptrunks\n"; #Remove remark to see output from "sip show registry" command
        if ($siptrunks =~ "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) {
                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";
            }
        }
    }
}
#end of while loop (read SIP file)
################################################## ##################################################
####
#### IAX Related Code
####
#print "\n\n";
#print "=============================================\n";
#print "IAX2 Trunk information\n";
#print "=============================================\n";
open(IAXTRUNKS,"&lt;/etc/asterisk/trunkalerts_iax.conf");
while (<IAXTRUNKS>) {
    chomp;
    my $upcounter = 5;
    my $dncounter = 5;
    until ($upcounter == 0) {
        my $iaxtrunks = `/usr/sbin/asterisk -rx "iax2 show registry" |/bin/grep \"$_\" | awk '{
            print \$5
        }
        '`;
        #print "iaxtrunks = $iaxtrunks\n"; #Remove remark to see output from "iax2 show registry" command
        if ($iaxtrunks =~ "Registered") {
            $upcounter = $upcounter - 1;
            #print $counter,"\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) {
                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";
                my $subject = "Subject: TRUNK $iaxtrunks is DOWN!!!!\n";
                my $content = "TRUNK $iaxtrunks is DOWN!!!!\n";
            }
        }
    }
}
#end of while loop (read IAX file)
################################################## ##################################################
####
#### Email Subroutines
#### Change anywhere below where there is an email address an email addres
#### must have \@ as perl needs to escape the @ symbol
####
################################################## ##################################################
sub mailalert {
    my $sendmail = "/usr/sbin/sendmail -t";
    my $from= "FROM: <pbx\@YOURCOMPANYNAME.com>\n";
    my $reply_to = "Reply-to: <pbx\@YOURCOMPANYNAME.com\n";
    my $subject = "Subject: $_ is DOWN!!!!\n";
    my $content = "PBX TRUNK $_ is DOWN!!!!\n";
    my $send_to = "To:<YOU\@YOUREMAILADDRESS.com>\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";
}



Here is a flowchart of how it works:

trunkalerts.gif
 

cwpippin

New Member
Joined
May 25, 2008
Messages
15
Reaction score
0
On a seperate box or on the PIAF server?

Tell us more..
 

robinmurray

New Member
Joined
Mar 8, 2008
Messages
7
Reaction score
0
Any zenoss or nagios examples

Does anyone want to share their zenoss or nagios examples...
please..

Robin
 

Speedy2k

Member
Joined
Oct 11, 2008
Messages
387
Reaction score
0
It would be nice if we can make a amportal restart when this script detect a trunk failure, i don't know hot to send this command to the shell in perl, i have tried
`amportal restart`;
but it don't seems to be working pretty well, if someone know how to do it thanx!
 

jroper

Guru
Joined
Oct 20, 2007
Messages
3,832
Reaction score
71
You should not nee anything as drastic as an amportal restart.

A simple asterisk -rx "module reload" or if you want to restart asterisk asterisk -rx "restart now" would do.

The second command should be used with care, as that will cut off calls in progress, and a frozen trunk can usually be cleared with a reload.

Also you would want to be careful not to end up in some loop of constant reloading. It's proabable better to fix the underlying problem.

Joe
 

Speedy2k

Member
Joined
Oct 11, 2008
Messages
387
Reaction score
0
It must have a way to tell him to try to reload the module and wait 2 minutes, and recheck if the IAX2 trunk is registered, if still not, it can restart asterisk, and wait 2 minutes, and if it still fail, send an e-mail and make the other cron job be skipped, i don't know how to do this,but it should be doable o and it should be the best wayto do this ?

If there is something i don't understand, if i run my script in the shell, it is working perfectly, is it sees the trunk is not registered, it make a convenient restart, but as soon as i put it in a cron job, it check if the trunk is ok, but does not restart asterisk, i have set the cron job to run as root, is there something else i need to do??

Here is the script.


#!/usr/bin/perl -w
use strict;
use warnings;
#no warnings; #Remark out to suppress ALL warnings, also add remark to above line.
################################################## ##################################################
####
#### 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
####
################################################## ##################################################
####
#### Create the following 2 files in /etc/asterisk
####
#### in the files below add the hosts entry from asterisk -rx "sip show registry" and
#### from asterisk -rx "iax2 show registry".
####
################################################## ##################################################
####
#### SIP Related Code
####
#print "=============================================\n";
#print "SIP Trunk information\n";
#print "=============================================\n";
open(SIPTRUNKS,"/etc/asterisk/trunkalerts_sip.conf");
while (<SIPTRUNKS>) {
chomp;
my $upcounter = 5;
my $dncounter = 5;
until ($upcounter == 0) {
my $siptrunks = `/usr/sbin/asterisk -rx "sip show registry" | grep \"$_\" | awk '{
print \$5
}
'`;
#print "siptrunks = $siptrunks\n"; #Remove remark to see output from "sip show registry" command
if ($siptrunks =~ "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) {
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";
}
}
}
}
#end of while loop (read SIP file)
################################################## ##################################################
####
#### IAX Related Code
####
#print "\n\n";
#print "=============================================\n";
#print "IAX2 Trunk information\n";
#print "=============================================\n";
open(IAXTRUNKS,"/etc/asterisk/trunkalerts_iax.conf");
while (<IAXTRUNKS>) {
chomp;
my $upcounter = 5;
my $dncounter = 5;
until ($upcounter == 0) {
my $iaxtrunks = `/usr/sbin/asterisk -rx "iax2 show registry" |/bin/grep \"$_\" | awk '{
print \$6
}
'`;
#print "iaxtrunks = $iaxtrunks\n"; #Remove remark to see output from "iax2 show registry" command
if ($iaxtrunks =~ "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) {
#mailalert();
$upcounter = 0;
#print $upcounter,"\n"; #Remove remark to see counter decrement once trunk is found to be down
print "Erreur:\n";
print "$_ trunk is not registering\n";
print "Nous allons reloader les modules\n";
print `asterisk -rx "restart when convenient"\n`;
print `echo -e Solutions Nursing Mtl trunk is DOWN, Nous avons fais un reload de module. | mail -s "Solutions Nursing Mtl trunk have failed" speedy_2k2\@hotmail.com\n`;
my $subject = "Subject: TRUNK $iaxtrunks is DOWN!!!!\n";
my $content = "TRUNK $iaxtrunks is DOWN!!!!\n";
}
}
}
}
#end of while loop (read IAX file)
################################################## ##################################################
####
#### Email Subroutines
#### Change anywhere below where there is an email address an email addres
#### must have \@ as perl needs to escape the @ symbol
####
################################################## ##################################################
sub mailalert {
my $sendmail = "/usr/sbin/sendmail -t";
my $from= "FROM: <pbx\@solutionsnursing.ca>\n";
my $reply_to = "Reply-to: <pbx\@solutionsnursing.ca\n";
my $subject = "Subject: $_ is DOWN!!!!\n";
my $content = "PBX TRUNK $_ is DOWN!!!!\n";
my $send_to = "To:<speedy_2k2\@hotmail.com.com>\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";
}
 

cwpippin

New Member
Joined
May 25, 2008
Messages
15
Reaction score
0
Maybe try setting the script to run as "asterisk" instead of "root"..
I dunno.. :crazy:
 

Phil_Garner

New Member
Joined
Dec 5, 2008
Messages
2
Reaction score
0
I have added the .agi script and followed the instructions in this thread . I can see it checking the script but it doesn't send an email after a failed call (changing SIP secret to simulate trunk failure). I am running freepbx 2.5.1.5 on asterisk 1.4.24.1 can anybody help me with this?
 

Phil_Garner

New Member
Joined
Dec 5, 2008
Messages
2
Reaction score
0
what is the command?

When I try to check the config at the command line I get the message below:

[root@infinityip ~]# /usr/sbin $ perl trunkalerts.pl
-bash: /usr/sbin: is a directory

As I cannot start this service I cannot set up the cron job because I am unsure of the correct command. I am very new to all of this but would like to set this up as we have been having trouble with our broadband of late.

Thanks in advance.
 

Members online

Forum statistics

Threads
25,782
Messages
167,509
Members
19,202
Latest member
pbxnewguy
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