2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief Asterisk Call Manager CDR records.
24 * \arg \ref Config_ami
25 * \ingroup cdr_drivers
28 /*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
29 * \addtogroup configuration_file Configuration Files
33 * \page cdr_manager.conf cdr_manager.conf
34 * \verbinclude cdr_manager.conf.sample
38 <support_level>core</support_level>
42 <managerEvent language="en_US" name="Cdr">
43 <managerEventInstance class="EVENT_FLAG_CDR">
44 <synopsis>Raised when a CDR is generated.</synopsis>
46 <parameter name="AccountCode">
47 <para>The account code of the Party A channel.</para>
49 <parameter name="Source">
50 <para>The Caller ID number associated with the Party A in the CDR.</para>
52 <parameter name="Destination">
53 <para>The dialplan extension the Party A was executing.</para>
55 <parameter name="DestinationContext">
56 <para>The dialplan context the Party A was executing.</para>
58 <parameter name="CallerID">
59 <para>The Caller ID name associated with the Party A in the CDR.</para>
61 <parameter name="Channel">
62 <para>The channel name of the Party A.</para>
64 <parameter name="DestinationChannel">
65 <para>The channel name of the Party B.</para>
67 <parameter name="LastApplication">
68 <para>The last dialplan application the Party A executed.</para>
70 <parameter name="LastData">
72 The parameters passed to the last dialplan application the
76 <parameter name="StartTime">
77 <para>The time the CDR was created.</para>
79 <parameter name="AnswerTime">
81 The earliest of either the time when Party A answered, or
82 the start time of this CDR.
85 <parameter name="EndTime">
87 The time when the CDR was finished. This occurs when the
88 Party A hangs up or when the bridge between Party A and
92 <parameter name="Duration">
93 <para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
95 <parameter name="BillableSeconds">
96 <para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
98 <parameter name="Disposition">
99 <para>The final known disposition of the CDR.</para>
101 <enum name="NO ANSWER">
102 <para>The channel was not answered. This is the default disposition.</para>
105 <para>The channel attempted to dial but the call failed.</para>
107 <para>The congestion setting in <filename>cdr.conf</filename> can result
108 in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
109 <literal>CONGESTION</literal> dial status to map to this disposition.
114 <para>The channel attempted to dial but the remote party was busy.</para>
116 <enum name="ANSWERED">
117 <para>The channel was answered. The hang up cause will no longer
118 impact the disposition of the CDR.</para>
120 <enum name="CONGESTION">
121 <para>The channel attempted to dial but the remote party was congested.</para>
125 <parameter name="AMAFlags">
126 <para>A flag that informs a billing system how to treat the CDR.</para>
129 <para>This CDR should be ignored.</para>
131 <enum name="BILLING">
132 <para>This CDR contains valid billing data.</para>
134 <enum name="DOCUMENTATION">
135 <para>This CDR is for documentation purposes.</para>
139 <parameter name="UniqueID">
140 <para>A unique identifier for the Party A channel.</para>
142 <parameter name="UserField">
144 A user defined field set on the channels. If set on both the Party A
145 and Party B channel, the userfields of both are concatenated and
146 separated by a <literal>;</literal>.
152 The <replaceable>Cdr</replaceable> event is only raised when the
153 <filename>cdr_manager</filename> backend is loaded and registered with
158 This event can contain additional fields depending on the configuration
159 provided by <filename>cdr_manager.conf</filename>.
163 </managerEventInstance>
167 #include "asterisk.h"
171 #include "asterisk/channel.h"
172 #include "asterisk/cdr.h"
173 #include "asterisk/module.h"
174 #include "asterisk/utils.h"
175 #include "asterisk/manager.h"
176 #include "asterisk/config.h"
177 #include "asterisk/pbx.h"
179 #define DATE_FORMAT "%Y-%m-%d %T"
180 #define CONF_FILE "cdr_manager.conf"
181 #define CUSTOM_FIELDS_BUF_SIZE 1024
183 static const char name[] = "cdr_manager";
185 static int enablecdr = 0;
187 static struct ast_str *customfields;
188 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
190 static int manager_log(struct ast_cdr *cdr);
192 static int load_config(int reload)
195 struct ast_config *cfg;
196 struct ast_variable *v;
197 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
198 int newenablecdr = 0;
200 cfg = ast_config_load(CONF_FILE, config_flags);
201 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
205 if (cfg == CONFIG_STATUS_FILEINVALID) {
206 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
211 /* Standard configuration */
212 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
214 ast_cdr_backend_suspend(name);
221 ast_rwlock_wrlock(&customfields_lock);
224 if (reload && customfields) {
225 ast_free(customfields);
229 while ( (cat = ast_category_browse(cfg, cat)) ) {
230 if (!strcasecmp(cat, "general")) {
231 v = ast_variable_browse(cfg, cat);
233 if (!strcasecmp(v->name, "enabled"))
234 newenablecdr = ast_true(v->value);
238 } else if (!strcasecmp(cat, "mappings")) {
239 customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
240 v = ast_variable_browse(cfg, cat);
242 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
243 if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
244 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
245 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
247 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
258 ast_rwlock_unlock(&customfields_lock);
261 ast_config_destroy(cfg);
264 ast_cdr_backend_suspend(name);
265 } else if (newenablecdr) {
266 ast_cdr_backend_unsuspend(name);
268 enablecdr = newenablecdr;
273 static int manager_log(struct ast_cdr *cdr)
275 struct ast_tm timeresult;
276 char strStartTime[80] = "";
277 char strAnswerTime[80] = "";
278 char strEndTime[80] = "";
279 char buf[CUSTOM_FIELDS_BUF_SIZE];
284 ast_localtime(&cdr->start, &timeresult, NULL);
285 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
287 if (cdr->answer.tv_sec) {
288 ast_localtime(&cdr->answer, &timeresult, NULL);
289 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
292 ast_localtime(&cdr->end, &timeresult, NULL);
293 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
296 ast_rwlock_rdlock(&customfields_lock);
297 if (customfields && ast_str_strlen(customfields)) {
298 struct ast_channel *dummy = ast_dummy_channel_alloc();
300 ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
303 ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
304 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
305 ast_channel_unref(dummy);
307 ast_rwlock_unlock(&customfields_lock);
309 manager_event(EVENT_FLAG_CDR, "Cdr",
310 "AccountCode: %s\r\n"
312 "Destination: %s\r\n"
313 "DestinationContext: %s\r\n"
316 "DestinationChannel: %s\r\n"
317 "LastApplication: %s\r\n"
323 "BillableSeconds: %ld\r\n"
324 "Disposition: %s\r\n"
329 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
330 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
331 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
332 ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
337 static int unload_module(void)
339 if (ast_cdr_unregister(name)) {
344 ast_free(customfields);
349 static int load_module(void)
351 if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
352 return AST_MODULE_LOAD_DECLINE;
355 if (load_config(0)) {
356 ast_cdr_unregister(name);
357 return AST_MODULE_LOAD_DECLINE;
360 return AST_MODULE_LOAD_SUCCESS;
363 static int reload(void)
365 return load_config(1);
368 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
369 .support_level = AST_MODULE_SUPPORT_CORE,
371 .unload = unload_module,
373 .load_pri = AST_MODPRI_CDR_DRIVER,