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>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47 #include "asterisk/channel.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/config.h"
53 #include "asterisk/pbx.h"
55 #define DATE_FORMAT "%Y-%m-%d %T"
56 #define CONF_FILE "cdr_manager.conf"
57 #define CUSTOM_FIELDS_BUF_SIZE 1024
59 static const char name[] = "cdr_manager";
61 static int enablecdr = 0;
63 static struct ast_str *customfields;
64 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
66 static int manager_log(struct ast_cdr *cdr);
68 static int load_config(int reload)
71 struct ast_config *cfg;
72 struct ast_variable *v;
73 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
76 cfg = ast_config_load(CONF_FILE, config_flags);
77 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
81 if (cfg == CONFIG_STATUS_FILEINVALID) {
82 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
87 /* Standard configuration */
88 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
90 ast_cdr_backend_suspend(name);
97 ast_rwlock_wrlock(&customfields_lock);
100 if (reload && customfields) {
101 ast_free(customfields);
105 while ( (cat = ast_category_browse(cfg, cat)) ) {
106 if (!strcasecmp(cat, "general")) {
107 v = ast_variable_browse(cfg, cat);
109 if (!strcasecmp(v->name, "enabled"))
110 newenablecdr = ast_true(v->value);
114 } else if (!strcasecmp(cat, "mappings")) {
115 customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
116 v = ast_variable_browse(cfg, cat);
118 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
119 if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
120 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
121 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
123 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
134 ast_rwlock_unlock(&customfields_lock);
137 ast_config_destroy(cfg);
140 ast_cdr_backend_suspend(name);
141 } else if (newenablecdr) {
142 ast_cdr_backend_unsuspend(name);
144 enablecdr = newenablecdr;
149 static int manager_log(struct ast_cdr *cdr)
151 struct ast_tm timeresult;
152 char strStartTime[80] = "";
153 char strAnswerTime[80] = "";
154 char strEndTime[80] = "";
155 char buf[CUSTOM_FIELDS_BUF_SIZE];
160 ast_localtime(&cdr->start, &timeresult, NULL);
161 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
163 if (cdr->answer.tv_sec) {
164 ast_localtime(&cdr->answer, &timeresult, NULL);
165 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
168 ast_localtime(&cdr->end, &timeresult, NULL);
169 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
172 ast_rwlock_rdlock(&customfields_lock);
173 if (customfields && ast_str_strlen(customfields)) {
174 struct ast_channel *dummy = ast_dummy_channel_alloc();
176 ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
179 ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
180 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
181 ast_channel_unref(dummy);
183 ast_rwlock_unlock(&customfields_lock);
185 manager_event(EVENT_FLAG_CDR, "Cdr",
186 "AccountCode: %s\r\n"
188 "Destination: %s\r\n"
189 "DestinationContext: %s\r\n"
192 "DestinationChannel: %s\r\n"
193 "LastApplication: %s\r\n"
199 "BillableSeconds: %ld\r\n"
200 "Disposition: %s\r\n"
205 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
206 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
207 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
208 ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
213 static int unload_module(void)
215 if (ast_cdr_unregister(name)) {
220 ast_free(customfields);
225 static int load_module(void)
227 if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
228 return AST_MODULE_LOAD_DECLINE;
231 if (load_config(0)) {
232 ast_cdr_unregister(name);
233 return AST_MODULE_LOAD_DECLINE;
236 return AST_MODULE_LOAD_SUCCESS;
239 static int reload(void)
241 return load_config(1);
244 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
246 .unload = unload_module,
248 .load_pri = AST_MODPRI_CDR_DRIVER,