0

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?

KenD
  • 5,280
  • 7
  • 48
  • 85
  • Just open an AMI session and trigger the behaviour. Do you get an event? – miken32 Aug 08 '23 at 16:50
  • @miken32: no - hence the question :) I've tried all the AMI events: `NewChannel`, `NewExtension` etc, but nothing seems to raise an event. It feels like AMI only is available once something has matched in the dialplan ... – KenD Aug 09 '23 at 07:35

2 Answers2

1
  1. Setup ami or ARI session

  2. Catch "NewChannel" event

  3. If any other dialplan-type event come(like NewExtension) - mark it is okay.

  4. Catch Hangup event, if no diaplan mark - fire your procedure for incorrect dialplan.

Another option(low-code):

Setup invalid extension

exten => i,1,Noop(something here, no extension)
same => n,CelUserEvent(OOPS); for example fire cel event.
arheops
  • 15,544
  • 1
  • 21
  • 27
  • The `hangup` event isn't fired when an incoming call is rejected. However, I wonder if there's something in the second option - add a "catch all" match at the bottom of the dialplan context, and then detect for that being matched/executed ... – KenD Aug 09 '23 at 07:39
  • Dialplan above WILL be fired adn will give event. – arheops Aug 10 '23 at 04:26
0

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.

KenD
  • 5,280
  • 7
  • 48
  • 85
  • _. will catch any number. _X. will catch numbers starting with digits(ignore +1 for example) and at least 2 digits long. – arheops Aug 10 '23 at 04:27
  • Also monitoring VarSet is bad idea. For that you have UserEvent. VarSet is too common. – arheops Aug 10 '23 at 04:28
  • https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Application_UserEvent – arheops Aug 10 '23 at 04:29
  • Using `_.` throws a scary warning on the Asterisk console, hence me using `_X.` instead. I agree there's a few VarSet events, but I'm filtering those out in the C# code: but yes, UserEvent would probably be "lower volume" – KenD Aug 10 '23 at 08:13
  • edited solution now to use UserEvents, thank you :) – KenD Aug 10 '23 at 08:29