FreeSWITCH Dialer – Importing CDR Logs

Introduction

Following our first tutorial, this tutorial teaches you how to properly mine the logs provided by FreeSWITCH.

The previous tutorial assumed that your FreeSWITCH installation path is the default /opt/freeswitch. This document also makes the same assumption.

Logs Processing

Problem: You may need to have a list of calls initiated, allong with statistics like calls duration, reasons for unaswered calls and what keys where pressed during that call (the works!).

Configuring mod_xml_cdr

We have two choices for logging CDR records from FreeSWITCH: mod_xml_cdr and mod_cdr_csv.

Although you may feel like a CSV file is easier to parse and work with, the CSV format is not flexibile enough (you need to manually specify the fields you want logged) and whenever you add or remove fields, parsers depending on the old format may become easily invalidated. mod_xml_cdr exports all available fields and channel variables by default, and it’s a more reliable format for logging.

To activate mod_xml_cdr you need to add to/opt/FreeSWITCH/config/autoload_configs/modules.conf.xml the following line:

    <load module="mod_xml_cdr"/>

Also, in /opt/FreeSWITCH/config/autoload_configs/xml_cdr.conf.xml you have to activate log-b-leg by adding/editing the following line:

     <param name="log-b-leg" value="true"/>

log-b-leg is activating the logging of a bridged call. For example, let’s say that you want to connect your current session to another phone, using the bridge command. With log-b-legyou can get all the necessary info about the bridged session, like duration or if the phone answered or not. FreeSWITCH will create a second record for it, with which you can work with.

Importing Logs in Your Database

Reading Logs

By default, logs are created in /var/log/cdr-xml. By default files are named with the uuid of the call. But other than the phone number, by default, you can’t reliably connect a FreeSWITCH uuid to a record in the database.

The fields available, that may be of interest, are the following:

sip_destination_url call phone number
endpoint_disposition call result. Equal to ANSWER if succesfull
hangup_cause reason of failure, if endpoint_disposition is not ANSWER(NORMAL_CLEARING if succesfull, USER_BUSY, NO_ANSWER, CALL_REJECTED, DESTINATION_OUT_OF_ORDER, NORMAL_TEMPORARY_FAILURE, NO_ROUTE_DESTINATION, NO_USER_RESPONSE)
billmsec Call duration in milliseconds
answer_stamp Timestamp when the phone was answered
end_stamp Timestamp when the call ended

Of course, there are other fields that may be of interest, but these are the most important.

Adding Helpers

If you use your database for keeping track of what calls you made, you need to pass a $db_row_id as a channel variable. Any channel variables you pass when making the call connection (through the originate command), will be available in the cdr records of the call.

Example (Javascript):

    var DB_ROW_ID = argv[0];
    var PHONE     = argv[1];
 
    // ...
 
    session = new Session('{db_row_id=' + DB_ROW_ID + ',is_bridged=0}sofia/external/' + PHONE_NUMBER + '@myprovider.com');

And then, when you want to bridge the current session to another phone number:

    // ...
 
    var new_session = new Session();
    new_session.originate(session, 
        "{db_row_id=" + DB_ROW_ID + ",is_bridged=1}sofia/external/" + SECOND_PHONE + "@myprovider.com");
    new_session.waitForAnswer(10000);
 
    if (new_session.ready())
        bridge(session, new_session);

In this example, the jscript is passed the $db_row_id and the $phone as parameters. When opening the channel, we pass two custom channel variables: db_row_id and is_bridged. These variables will be available in the log files.