How to log the DID of all incoming calls (including rejected ones) in FreePBX/Asterisk

One small problem with FreePBX/Asterisk installations is that if you deny anonymous inbound SIP calls (and you should be doing that to help keep your system secure), then any incoming calls on DIDs that don’t match one of your inbound routes will be quietly dropped, and will NOT appear in your CDR (call detail record). But maybe you’d like to see which DIDs calls are coming in on even if they are dropped, in case you have one or two DIDs that you don’t know about, particularly if you receive calls from multiple DIDs on the same trunks(s).

Normally, in your trunk configuration(s) you will have a line such as this:


All you need to do is change it so that it points to a custom context that you will place at the end of your existing /etc/asterisk/extensions_custom.conf file. So, you might change it to something like this:


And then, at the end of /etc/asterisk/extensions_custom.conf …

exten => _X!,1,TrySystem(echo “${EXTEN}” >> /var/log/dids)
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
exten => h,1,Macro(hangupcall,)

This will write the DID of each incoming call to the file /var/log/dids. Note that if you have already changed the context statement in your trunks to point to a custom context for some other reason, you can just add the first line of the above context as a new line in that context (don’t forget to change the line number 1 to n if it’s not the first statement in the context).

Also note that before you apply the configuration changes, from a Linux command prompt you should do

touch /var/log/dids

to create the file and then either make it world-writeable, or change the ownership to asterisk. Otherwise Asterisk won’t have permission to write to the file and nothing will be saved.

Over time the log file might get a bit large and will have many duplicates. So whenever you want to look at it, just run the the following command (you could also put this in a cron job and run it once daily to keep the file from growing huge):

/bin/sort -u -o /var/log/dids /var/log/dids

This will sort the lines in numerical order and remove the duplicates.

Once you have done this, you can take a look at /var/log/dids now and then and see if any pop up that you didn’t know you had. Your provider might be deliberately or inadvertently “gifting” you with an extra DID, or you might be getting charged for a DID you aren’t using! Either way, it’s good to know what’s being sent to your system.

By the way, you can easily combine this with the hint given in How to show the source DID in FreePBX call detail reports – simply add the line

exten => _X!,n,Set(CDR(accountcode)=${EXTEN})

between the “TrySystem” line and the “Goto” line in the context shown above.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

Recent Comments




GiottoPress by Enrique Chavez