SIP/IAX Trunk Monitor script

Discussion in 'Open Discussion' started by stucker, Aug 13, 2009.

  1. stucker

    stucker Member

    Joined:
    Oct 19, 2007
    Messages:
    74
    Likes Received:
    0
    For those whom may be interested in a working script to use in the event of a failed SIP/IAX trunk use the following: I take no credit just passing along what worked for me. Credit for the script is Jim Hribnak. Thanks Jim!
    For those of you (which are many) whom have a lot more experience then I, please feel free to improve this script and share. Thanks!

    1) create/place the following file: /etc/asterisk/trunkalerts_sip.conf
    # in file place whomever your trunk provider FQDN+port. You can also obtained this by using
    # /usr/sbin/asterisk -rx "sip show registry"
    # Owner of the file should be: asterisk asterisk
    iad.telasip.com:5060

    Note: I was unable to get this to trigger an email alert when used in the FreePBX Monitor Trunk field. However, I was able to get this working when adding this to the cron job as such: /etc/crontab
    * * * * * asterisk /var/lib/asterisk/agi-bin/trunkmon.agi
    Also, I was unable to get email alerts using sendmail as I saw in the logs that the email alerts were being sent back as nondeliverable. Eventhou voice mail worked fine using sendmail. So instead of mucking around with sendmail I changed to the devil I know PostFix. Thereafter all was well.

    2) Cut/Paste the following perl script to /var/lib/asterisk/agi-bin/trunkmon.agi


    #!/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.conf");

    ################################################## ################################################## ###########################
    ####
    #### 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: <root\@pbx.local>\n"; #replace xxx with your FROM email ID
    my $reply_to = "Reply-to: <email\@address.com\n";
    my $subject = "Subject: $_ is DOWN!!!!\n";
    my $content = "TRUNK $_ is DOWN!!!!\n";
    my $send_to = "To:<email\@address.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 `asterisk -rx "restart when convenient"\n`;
    print "An email has been sent!\n\n";
    }
     

Share This Page