Newer
Older
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2004 - 2005
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
* \brief Asterisk Call Manager CDR records.
* See also
* \arg \ref AstCDR
* \arg \ref AstAMI
* \arg \ref Config_ami
/*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
* \addtogroup configuration_file Configuration Files
*/
/*!
* \page cdr_manager.conf cdr_manager.conf
* \verbinclude cdr_manager.conf.sample
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*** DOCUMENTATION
<managerEvent language="en_US" name="Cdr">
<managerEventInstance class="EVENT_FLAG_CDR">
<synopsis>Raised when a CDR is generated.</synopsis>
<syntax>
<parameter name="AccountCode">
<para>The account code of the Party A channel.</para>
</parameter>
<parameter name="Source">
<para>The Caller ID number associated with the Party A in the CDR.</para>
</parameter>
<parameter name="Destination">
<para>The dialplan extension the Party A was executing.</para>
</parameter>
<parameter name="DestinationContext">
<para>The dialplan context the Party A was executing.</para>
</parameter>
<parameter name="CallerID">
<para>The Caller ID name associated with the Party A in the CDR.</para>
</parameter>
<parameter name="Channel">
<para>The channel name of the Party A.</para>
</parameter>
<parameter name="DestinationChannel">
<para>The channel name of the Party B.</para>
</parameter>
<parameter name="LastApplication">
<para>The last dialplan application the Party A executed.</para>
</parameter>
<parameter name="LastData">
<para>
The parameters passed to the last dialplan application the
Party A executed.
</para>
</parameter>
<parameter name="StartTime">
<para>The time the CDR was created.</para>
</parameter>
<parameter name="AnswerTime">
<para>
The earliest of either the time when Party A answered, or
the start time of this CDR.
</para>
</parameter>
<parameter name="EndTime">
<para>
The time when the CDR was finished. This occurs when the
Party A hangs up or when the bridge between Party A and
Party B is broken.
</para>
</parameter>
<parameter name="Duration">
<para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
</parameter>
<parameter name="BillableSeconds">
<para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
</parameter>
<parameter name="Disposition">
<para>The final known disposition of the CDR.</para>
<enumlist>
<enum name="NO ANSWER">
<para>The channel was not answered. This is the default disposition.</para>
</enum>
<enum name="FAILED">
<para>The channel attempted to dial but the call failed.</para>
<note>
<para>The congestion setting in <filename>cdr.conf</filename> can result
in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
<literal>CONGESTION</literal> dial status to map to this disposition.
</para>
</note>
</enum>
<enum name="BUSY">
<para>The channel attempted to dial but the remote party was busy.</para>
</enum>
<enum name="ANSWERED">
<para>The channel was answered. The hang up cause will no longer
impact the disposition of the CDR.</para>
</enum>
<enum name="CONGESTION">
<para>The channel attempted to dial but the remote party was congested.</para>
</enum>
</enumlist>
</parameter>
<parameter name="AMAFlags">
<para>A flag that informs a billing system how to treat the CDR.</para>
<enumlist>
<enum name="OMIT">
<para>This CDR should be ignored.</para>
</enum>
<enum name="BILLING">
<para>This CDR contains valid billing data.</para>
</enum>
<enum name="DOCUMENTATION">
<para>This CDR is for documentation purposes.</para>
</enum>
</enumlist>
</parameter>
<parameter name="UniqueID">
<para>A unique identifier for the Party A channel.</para>
</parameter>
<parameter name="UserField">
<para>
A user defined field set on the channels. If set on both the Party A
and Party B channel, the userfields of both are concatenated and
separated by a <literal>;</literal>.
</para>
</parameter>
</syntax>
<description>
<para>
The <replaceable>Cdr</replaceable> event is only raised when the
<filename>cdr_manager</filename> backend is loaded and registered with
the CDR engine.
</para>
<note>
<para>
This event can contain additional fields depending on the configuration
provided by <filename>cdr_manager.conf</filename>.
</para>
</note>
</description>
</managerEventInstance>
</managerEvent>
***/
Kevin P. Fleming
committed
#include "asterisk.h"
Kevin P. Fleming
committed
#include <time.h>
Kevin P. Fleming
committed
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
Steve Murphy
committed
#include "asterisk/pbx.h"
#define DATE_FORMAT "%Y-%m-%d %T"
#define CONF_FILE "cdr_manager.conf"
Steve Murphy
committed
#define CUSTOM_FIELDS_BUF_SIZE 1024
static const char name[] = "cdr_manager";
static int enablecdr = 0;
static struct ast_str *customfields;
AST_RWLOCK_DEFINE_STATIC(customfields_lock);
static int manager_log(struct ast_cdr *cdr);
static int load_config(int reload)
Steve Murphy
committed
char *cat = NULL;
struct ast_config *cfg;
struct ast_variable *v;
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
int newenablecdr = 0;
cfg = ast_config_load(CONF_FILE, config_flags);
Russell Bryant
committed
if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
Russell Bryant
committed
}
Terry Wilson
committed
if (cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
Russell Bryant
committed
return -1;
Terry Wilson
committed
}
if (!cfg) {
/* Standard configuration */
Steve Murphy
committed
ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
if (enablecdr) {
ast_cdr_backend_suspend(name);
}
enablecdr = 0;
Russell Bryant
committed
return -1;
if (reload) {
ast_rwlock_wrlock(&customfields_lock);
}
if (reload && customfields) {
ast_free(customfields);
customfields = NULL;
}
Steve Murphy
committed
while ( (cat = ast_category_browse(cfg, cat)) ) {
if (!strcasecmp(cat, "general")) {
v = ast_variable_browse(cfg, cat);
while (v) {
if (!strcasecmp(v->name, "enabled"))
newenablecdr = ast_true(v->value);
Steve Murphy
committed
v = v->next;
}
} else if (!strcasecmp(cat, "mappings")) {
customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
v = ast_variable_browse(cfg, cat);
while (v) {
if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
Steve Murphy
committed
ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
} else {
ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
break;
}
Steve Murphy
committed
}
v = v->next;
}
}
}
if (reload) {
ast_rwlock_unlock(&customfields_lock);
}
if (!newenablecdr) {
ast_cdr_backend_suspend(name);
} else if (newenablecdr) {
ast_cdr_backend_unsuspend(name);
}
Tilghman Lesher
committed
enablecdr = newenablecdr;
Russell Bryant
committed
return 0;
}
static int manager_log(struct ast_cdr *cdr)
{
Tilghman Lesher
committed
struct ast_tm timeresult;
char strStartTime[80] = "";
char strAnswerTime[80] = "";
char strEndTime[80] = "";
Steve Murphy
committed
char buf[CUSTOM_FIELDS_BUF_SIZE];
if (!enablecdr)
return 0;
Tilghman Lesher
committed
ast_localtime(&cdr->start, &timeresult, NULL);
ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
if (cdr->answer.tv_sec) {
Tilghman Lesher
committed
ast_localtime(&cdr->answer, &timeresult, NULL);
ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
Tilghman Lesher
committed
ast_localtime(&cdr->end, &timeresult, NULL);
ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
buf[0] = '\0';
ast_rwlock_rdlock(&customfields_lock);
if (customfields && ast_str_strlen(customfields)) {
struct ast_channel *dummy = ast_dummy_channel_alloc();
if (!dummy) {
ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
return 0;
}
ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
Steve Murphy
committed
}
ast_rwlock_unlock(&customfields_lock);
Steve Murphy
committed
Tilghman Lesher
committed
manager_event(EVENT_FLAG_CDR, "Cdr",
"AccountCode: %s\r\n"
"Source: %s\r\n"
"Destination: %s\r\n"
"DestinationContext: %s\r\n"
"CallerID: %s\r\n"
"Channel: %s\r\n"
"DestinationChannel: %s\r\n"
"LastApplication: %s\r\n"
"LastData: %s\r\n"
"StartTime: %s\r\n"
"AnswerTime: %s\r\n"
"EndTime: %s\r\n"
"Duration: %ld\r\n"
"BillableSeconds: %ld\r\n"
"Disposition: %s\r\n"
"AMAFlags: %s\r\n"
"UniqueID: %s\r\n"
Steve Murphy
committed
"UserField: %s\r\n"
"%s",
cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
Steve Murphy
committed
cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
Steve Murphy
committed
return 0;
}
static int unload_module(void)
if (ast_cdr_unregister(name)) {
return -1;
}
Steve Murphy
committed
if (customfields)
ast_free(customfields);
return 0;
}
static int load_module(void)
if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
return AST_MODULE_LOAD_DECLINE;
}
Russell Bryant
committed
if (load_config(0)) {
ast_cdr_unregister(name);
return AST_MODULE_LOAD_DECLINE;
Russell Bryant
committed
}
return AST_MODULE_LOAD_SUCCESS;
static int reload(void)
return load_config(1);
Tilghman Lesher
committed
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
.reload = reload,
.load_pri = AST_MODPRI_CDR_DRIVER,