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

 
Search:


Freepbx / Asterisk » What number was called?

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

(FreePBX Distro version 1.86.29.55, 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
[custom-from-trunk-accountcode]
exten => _X!,1,Set(CDR(accountcode)=${EXTEN})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
exten => h,1,Macro(hangupcall,)

...save 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:
context=custom-from-trunk-accountcode
username=111111111
type=peer
secret=*********
port=5060
insecure=very
host=
*******IP-or-hostname-of-my-provider******
dtmfmode=rfc2833
call-limit=3
allow=g711
qualify=yes

Register String:

111111111:***MY-PASSWORD***@*******IP-or-hostname-of-my-provider******/111111111

...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");

$FG_TABLE_DEFAULT_ORDER = "calldate";
$FG_TABLE_DEFAULT_SENS = "DESC";

// 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
$FG_LIMITE_DISPLAY=25;

// Number of column in the html table
$FG_NB_TABLE_COL=count($FG_TABLE_COL);

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

//This variable will store the total number of column
$FG_TOTAL_TABLE_COL = $FG_NB_TABLE_COL;
if ((isset($FG_DELETION) && $FG_DELETION) || $FG_EDITION) $FG_TOTAL_TABLE_COL++;

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

//This variable define the width of the HTML table
$FG_HTML_TABLE_WIDTH="100%";

?>

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.

WEBEDITOR DOMÉNY s.r.o.