Notes, tests, directions, instructions...
Anything that could be useful once more (to someone)...


Freepbx / Asterisk » What number was called?

What number was called in (more trunks, same provider, wrong DID)?

(FreePBX Distro version, but might be applicable for other versions around too)

Keywords: Freepbx, asterisk, wrong incoming number (DID), more trunks, what number was called, inbound routes and trunk conditions.

I have got seven trunks, five of them from the same provider. Each trunk is related to one number (=DID). Some of the numbers go to IVR, some directly to extensions, it does not matter. I was expecting to see in Freepbx web-admin under "Reports" menu (=CDR database) inbound numbers that were called by each remote caller. It works this way for some trunks, there is usually a record in "channel" column saying SIP/xxxxxxxxx-yyyyy... (where xxxxxxxxxx is the number that was called from outside, also called a DID number. To be exact, xxxxxxxxx is the trunk name, as far as i know). So if anyone calls any of my numbers, I expected to see in this reports what number was called. Why? Some of the numbers are "secret", known only to some people and connecting directly to extensions. Others should get connected through IVR. Once I find out that  a "secret" number was revealed and called too often by public, I need to change the number.

But here comes the problem: For some reason, five of the numbers that are from the same provider are shown in the Channel column as to be all to one number/trunk. Let's call it numbers 111111111 to 555555555. Although inbound conditions work well e.g. number 333333333 gets directly connected to extension 123 while 111111111 goes to IVR, both calls are visible in Channel as SIP/111111111-.... I even tried to disable trunk 111111111, then all calls are logged as to be to SIP/222222222-.... even when I enable trunk 111111111 back-untlil I reboot the machine. Then, we are back at SIP/111111111. And yes, the other trunks from different providers (different IPs Asterisk is connected, to be exact) are logged as expected.

I have spent hours with tests and googling, looking for some misconfiguration of my trunks. Finally, I realized it: I was looking for bad keywords. This seems to be the way Asterisk (FreePBX) works. Once the IP is the same for all five trunks (same provider) and all trunks have "insecure=very" in its configuration (disabling insecure=very is not possible for me), the first trunk that matches the criteria is used and therefore logged in CDR. Which is obviously the first one in alphabetical order, unless you disable and reenable that trunk (without rebooting the machine-see above).

Now it is clear that I will not force asterisk without some hacking to use the trunk I was expecting. So I started searching other keywords in a way of finding/logging the number that was called and this and this was a big help. However, source codes in the first link were not complete at the time I was solving this.

Solution: Freepbx (and probably Asterisk at the present version) does not log inbound number (DID) in CDR database. This is promised to change in future versions, but not working for me now. From what I understand, there are two "free" columns in CDR database that we can possibly use, one is "userfield" and the other "accountcode". I have decided to use the second one.

1) First thing we have to do is to tell Freepbx to save DID into accountcode column in CDR database. SSH to your system and create a macro:
[root@freepbx]# vi /etc/asterisk/extensions_custom.conf
exten => _X!,1,Set(CDR(accountcode)=${EXTEN})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
exten => h,1,Macro(hangupcall,) the file with ZZ (shift-Z, shift-Z if in lowercase mode). Red text is the content of the file I added. If the file already exists with some content, add this text.

Now tell each trunk you wish to have incoming number recorded in a database by changing/adding the context. Here is my trunks configuration (web Admin/Setup/Trunks):

Trunk Name: 111111111
Outbound CallerID: 111111111
Outgoing Settins:
Trunk Name: 111111111

PEER Details:

Register String:


...where 111111111 is my phone number=username. Red line is the one we are talking about and is important to run the macro created. Now all incoming calls that pass through the trunk with context=custom-from-trunk-accountcode definition record DID number into the database.

2) Now we need to display "accountcode" column in "Reports", ok, let's call the column "Source DID" here. All you need is to create a new file /etc/asterisk/call-log-table.php containing PHP code. Again SSH as a root to your system and do:

[root@freepbx]# vi /etc/asterisk/call-log-table.php

$FG_TABLE_COL[]=array ("Calldate", "calldate", "18%", "center", "SORT", "19");
$FG_TABLE_COL[]=array ("Source DID", "accountcode", "10%", "center", "SORT", "20");
$FG_TABLE_COL[]=array ("Channel", "channel", "13%", "center", "", "30", "", "", "", "", "", "display_acronym");
$FG_TABLE_COL[]=array ("Source", "src", "14%", "center", "", "30");
$FG_TABLE_COL[]=array ("Clid", "clid", "26%", "center", "", "80",'','','','','','filter_html');

$FG_TABLE_COL[]=array ("Dst", "dst", "14%", "center", "SORT", "30");

$FG_TABLE_COL[]=array ("Disposition", "disposition", "9%", "center", "", "30");
if ((!isset($resulttype)) || ($resulttype=="min")) $minute_function= "display_minute";
$FG_TABLE_COL[]=array ("Duration", "duration", "6%", "center", "SORT", "30", "", "", "", "", "", "$minute_function");


// This Variable store the argument for the SQL query
//$FG_COL_QUERY='calldate, channel, src, clid, lastapp, lastdata, dst, dst, serverid, disposition, duration';

$FG_COL_QUERY='calldate, accountcode, channel, src, clid, dst, disposition, duration';

/* --AMP End -- */

$FG_COL_QUERY_GRAPH='calldate, duration';

// The variable LIMITE_DISPLAY define the limit of record to display by page

// Number of column in the html table

// The variable $FG_EDITION define if you want process to the edition of the database record

//This variable will store the total number of column

//This variable define the Title of the HTML table
$FG_HTML_TABLE_TITLE=" - Call Logs - ";

//This variable define the width of the HTML table


Again, press ZZ to save the file. By creating this file, it overrides the "call-log-table" defined in /var/www/html/admin/cdr/call-log.php. Once you delete /etc/asterisk/call-log-table.php, the original settings at  /var/www/html/admin/cdr/call-log.php will be used again (meaning without "Source DID" with values from "accountcode" database column).

Now, click on "Reports" and you should see new column called "Source DID". Call any of your trunk numbers and test it.