1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. If you had a PIAF Forum account in the vBulletin days, log in with your old credentials. Otherwise, sign up again and we'll get you back in business as soon as we can.
  3. A serious FreePBX vulnerability has been reported. Update your Framework Module immediately. Click here for details.

Email Alert On Trunk Failure

Discussion in 'Add-On Install Instructions' started by Hometech, Aug 10, 2008.

  1. Hometech Member

    Is there any way to have pbxiaf email a short message when a trunk is not connected to its provider? as well as emailing when its back up? Looking for a way to remote monitor with out dialing in to check if a trunk has died.

    Thanks
  2. TDF New Member

    Does your version of FreePBX not have the option 'monitor trunk failures' in the trunk settings.

    It needs a script to work with, but I'm sure someones already wrote one.
  3. Hometech Member

    I will check that. but will that email me on a trunk falure?
  4. TDF New Member

  5. joevoirol New Member

    Monitor Trunk Failure

    :banghead:
    Can anyone provide some detail on the Monitor Trunk Failure feature?

    i.e.
    What type of script does it need to be?
    Where does the file reside?
    What triggers it?

    I have looked at the FreePBX site and I cannot find any documentation on it at all.

    Thanks
  6. thunderheart New Member

    Script

    Joe,

    If you follow the link TDF provided above, it points to a script that should work. Possibly with a little modification. The example is a bash script which pretty much makes sense. I think if you put something like: /foo/trunk_failure.sh in the "Monitor Trunk Failures" box in FPBX, and the corresponding bash script lived in /foo it would work. I've been meaning to look into this myself but haven't had time. I imagine it is triggered by a registration failure ... not sure though.

    Dallas
  7. joevoirol New Member

    Tried that.

    :banghead:
    All I want to do is send an email when the trunk fails registration. Here are the contents of the script:

    #!/bin/bash

    echo -e "Vitelity trunk is DOWN" | mail -s "Failed call on Vitelity" somebody@company.com

    This is a bash script so the file name is mailtest.sh and is located in the /var/lib/asterisk/agi-bin directory. The Monitor Trunk Failure is set to this path and file and is enabled.

    This script works from the command line and sends email.

    Does anyone know if this feature of FrePBX actually works? I have not been able to find anything else about it on the internet except the post referenced by TDS. The script TDS references does not work at all, there are about 25 errors when I run it from the command line. I am in the process of trying to fix it. I cannot find any documentation on the FreePBX site about this feature.

    Any help would be appreciated.

    Thanks
  8. thunderheart New Member

    I'll give it a whirl Joe

    Like I said above, I've wanted to get this working myself (even though my trunks to VOIPStreet never seem to loose registration). Just seems like I good thing to have. If I can figure it out I'll post back.

    Dallas
  9. gbrook New Member

    I have a script that works and is called by cron at set periods eg 15 minutes.

    It sends an email for each trunk that is not registered. The script came from another community member

    I can PM if needed

    Cheers
    Garry
  10. TheShniz Guru

    ^^^ I'm interested :D
  11. klingon888 Guru

    Philippe Lindheimer's description of how this shld work - http://www.freepbx.org/forum/freepbx/users/monitor-trunk-failures-any-scripts

    I've put the script that TDF referenced on one of my trunks and I'll let you know if it works when this trunk fails! Managed to clean out the syntax errors. Code below. Can others try it out too?

    #!/bin/bash

    EMAIL=xxx@xxx.com

    DATE=`date "+%m-%d-%Y %Hh%M"`

    HOST=`hostname`

    CID=`echo $agi_callerid`
    CIDNAME=`echo $agi_calleridname`
    EXTENSION=`echo $agi_extension`
    CHANNEL=`echo $agi_channel`

    REPORT=$EXTENSION

    if [ "$EXTENSION" = "s-CHANUNAVAIL" ]
    then
    REPORT="Channel Unavailable"
    fi

    # Message Formating

    MSG="\n\n A call from channel [$CHANNEL] \n"
    MSG="$MSG With CID : [$CIDNAME] - [$CID] \n\n"
    MSG="$MSG has failed on host [$HOST] at $DATE \n\n"
    MSG="$MSG with error : [$REPORT] \n\n\n"

    # log

    echo >> /var/log/asterisk/trunkfailure.log
    echo "$MSG" >> /var/log/asterisk/trunkfailure.log

    # Send an email

    echo -e "$MSG" | mail -s " - Failed call on host [$HOST] -" "$EMAIL"
  12. thunderheart New Member

    Ok ... worked for me.

    What I did was put the script in:

    /var/lib/asterisk/agi-bin/trunk_monitor.agi

    Then in 'Monitor Trunk Failure' I put 'trunk_monitor.agi'

    Made sure the script was asterisk:asterisk and +x

    Changed the secret for my trunk (to simulate a failure) and then made an outgoing call. Instantly got the email delivered.

    So you don't put the full pathname in the 'Monitor Trunk Failure' field. Just the filename. FreePBX looks for it in /var/lib/asterisk/agi-bin

    Seems a little stiff to me but there it is. The script posted above by Klingon is a little buggy. Try it like this:

    #!/bin/bash

    declare -a array
    while read -e ARG && [ "$ARG" ] ; do
    array=(` echo $ARG | sed -e 's/://'`)
    export ${array[0]}=${array[1]}
    done

    EMAIL=you@your.domain

    DATE=`date "+%m-%d-%Y %H:%M"`

    HOST=`hostname`

    CID=`echo $agi_callerid`
    CIDNAME=`echo $agi_calleridname`
    EXTENSION=`echo $agi_extension`
    CHANNEL=`echo $agi_channel`

    REPORT=$EXTENSION

    if [ "$EXTENSION" = "s-CHANUNAVAIL" ]
    then
    REPORT="Channel Unavailable"
    fi

    # Message Formating

    MSG="\n\n A call from channel [$CHANNEL] \n"
    MSG="$MSG With CID : [$CIDNAME] - [$CID] \n\n"
    MSG="$MSG has failed on host [$HOST] at $DATE \n\n"
    MSG="$MSG with error : [$REPORT] \n\n"

    # log

    echo >> /var/log/asterisk/trunkfailure.log
    echo -e "$MSG" >> /var/log/asterisk/trunkfailure.log

    # Send an email

    echo -e "$MSG" | mail -s " - Failed call on host [$HOST] -" "$EMAIL"

    Works a bit better. Still not convinced if this really helps much. How would ya know if incoming calls were broken? Fortunately, my carrier has a fail-over number which I set to one of our ZAP DNs. As far as I know we've never had a failover though. VOIPStreet is pretty darn reliable.

    Dallas
  13. gbrook New Member

    How do I attach or please provide your email address

    Cheers
    Garry
  14. joevoirol New Member

    Trunk not registered.

    :smile5:
    The script provided and the Monitor Trunk Failure feature work OK but it only notifies you when a call fails on everything except BUSY and CANCEL. I need to know before a call fails, like when the trunk is not registered.

    Here is the perl script we used for that and the configuration file that goes with it (and Jim Hribnak, thank you):

    #!/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
    ####


    ###############################################################################################################################
    ####
    #### Create the following 2 files in /etc/asterisk
    ####
    #### if 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.conf");
    open(SIPTRUNKS,"</etc/asterisk/trunkalerts_sip.conf");


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

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

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

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

    } else {
    mailalert();
    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============================================================\n";
    #print "IAX2 Trunk information\n";
    #print "============================================================\n";

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

    #$iaxtrunks = `asterisk -rx "iax2 show registry" |grep \"$_\" | awk '{print $5}'`;

    #print "iaxtrunks = $iaxtrunks\n";

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

    } else {
    mailalert();
    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 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: <user\@domain.com>\n";
    my $reply_to = "Reply-to: <user\@domain.com\n";
    my $subject = "Subject: $_ is DOWN!!!!\n";
    my $content = "TRUNK $_ is DOWN!!!!\n";
    my $send_to = "To:<user\@domain.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";
    }
    trunkalerts_iax.conf and trunkalerts_sip.conf are text files and contain the IP address : port of your provider (SIP or IAX)

    xx.xx.xx.xx:4569

    Each trunk is on a separate line.

    The file is trunkalerts.pl placed in the /usr/sbin. The iax and sip files are trunkalerts_iax.conf and trunkalerts_sip.conf placed in the /etc/asterisk so they can be edited in the FreePBX, Tools, Config Edit screen.

    I had problems with this script at first but eventually my boss got it working on his TB machine at home and we cloned it to our PIAF machine at the office and it works.

    I have it going to my cell phone so I know when it is down. You create a cron job and run it at whatever interval you think is appropriate for your situation.

    Thanks for all of the insightful information.
  15. leonash07 New Member

    Joe/Anyone,
    When I tested the perl script
    root@pbx:/usr/sbin $ perl trunkalerts.pl
    It output as follows:
    sip.callwithus.com is up
    proxy01.sipphone.com is up.

    However, when the cron job runs I get following error message on /var/spool/mail/root file when the cron job runs.
    Error:
    sh: asterisk: command not found
    sh: asterisk: command not found

    Script output is as follows:
    sip.callwithus.com trunk is not registering
    proxy01.sipphone.com trunk is not registering

    :banghead:I'm not sure what I'm doing wrong here. It seems like when the job runs it doesn't understand asterisk command.
    If you know how to fix this or guide me on this I would appreciate it.

    Leo
  16. jroper Guru

    Your faith in me is touching, but probably misplaced;-)

    I think it might be a permissions or ownership,either of the cron job, or the script or the conf files

    Joe
  17. klingon888 Guru

    Change :

    $siptrunks = `asterisk -rx "sip show registry" |grep \"$_\" | awk '{print $4}'`;

    To:

    $siptrunks = `/usr/sbin/asterisk -rx "sip show registry" |grep \"$_\" | awk '{print $4}'`;
  18. leonash07 New Member

    klingon888,

    I made the change and it solve my problem. Thank so much!!!!:thumbsup:
  19. marv Member

    Great trunk monitoring script

    The monitoring of the trunk registrations is exactly what I've been looking for. I have been troubleshooting and monitoring the trunk providers I have now because people have been telling me their calls were not going through. I like the proactive approach of assuring the trunk is registered versus you can't make an outbound call failure. Just makes logical sense to me. I believe if more people were to install this script and monitor their trunks, they might see that their trunks are having problems that they may not have been aware of.

    I personally have it setup to run every minute right now. I had a strange occurrence yesterday where I lost all of my registrations and I didn't realize it until I tried making an outbound call today. I might tweak the script a little down the road for more bells and whistles... but it's great now. Thanks guys!
  20. shane Member

    I would love to get this script working but for the life of me cannot figure out why I am getting the following error when I try to run the script from the shell using the following command:
    root@pbx:/usr/sbin $./trunkalerts.pl

    I am getting the following error:
    ./trunkalerts.pl: line 18: syntax error near unexpected token `IAXTRUNKS,"/etc/asterisk/trunkalerts_iax.conf");

    Any help would be greatly appreciated.

Share This Page