#!/usr/bin/perl

################################################## ################################################## ###########################
####
#### 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
#### Called using Cron job

################################################## ################################################## ###########################
####
#### 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".
####

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

################################################## ################################################## ###########################
####
#### SIP Related Code
####

#print "================================================= ===========\n";
#print "SIP Trunk information\n";
#print "================================================= ===========\n";

while (<SIPTRUNKS>) {
chomp;
$siptrunks = `/usr/sbin/asterisk -rx "sip show registry" |grep \"$_\" | awk '{print $4}'`;

#print "siptrunks = $siptrunks\n";
if ($siptrunks =~ "Registered") {
#print "$_ is up\n" ;

} else {
#print "We have a problem\n";
print "$_ trunk is not registering\n";
mailalert();

}
} #end of while loop (read SIP file)

################################################## ################################################## ###########################
####
#### IAX Related Code
####

#print "\n\n============================================= ===============\n";
#print "IAX2 Trunk information\n";
#print "================================================= ===========\n";

while (<IAXTRUNKS>) {
chomp;
$iaxtrunks = `/usr/sbin/asterisk -rx "iax2 show registry" |/bin/grep \"$_\" | awk '{print $5}'`;

#print "iaxtrunks = $iaxtrunks\n";

if ($iaxtrunks =~ "Registered") {
#print "$_ is up\n" ;

} else {
mailalert();
print "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 SIP 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\@domain.com>\n"; #replace xxx with your FROM email ID
my $reply_to = "Reply-to: <support\@domain.com\n";
my $subject = "Subject: $_ is DOWN!!!!\n";
my $content = "TRUNK $_ is DOWN!!!!\n";
my $send_to = "To:<support\@domain.com>\n"; #replace xxx with your TO email ID
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);

#log
my $logfile = "/var/log/asterisk/trunkfailure.log";
my $date = localtime();
my $logmsg = "$date TRUNK $_ is down";
open LOGFILE, ">>$logfile" or die "cannot open logfile $logfile for append: $!";
print LOGFILE $logmsg, "\n";
close LOGFILE;

print "An email has been sent!\n\n";
}
