FREESWITCH Mod - CDR - CSV 250519 1726 5024
FREESWITCH Mod - CDR - CSV 250519 1726 5024
About
This module allows you to log call detail records (CDRs) to a text file using text generation templates. These templates along with the default configuration
are stored in the file <tt>''freeswitch''/conf/autoload_configs/cdr_csv.conf.xml</tt>. Which template is used for the default is defined by the following line
(which uses "example" in the default installation).
The Basics
This module allows you to log call detail records (CDRs) to a text file using text generation templates. These templates along with the default configuration
are stored in the file <tt>''freeswitch''/conf/autoload_configs/cdr_csv.conf.xml</tt>. Which template is used for the default is defined by the following line
(which uses "example" in the default installation).
Further down in the cdr_csv.conf.xml file, the "example" template is defined: <tt><template name="example"> ... </template ></tt>. Therefore to
change what is being logged you may edit the example template or create a new template and update the <tt>default-template</tt> parameter to that
template name.
Default configuration
'''conf/autoload_configs/cdr_csv.conf.xml'''
XML Structure
The following params can be set under the settings tag in your ''cdr_csv.conf.xml'' file.
debug
When set to true, this prints out all channel data in your log when creating a new CDR record is generated.
default-template
The name of a template as defined in the 'templates' section of your cdr_csv.conf.xml
legs
Choose whether the module will save cdr's for the a or b leg, or both of them.
log-base
The directory where the csv files will be written to.
master-file-only
When enabled, all cdr's will be written to a single file instead of to multiple files split up per accountcode.
rotate-on-hup
When enabled, after sending a HUP signal to FreeSWITCH, the logfile will be rotated to a format like this:
Master.csv.YYYY-mm-dd-HH-MM-SS
API
To rotate CDRs (This method will rotate only CSV CDRs without HUPing the entire FS)
cdr_csv rotate
Variables in a Template
Variables are given in the form ${varname}. For an overview of variables, check Channel Variables. You can specify any channel variable.
<template name="custom_timestamp">"${expr(${end_epoch}-${my_timestamp})}"</template>
Here is a sample CDR of a call that was successful, we'll use this to refer to each variable below.
"xtec","2185738219","919814440333","default",""2185738219" <2185738219>","sofia/in/2185738219@89.212.283.238:
5060","sofia/out/+919814440333
begin_of_the_skype_highlighting +919814440333 end_of_the_skype_highlighting
begin_of_the_skype_highlighting +919814440333 end_of_the_skype_highlighting@222.199.89.27","
bridge","sofia/out/+919814440203@222.199.89.27","2008-02-11 07:01:49","2008-02-11 07:02:07","2008-02-11 07:27:
13","1524","1506","NORMAL_CLEARING","","c60a1f09-ce61-47d3-a806-82bd2c9a4b6a","89.202.183.238"
this particular CDR was made with the following template (note, it doesn't have bleg_uuid)
The API call sql_escape can be used to escape these variables to generate valid SQL, for example:
NOTE: if the value of the accountcode variable matches the name of a template then that template will be used. This is valuable for having a specific
template be used on a per-call basis.
amaflags
Automatic Message Accounting, an archaic system that "flags" CDR for post-processing. Commonly used flags are billing, default, documentation, omit.
answer_epoch
UNIX epoch value when the call was answered, eg. 1294060468 for Mon, 03 Jan 2011 13:14:28 GMT.
answer_stamp
Timestamp when the call was answered (eg, SIP 200 OK is received), in ISO 8601 format (YYYY-MM-DD hh:mm:ss), in the local timezone (not UTC). If
the call is not answered, this will be an empty string.
billsec
The answered or billing span of the calls in seconds, i.e. "end_stamp - answer_stamp". Should only be > 0 in calls where HANGUP_CAUSE ==
NORMAL_CLEARING (16).
bleg_uuid
The UUID of the bleg, i.e. the called party or terminating end.
bleg_uuidaccountcode
The accountcode of the bleg_uuid in the call.
bridge_channel
The channel name of the bleg that the aleg was bridged to. e.g. sofia/out/+919814440333@222.199.89.27
call_clientcode
caller_id
caller_id_name
See Channel_Variables#caller_id_name
caller_id_number
See caller_id_number
channel_name
This is the channel_name of the A-leg originating end. In this case its sofia/in/2185738219@89.212.283.238:5060
context
The context where the call first entered the dialplan, in this case default.
destination_number
This is the number (rather, extension) specified by the A-leg. In the example above its 919814440333. The dialplan can perform any mutation on this
number before its actually dialed out (in the case above a + is added).
duration
The entire duration of the call, end to end in seconds, i.e. "start_stamp - end_stamp". Duration should always be >= billsec.
end_epoch
UNIX epoch value when the call ended, eg. 1294060468 for Mon, 03 Jan 2011 13:14:28 GMT.
end_stamp
Timestamp when the call was hung up, in ISO 8601 format (YYYY-MM-DD hh:mm:ss), in the local timezone (not UTC).
hangup_cause
See Hangup Causes
last_app
The last application run by the call, in the case of our example, its bridge.
last_arg
The arguments passed to the last_app, i.e. sofia/out/+919814440203@222.199.89.27
read_codec
See Channel_Variables#read_codec
sip_gateway_name
See How does FreeSWITCH CDR determine which gateway was used in failover?
sip_rtp_rxstat
sip_user_agent
sofia_record_file
start_epoch
UNIX epoch value when the call was started, eg. 1294060468 for Mon, 03 Jan 2011 13:14:28 GMT.
start_stamp
Timestamp when the call was started, in ISO 8601 format (YYYY-MM-DD hh:mm:ss), in the local timezone (not UTC).
userfield
A field to set your own custom values. For example
uuid
A unique identifier for the call. A UUID is a block of 16 bytes ref. This is the uuid of the aleg of the call, i.e. the calling party or origination end. According to r
ef UUIDs are formatted as: 00112233-4455-6677-8899-AABBCCDDEEFF
write_codec
See Channel_Variables#write_codec
Examples
SQL Script to create MySQL CDR table for default "example" CDRs
To confirm that the mod_cdr_csv is set for the "sql" template, check the file
/usr/local/freeswitch/conf/autoload_configs/cdr_csv.conf.xml
The default is the example template. If that is indeed the case, the script below is just what you will need to create a the cdr table.
MySQL schema
For PostgreSQL
PostgreSQL schema
What this does is allow the Master.csv and other CDR files to be rotated via the same method as used by the mod_logfile method. The script below uses
one of these methods to rotate the log file and load the file into a database. (The naming format for the rotated files is from e.g. Master.csv to Master.csv.
yyyy-mm-dd-hh-mm-ss)
Create the script using the code below, and run through cron every minute if you like.
Example Perl script
#!/usr/bin/perl -w
use strict;
use DBI();
use File::Copy;
# this command sends -SIGHUP to fs, she creates new cdr.csv files, so we can load the old ones up
my @cc = ("killall", "-HUP", "freeswitch");
system(@cc) == 0 or die "$0: system @cc failed: $?";
# one silly thing is that each accountcode has its own cdr.csv as well, either handle those here, by loading
them into their own tables, or rm them
my @BS = ("xtec","megaphone","mafcom","xeivacom");
foreach my $code (@BS) {
@LS = `ls -1t /usr/local/freeswitch/log/cdr-csv/$code.csv.*`;
foreach my $line (@LS) {
chop($line);
#unlink($line); #to delete
move($line, "/usr/local/freeswitch/log/cdr-csv/trash/$code/"); # or move into a separate dir for later
procession
}
}
exit 0;