Search code examples
asteriskasteriskami

Detecting when Asterisk rejects an incoming call via AMI


We have a number of Asterisk servers; occasionally, human error means someone messes up the dialplan and incoming calls are rejected:

NOTICE[27927][C-00000188]: chan_sip.c:26826 handle_request_invite: Call from 'upstream-peer' (192.168.1.1:5060) to extension '44123123123' rejected because extension not found in context 'ourcontext'.

I'd like to implement a small piece of code that will raise some kind of alert when this happens, so we can fix it quickly.

I can (and do) use AMI to get all sorts of events out of the server - when channels are created, when calls end etc - but I can't seem to find any event or command that will raise an AMI event when a call is rejected. Does anyone know if such a thing exists?


Solution

  • Thanks to @arheops for the inspiration: I'd added this to the bottom of the context dialplan:

    exten => _X.,1,UserEvent(InvalidExtn)
    exten => _X.,2,Verbose(ATTENTION: incoming call tried to go to ${EXTEN} which is not configured.)
    exten => _X.,3,Hangup
    

    _X. will catch any number; as the dialplan is parsed in order, then only (presumably invalid) numbers that have reached the bottom of the plan will be caught by this wildcard match.

    Then, in my monitoring application I handle the UserEvents event:

    manager.UserEvents += Manager_UserEvents;
    

    with something simple like:

    private static void Manager_UserEvents(object sender, UserEvent e)
    {
      if (e.UserEventName=="InvalidExtn")
      {
         Console.WriteLine($"INVALID CALL! To extension {e.Attributes["exten"]} on Asterisk server {e.Source.Hostname}");
      }
    }
    

    Not as "clean" as I've have liked it, but it appears to work.