Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
cdr_manager.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2004 - 2005
5 *
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.
11 *
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.
15 */
16
17/*!
18 * \file
19 * \brief Asterisk Call Manager CDR records.
20 *
21 * See also
22 * \arg \ref AstCDR
23 * \arg \ref AstAMI
24 * \arg \ref Config_ami
25 * \ingroup cdr_drivers
26 */
27
28/*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
29 * \addtogroup configuration_file Configuration Files
30 */
31
32/*!
33 * \page cdr_manager.conf cdr_manager.conf
34 * \verbinclude cdr_manager.conf.sample
35 */
36
37/*** MODULEINFO
38 <support_level>core</support_level>
39 ***/
40
41/*** DOCUMENTATION
42 <managerEvent language="en_US" name="Cdr">
43 <managerEventInstance class="EVENT_FLAG_CDR">
44 <since>
45 <version>13.2.0</version>
46 </since>
47 <synopsis>Raised when a CDR is generated.</synopsis>
48 <syntax>
49 <parameter name="AccountCode">
50 <para>The account code of the Party A channel.</para>
51 </parameter>
52 <parameter name="Source">
53 <para>The Caller ID number associated with the Party A in the CDR.</para>
54 </parameter>
55 <parameter name="Destination">
56 <para>The dialplan extension the Party A was executing.</para>
57 </parameter>
58 <parameter name="DestinationContext">
59 <para>The dialplan context the Party A was executing.</para>
60 </parameter>
61 <parameter name="CallerID">
62 <para>The Caller ID name associated with the Party A in the CDR.</para>
63 </parameter>
64 <parameter name="Channel">
65 <para>The channel name of the Party A.</para>
66 </parameter>
67 <parameter name="DestinationChannel">
68 <para>The channel name of the Party B.</para>
69 </parameter>
70 <parameter name="LastApplication">
71 <para>The last dialplan application the Party A executed.</para>
72 </parameter>
73 <parameter name="LastData">
74 <para>
75 The parameters passed to the last dialplan application the
76 Party A executed.
77 </para>
78 </parameter>
79 <parameter name="StartTime">
80 <para>The time the CDR was created.</para>
81 </parameter>
82 <parameter name="AnswerTime">
83 <para>
84 The earliest of either the time when Party A answered, or
85 the start time of this CDR.
86 </para>
87 </parameter>
88 <parameter name="EndTime">
89 <para>
90 The time when the CDR was finished. This occurs when the
91 Party A hangs up or when the bridge between Party A and
92 Party B is broken.
93 </para>
94 </parameter>
95 <parameter name="Duration">
96 <para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
97 </parameter>
98 <parameter name="BillableSeconds">
99 <para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
100 </parameter>
101 <parameter name="Disposition">
102 <para>The final known disposition of the CDR.</para>
103 <enumlist>
104 <enum name="NO ANSWER">
105 <para>The channel was not answered. This is the default disposition.</para>
106 </enum>
107 <enum name="FAILED">
108 <para>The channel attempted to dial but the call failed.</para>
109 <note>
110 <para>The congestion setting in <filename>cdr.conf</filename> can result
111 in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
112 <literal>CONGESTION</literal> dial status to map to this disposition.
113 </para>
114 </note>
115 </enum>
116 <enum name="BUSY">
117 <para>The channel attempted to dial but the remote party was busy.</para>
118 </enum>
119 <enum name="ANSWERED">
120 <para>The channel was answered. The hang up cause will no longer
121 impact the disposition of the CDR.</para>
122 </enum>
123 <enum name="CONGESTION">
124 <para>The channel attempted to dial but the remote party was congested.</para>
125 </enum>
126 </enumlist>
127 </parameter>
128 <parameter name="AMAFlags">
129 <para>A flag that informs a billing system how to treat the CDR.</para>
130 <enumlist>
131 <enum name="OMIT">
132 <para>This CDR should be ignored.</para>
133 </enum>
134 <enum name="BILLING">
135 <para>This CDR contains valid billing data.</para>
136 </enum>
137 <enum name="DOCUMENTATION">
138 <para>This CDR is for documentation purposes.</para>
139 </enum>
140 </enumlist>
141 </parameter>
142 <parameter name="UniqueID">
143 <para>A unique identifier for the Party A channel.</para>
144 </parameter>
145 <parameter name="UserField">
146 <para>
147 A user defined field set on the channels. If set on both the Party A
148 and Party B channel, the userfields of both are concatenated and
149 separated by a <literal>;</literal>.
150 </para>
151 </parameter>
152 </syntax>
153 <description>
154 <para>
155 The <replaceable>Cdr</replaceable> event is only raised when the
156 <filename>cdr_manager</filename> backend is loaded and registered with
157 the CDR engine.
158 </para>
159 <note>
160 <para>
161 This event can contain additional fields depending on the configuration
162 provided by <filename>cdr_manager.conf</filename>.
163 </para>
164 </note>
165 </description>
166 </managerEventInstance>
167 </managerEvent>
168 ***/
169
170#include "asterisk.h"
171
172#include <time.h>
173
174#include "asterisk/channel.h"
175#include "asterisk/cdr.h"
176#include "asterisk/module.h"
177#include "asterisk/utils.h"
178#include "asterisk/manager.h"
179#include "asterisk/config.h"
180#include "asterisk/pbx.h"
181
182#define DATE_FORMAT "%Y-%m-%d %T"
183#define CONF_FILE "cdr_manager.conf"
184#define CUSTOM_FIELDS_BUF_SIZE 1024
185
186static const char name[] = "cdr_manager";
187
188static int enablecdr = 0;
189
190static struct ast_str *customfields;
192
193static int manager_log(struct ast_cdr *cdr);
194
195static int load_config(int reload)
196{
197 char *cat = NULL;
198 struct ast_config *cfg;
199 struct ast_variable *v;
200 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
201 int newenablecdr = 0;
202
203 cfg = ast_config_load(CONF_FILE, config_flags);
204 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
205 return 0;
206 }
207
208 if (cfg == CONFIG_STATUS_FILEINVALID) {
209 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
210 return -1;
211 }
212
213 if (!cfg) {
214 /* Standard configuration */
215 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
216 if (enablecdr) {
218 }
219 enablecdr = 0;
220 return -1;
221 }
222
223 if (reload) {
225 }
226
227 if (reload && customfields) {
230 }
231
232 while ( (cat = ast_category_browse(cfg, cat)) ) {
233 if (!strcasecmp(cat, "general")) {
234 v = ast_variable_browse(cfg, cat);
235 while (v) {
236 if (!strcasecmp(v->name, "enabled"))
237 newenablecdr = ast_true(v->value);
238
239 v = v->next;
240 }
241 } else if (!strcasecmp(cat, "mappings")) {
243 v = ast_variable_browse(cfg, cat);
244 while (v) {
246 if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
247 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
248 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
249 } else {
250 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
251 break;
252 }
253
254 }
255 v = v->next;
256 }
257 }
258 }
259
260 if (reload) {
262 }
263
265
266 if (!newenablecdr) {
268 } else if (newenablecdr) {
270 }
271 enablecdr = newenablecdr;
272
273 return 0;
274}
275
276static int manager_log(struct ast_cdr *cdr)
277{
278 struct ast_tm timeresult;
279 char strStartTime[80] = "";
280 char strAnswerTime[80] = "";
281 char strEndTime[80] = "";
283
284 if (!enablecdr)
285 return 0;
286
287 ast_localtime(&cdr->start, &timeresult, NULL);
288 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
289
290 if (cdr->answer.tv_sec) {
291 ast_localtime(&cdr->answer, &timeresult, NULL);
292 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
293 }
294
295 ast_localtime(&cdr->end, &timeresult, NULL);
296 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
297
298 buf[0] = '\0';
302 if (!dummy) {
303 ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
304 return 0;
305 }
309 }
311
313 "AccountCode: %s\r\n"
314 "Source: %s\r\n"
315 "Destination: %s\r\n"
316 "DestinationContext: %s\r\n"
317 "CallerID: %s\r\n"
318 "Channel: %s\r\n"
319 "DestinationChannel: %s\r\n"
320 "LastApplication: %s\r\n"
321 "LastData: %s\r\n"
322 "StartTime: %s\r\n"
323 "AnswerTime: %s\r\n"
324 "EndTime: %s\r\n"
325 "Duration: %ld\r\n"
326 "BillableSeconds: %ld\r\n"
327 "Disposition: %s\r\n"
328 "AMAFlags: %s\r\n"
329 "UniqueID: %s\r\n"
330 "UserField: %s\r\n"
331 "%s",
333 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
336
337 return 0;
338}
339
340static int unload_module(void)
341{
343 return -1;
344 }
345
346 if (customfields)
348
349 return 0;
350}
351
352static int load_module(void)
353{
354 if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
356 }
357
358 if (load_config(0)) {
361 }
362
364}
365
366static int reload(void)
367{
368 return load_config(1);
369}
370
371AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
372 .support_level = AST_MODULE_SUPPORT_CORE,
373 .load = load_module,
374 .unload = unload_module,
375 .reload = reload,
376 .load_pri = AST_MODPRI_CDR_DRIVER,
377 .requires = "cdr",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_log
Definition: astobj2.c:42
Call Detail Record API.
int ast_cdr_backend_unsuspend(const char *name)
Unsuspend a CDR backend.
Definition: cdr.c:3004
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:3108
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3563
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:3063
int ast_cdr_backend_suspend(const char *name)
Suspend a CDR backend temporarily.
Definition: cdr.c:2986
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a public CDR.
Definition: cdr.c:3118
static const char name[]
Definition: cdr_manager.c:186
static int manager_log(struct ast_cdr *cdr)
Definition: cdr_manager.c:276
#define CONF_FILE
Definition: cdr_manager.c:183
static int enablecdr
Definition: cdr_manager.c:188
static struct ast_str * customfields
Definition: cdr_manager.c:190
#define CUSTOM_FIELDS_BUF_SIZE
Definition: cdr_manager.c:184
static int load_module(void)
Definition: cdr_manager.c:352
static int unload_module(void)
Definition: cdr_manager.c:340
static int load_config(int reload)
Definition: cdr_manager.c:195
static int reload(void)
Definition: cdr_manager.c:366
static ast_rwlock_t customfields_lock
Definition: cdr_manager.c:191
#define DATE_FORMAT
Definition: cdr_manager.c:182
static void dummy(char *unused,...)
Definition: chan_unistim.c:220
General Asterisk PBX channel definitions.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4390
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
#define ast_dummy_channel_alloc()
Create a fake channel structure.
Definition: channel.h:1328
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
@ CONFIG_FLAG_FILEUNCHANGED
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
#define ast_rwlock_wrlock(a)
Definition: lock.h:240
#define ast_rwlock_rdlock(a)
Definition: lock.h:239
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
Definition: lock.h:547
#define ast_rwlock_unlock(a)
Definition: lock.h:238
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:254
#define EVENT_FLAG_CDR
Definition: manager.h:85
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CDR_DRIVER
Definition: module.h:345
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Core PBX routines and definitions.
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
Responsible for call detail data.
Definition: cdr.h:279
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:291
long int disposition
Definition: cdr.h:307
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:295
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:325
long int billsec
Definition: cdr.h:305
struct timeval answer
Definition: cdr.h:299
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:289
long int duration
Definition: cdr.h:303
long int amaflags
Definition: cdr.h:309
char src[AST_MAX_EXTENSION]
Definition: cdr.h:283
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:285
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:281
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:317
struct timeval start
Definition: cdr.h:297
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:311
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:293
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:287
struct timeval end
Definition: cdr.h:301
Main Channel structure associated with a channel.
struct ast_cdr * cdr
Structure used to handle boolean flags.
Definition: utils.h:199
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Time-related functions and macros.
Utility functions.