freeswitch的mod_cdr_csv模块
概述
freeswitch是一款简单好用的VOIP开源软交换平台。
在语音呼叫的过程中,话单是重要的计价和结算依据,话单的产生需要稳定可靠,可回溯。
fs中基本的话单模块mod_cdr_csv,可以满足大部分话单要求。
环境
centos:CentOS release 7.0 (Final)或以上版本
freeswitch:v1.10.7
GCC:4.8.5
模块介绍
mod_cdr_csv模块默认编译安装,并有加载。
检查conf/autoload_configs/modules.conf.xml文件。
<load module="mod_cdr_csv"/>
cdr_csv模块会在通话结束后写入话单记录到文本文件,话单的格式根据配置的模板确定。
检查conf/autoload_configs/cdr_csv.conf.xml文件,默认配置如下。
<configuration name="cdr_csv.conf" description="CDR CSV Format">
<settings>
<!-- 'cdr-csv' will always be appended to log-base -->
<!--<param name="log-base" value="/var/log"/>-->
<param name="default-template" value="example"/>
<!-- This is like the info app but after the call is hung up -->
<!--<param name="debug" value="true"/>-->
<param name="rotate-on-hup" value="true"/>
<!-- may be a b or ab -->
<param name="legs" value="a"/>
<!-- Only log in Master.csv -->
<!-- <param name="master-file-only" value="true"/> -->
</settings>
<templates>
<template name="sql">INSERT INTO cdr VALUES ("${caller_id_name}","${caller_id_number}","${destination_number}","${context}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${uuid}","${bleg_uuid}", "${accountcode}");</template>
<template name="example">"${caller_id_name}","${caller_id_number}","${destination_number}","${context}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${uuid}","${bleg_uuid}","${accountcode}","${read_codec}","${write_codec}"</template>
<template name="snom">"${caller_id_name}","${caller_id_number}","${destination_number}","${context}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${uuid}","${bleg_uuid}", "${accountcode}","${read_codec}","${write_codec}","${sip_user_agent}","${call_clientcode}","${sip_rtp_rxstat}","${sip_rtp_txstat}","${sofia_record_file}"</template>
<template name="linksys">"${caller_id_name}","${caller_id_number}","${destination_number}","${context}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${uuid}","${bleg_uuid}","${accountcode}","${read_codec}","${write_codec}","${sip_user_agent}","${sip_p_rtp_stat}"</template>
<template name="asterisk">"${accountcode}","${caller_id_number}","${destination_number}","${context}","${caller_id}","${channel_name}","${bridge_channel}","${last_app}","${last_arg}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${amaflags}","${uuid}","${userfield}"</template>
<template name="opencdrrate">"${uuid}","${signal_bond}","${direction}","${ani}","${destination_number}","${answer_stamp}","${end_stamp}","${billsec}","${accountcode}","${userfield}","${network_addr}","${regex('${original_caller_id_name}'|^.)}","${sip_gateway_name}"</template>
</templates>
</configuration>
默认配置中,fs使用“example”模板写话单文件“log/cdr_csv/Master.csv”。
其中几个比较有用的配置项解释如下。
<param name="debug" value="true"/>
debug参数在调试时比较有用,作用是通话结束后通过“info“命令打印出AB路的全部通道变量,这样就可以更容易知道在话单模板中应该如何设置自己需要的字段。
2023-07-05 10:48:13.704735 [INFO] mod_cdr_csv.c:219 CHANNEL_DATA:
Channel-State: [CS_REPORTING]
Channel-Call-State: [HANGUP]
Channel-State-Number: [11]
Channel-Name: [sofia/external4080/13712345678@10.55.55.138:5090]
Unique-ID: [5d7f96d0-1ade-11ee-a6e6-ef73ce267dbc]
Call-Direction: [outbound]
…
2023-07-05 10:48:13.724676 [INFO] mod_cdr_csv.c:219 CHANNEL_DATA:
Channel-State: [CS_REPORTING]
Channel-Call-State: [HANGUP]
Channel-State-Number: [11]
Channel-Name: [sofia/external/10011@10.55.55.138]
Unique-ID: [5d7e804c-1ade-11ee-a6dd-ef73ce267dbc]
Call-Direction: [inbound]
…
<param name="legs" value="ab"/>
话单记录可以选择写入“a“、”b“和”ab“,分别表示A路话单,B路话单,AB路双话单。
"10011","10011","13712345678","out_to_in_public","2023-07-05 10:48:04","2023-07-05 10:48:09","2023-07-05 10:48:13","9","4","NORMAL_CLEARING","5d7f96d0-1ade-11ee-a6e6-ef73ce267dbc","","","","","outbound","16","recv_bye","sip:200","10.55.55.138",""
"10011","10011","13712345678","out_to_in_public","2023-07-05 10:48:04","2023-07-05 10:48:09","2023-07-05 10:48:13","9","4","NORMAL_CLEARING","5d7e804c-1ade-11ee-a6dd-ef73ce267dbc","5d7f96d0-1ade-11ee-a6e6-ef73ce267dbc","","","","inbound","16","send_bye","","10.55.55.138",""
<param name="master-file-only" value="true"/>
当我们需要根据“accountcode”来区分客户的话单模板格式时,fs默认会根据模板名称产生多个csv文件,该参数设置话单记录只写入Master.csv文件。
自定义话单模板
修改conf/autoload_configs/cdr_csv.conf.xml文件,配置如下。
<template name="example">"${caller_id_name}","${caller_id_number}","${destination_number}","${context}","${start_stamp}","${answer_stamp}","${end_stamp}","${duration}","${billsec}","${hangup_cause}","${uuid}","${bleg_uuid}","${accountcode}","${read_codec}","${write_codec}","${direction}","${hangup_cause_q850}","${sip_hangup_disposition}","${proto_specific_hangup_cause}","${network_addr}","${rtp_audio_in_mos}"</template>
模板中的字段如果不能满足实际需求,可以通过debug模式查看AB路所有的通道变量。
多模板场景的话单
某些情况下,我们需要对不同的场景输出不同的话单格式。
fs提供通道变量“accountcode”来区分模板场景。
修改dialplan拨号计划如下。
<action application="export" data="accountcode=asterisk" />
在log/cdr_csv目录下产生两个文件“Master.csv”和”asterisk.csv”。
cat asterisk.csv
"asterisk","10011","13712345678","out_to_in_public",""10011" <10011>","sofia/external4080/13712345678@10.55.55.138:5090","","","","2023-07-05 15:30:34","2023-07-05 15:30:38","2023-07-05 15:30:55","21","17","NORMAL_CLEARING","","d44d3fe8-1b05-11ee-a6a1-875ed88826f8",""
"asterisk","10011","13712345678","out_to_in_public",""10011" <10011>","sofia/external/10011@10.55.55.138","","bridge","{sip_h_Route=<sip:10.55.55.138:5090;lr>,sip_invite_call_id=abaa3d90-95a8-123c-7ebf-b6fcd7842446,sip_invite_domain=test01.9999.com,sip_cid_type=pid}{}sofia/external4080/sip:13712345678@10.55.55.138:5090","2023-07-05 15:30:34","2023-07-05 15:30:38","2023-07-05 15:30:55","21","17","NORMAL_CLEARING","","d44c4570-1b05-11ee-a697-875ed88826f8",""
cat Master.csv
"10011","10011","13712345678","out_to_in_public","2023-07-05 15:30:34","2023-07-05 15:30:38","2023-07-05 15:30:55","21","17","NORMAL_CLEARING","d44d3fe8-1b05-11ee-a6a1-875ed88826f8","","asterisk","","","outbound","16","recv_bye","sip:200","10.55.55.138",""
"10011","10011","13712345678","out_to_in_public","2023-07-05 15:30:34","2023-07-05 15:30:38","2023-07-05 15:30:55","21","17","NORMAL_CLEARING","d44c4570-1b05-11ee-a697-875ed88826f8","d44d3fe8-1b05-11ee-a6a1-875ed88826f8","asterisk","","","inbound","16","send_bye","","10.55.55.138",""
话单文件分割
mod_cdr_csv模块提供了一个api命令“cdr_csv rotate“,可以根据实际需要来分割话单文件。
freeswitch@pxy137> cdr_csv rotate
+OK
2023-07-05 14:55:21.524681 [NOTICE] mod_cdr_csv.c:127 Rotated CDR logfile ../log/cdr-csv/Master.csv
-rw-r--r--. 1 root root 0 Jul 5 14:55 Master.csv
-rw-r--r--. 1 root root 5914 Jul 5 10:48 Master.csv.2023-07-05-14-55-21
总结
freeswitch的基本话单模块mod_cdr_csv,输出稳定高效,配置灵活,字段选择多。
空空如常
求真得真