Monitor Trunk Failures AGI script
I am looking for a script to configure Monitor Trunk Failure. Here is what I have found so far.
#!/bin/bash
echo -e "Vitelity trunk is DOWN @ office" | mail -s "Failed call on Vitelity " admin@compnay.com
reload IAX2
I named the file trunkfail.sh and put it in /var/lib/asterisk/agi-bin directory
If I run: echo -e "Vitelity trunk is DOWN @ office" | mail -s "Failed call on Vitelity " admin@compnay.com in a terminal session I can receive an email. Great! I added the” reload IAX2” line. Shouldn’t that reload the IAX trunk?
Is there another script that I cannot find that other people use? I could not find any documentation on this after searching here, Google and FreePBX. Maybe I am making this harder than I need to. Basically I want to get the customer back online ASAP by running reload IAX2 and be notified that there is a problem. I’d like to calling or emailing them rather than the other way around.
Does anybody monitor their installs with Nagios or Groundwork? I am looking at those two now any input on that front would be welcomed.
Thanks, Blake
Ummm, Yes, TDF, that helps. I did not finish reading the thread thre I guess. I found it but stopped at the script above.
Thanks, Blake
I came up with a slight variation that you can schedule through cron. It appears that it's working correctly as I have gotten a few notifications, but have never had an outage.
It builds on the above link, and I in now way remember what parts of it I changed.
I'm not sure why the counters are nessecary, but I left them in because I'm not a perl programmer.
If anyone has a better implementation by now, please let me know as I am having trouble with my provider, and really need this functionality.
Here we go:
#!/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
####
my $date = `date`;
print $date;
print "=============================================\n";
print "SIP Trunk information\n";
print "=============================================\n";
open(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();
print `/usr/bin/asterisk -rx "module reload chan_sip"`;
$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,"
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 $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();
print `/usr/bin/asterisk -rx "module reload chan_iax2"`;
$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:
my $reply_to = "Reply-to:
my $content = "PBX TRUNK $_ is DOWN!!!!\n A reload of the trunk has been processed";
my $send_to = "To:
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 `asterisk -rx "restart when convenient"\n`;
print "An email has been sent!\n\n";
}
Hi,
Can you please post the chmod or any other file permission to be applied on these script to make them actualy run ?
Thanks
Alex
chmod +x filename
Extensions are irrelevant in Linux (for the most part)
I have tried some shell scripts and the above Perl script. Nothing has worked for me. Perl scripts needs extra files from CPAN, eg. Proc::PID etc. So I have put together a php script. This is working for me when installed to the current version of trixbox.
I have ever have sip accounts. The script does not report IAX, but I think it will be simple enough to add some code to report IAX status. If someone can post a usual IAX status report. eg. the result of asterisk -rx "iax2 show registry"., I will add more code.
#!/usr/bin/php -q
<?php
/** ***********************************************************
* A quick and dirty way to report a failing trunk registration.
* Run this regularly, hopefully you will get an alert about one or more failing trunks.
* You run this script at your own risk.
* Not sure what damage this can cause, but I think a bathroom leakage should have nothing to do with this.
* 2009-11 Nikol S ns at eyo.com.au
* *************************************************************
*/
// To setup,
// 1. Change the report_email to yours.
// 2. Put this file somewhere (eg. /root/cron/monitor_trunk.php), chmod it to 750. Either owned by root or asterisk.
// 3. Edit /var/spool/cron/root, and add
// */15 * * * * /root/cron/monitor_trunk.php
// to the file. Last line in the cron file must have a carriage return!
// Above entry in the cron file will run the script every 15 minutes.
// set up the email address to receive the alert email
$report_email = 'your@emailemail.com';
// No need to edit below, unless you need or want.
if (wots_up() == 'requesting')
{
//give it 10 seconds, we try again.
sleep(20);
}
$state = wots_up();
if ($state == 'requesting')
{
send_alert_email('Trunk has been sending registrating requests', $status);
}
elseif ($state == 'empty')
{
send_alert_email('Voip trunk registration status is empty', $status);
}
elseif ($state == 'no_auth')
{
send_alart_email('No Authentication is reported, wrong password?', $status);
}
elseif ($state == 'unregistered')
{
send_alart_email('We have Unregistered trunk', $status);
}
elseif ($state <> 'ok')
{
send_alert_email('Trunk status is not registered', "$state\r\n\r\n$status");
}
function wots_up()
{
global $status;
//monitor trunks, make sure it is registered.
$status = shell_exec('/usr/sbin/asterisk -rx "sip show registry"');
if (strlen(trim($status)) == 0)
{
return 'empty';
}
$lines = explode("\n", str_replace("\r\n", "\n", trim($status)));
for ($i = 1; $i < count($lines) -1; $i++)
{
if (strpos($lines[$i], 'Request Sent') !== false)
{
return 'requesting';
}
elseif(strpos($lines[$i], 'No Authentication') !== false)
{
return 'no_auth';
}
elseif(strpos($lines[$i], 'Unregistered') !== false)
{
return 'unregistered';
}
$temp = preg_split('/\s+/', $lines[$i]);
if (isset($temp[3]) AND $temp[3] <> "Registered")
{
return $lines[$i];
}
}
return 'ok';
}
function send_alert_email($subject, $email_content = '')
{
global $report_email;
mail($report_email, $subject, $email_content);
}
?>
You have two typos there where you're calling send_alart_email instead of send_alert_email.
Also I believe if you have more than one sip trunk you're only every checking the first one. Your "return 'ok'" there are the end renders the loop useless.
Thank you very much for pointing out the typos! Hehe.
return ok only happens at the end fo the loop, ie. All lines have "Registered" tag as well as not other funny states. I in fact have three sip channels, it detected the third line's fault when I tried. This is a very rough code with no much test. So, please welcome inputs and corrections.
Edited and removed code. [ php ] BBcode removes the "\", so reposted in [ code ]
Code Removed
Edited with slightly changed code.
2009-11-04. Edited to fix the asterisk location. /usr/bin/asterisk => /usr/sbin/asterisk
#!/usr/bin/php -q
<?php
/**
* To monitor the trunk registration, despatch email when non-registered trunk is detected
* @package Eyo
* @version $Revision: 1.2 $
* @file $RCSfile: monitor_trunk.php,v $
* @date $Date: 2009/11/02 04:31:29 $
* @author $Author: nsu $
*/
/** ***********************************************************
* A quick and dirty way to report a failing trunk registration.
* Run this regularly, hopefully you will get an alert about one or more failing trunks.
* You run this script at your own risk.
* Not sure what damage this can cause, but I think a bathroom leakage should have nothing to do with this.
* Nikol S ns at eyo.com.au
* *************************************************************
*/
// To setup,
// 1. Change the report_email to yours.
// 2. Put this file somewhere (eg. /root/cron/monitor_trunk.php)
// chmod it to 750(#chmod 750 monitor_trunk.php). Either owned by root or asterisk(#chown root.root monitor_trunk.php).
// 3. Edit /var/spool/cron/root, and add
// */15 * * * * /root/cron/monitor_trunk.php
// to the file. Last line in the cron file must have a carriage return!
// Above entry in the cron file will run the script every 15 minutes.
// set up the email address to receive the alert email
$report_email = 'your@emailemail.com';
// No need to edit below, unless you need or want.
$state = wots_up();
//echo $state;
if ($state == 'ok')
{
//grace, usually should go this route
exit;
}
else
{
//keep cool for 20 seconds, we try again.
sleep(20);
$state = wots_up();
$have_done = "We have retried in 20 seconds.\r\n";
}
if ($state == 'ok')
{
//lucky we tried
exit;
}
else
{
$have_done .= "Trying to reload the sip channels.\r\n";
$have_done .= shell_exec('/usr/sbin/asterisk -rx "module reload chan_sip"');
sleep(20);
$have_done .= "Reloaded sip channels and waited 20 seconds.\r\n";
$state = wots_up();
}
if ($state == 'ok')
{
//wipes the sweat
exit;
}
$status = "$have_done\r\n=====================have done above, the current status: =====================\r\n$status";
if ($state == 'requesting')
{
send_alert_email('Trunk has been sending registrating requests', $status);
}
elseif ($state == 'empty')
{
send_alert_email('Voip trunk registration status is empty', $status);
}
elseif ($state == 'no_auth')
{
send_alert_email('No Authentication is reported, wrong password?', $status);
}
elseif ($state == 'unregistered')
{
send_alert_email('We have Unregistered trunk', $status);
}
elseif ($state == 'failed')
{
send_alert_email('We have Registration failed trunk', $status);
}
elseif ($state == 'auth_sent')
{
send_alert_email('We have trunk with Auth. Sent status', $status);
}
elseif ($state == 'rejected')
{
send_alert_email('We have trunk with Rejected status', $status);
}
else
{
send_alert_email('Trunk status is not registered', "$state\r\n\r\n$status");
}
function wots_up()
{
global $status;
//Prob the trunk registration.
$status = shell_exec('/usr/sbin/asterisk -rx "sip show registry"');
if (strlen(trim($status)) == 0)
{
return 'empty';
}
$lines = explode("\n", str_replace("\r\n", "\n", trim($status)));
for ($i = 1; $i < count($lines) - 1; $i++)
{
//echo "line $i " . $lines[$i] . "\n";
if (strpos($lines[$i], 'Request Sent') !== false)
{
return 'requesting';
}
elseif(strpos($lines[$i], 'No Authentication') !== false)
{
return 'no_auth';
}
elseif(strpos($lines[$i], 'Unregistered') !== false)
{
return 'unregistered';
}
elseif (strpos($lines[$i], 'Failed') !== false)
{
return 'failed';
}
elseif (strpos($lines[$i], 'Auth. Sent') !== false)
{
return 'auth_sent';
}
elseif (strpos($lines[$i], 'Rejected') !== false)
{
return 'rejected';
}
elseif (strpos($lines[$i], 'Timeout') !== false)
{
return 'timeout';
}
elseif (strpos($lines[$i], 'Unknown') !== false)
{
return 'unknown';
}
$temp = preg_split('/\s+/', $lines[$i]);
if (!isset($temp[3]))
{
return "Can not extract Reg State for this line: " . $lines[$i];
}
elseif ($temp[3] <> "Registered")
{
return "Unknown Reg state for this line: " . $lines[$i];
}
}
return 'ok';
}
function send_alert_email($subject, $email_content = '')
{
global $report_email;
mail($report_email, $subject, $email_content);
}
?> Ah that looks much better with the code tags. It's obvious now the return 'ok' works, hurrah for indentation :)
I created a version of my script above that runs persistantly as a daemon. It probably is not as cool as the php one as far as ease of install, but it seems to work for me.
If anyone want's it, let me know.
and this is the error that i'm getting
Parse error: syntax error, unexpected T_VARIABLE in /usr/src/monitor_trunk.php on line 34
i'm using postfix as email service (dont know if that help) :)
i'm not to familiar with scripting and php, any help will be appreciate it


Member Since:
2008-08-25