2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Call Detail Record API
23 * \author Mark Spencer <markster@digium.com>
25 * \note Includes code and algorithms from the Zapata library.
27 * \note We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
28 * through our fingers somehow. If someone allocates a CDR, it must be completely handled normally
29 * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
30 * isn't properly generated and posted.
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/cdr.h"
43 #include "asterisk/callerid.h"
44 #include "asterisk/manager.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/linkedlists.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/sched.h"
49 #include "asterisk/config.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/stringfields.h"
52 #include "asterisk/data.h"
54 /*! Default AMA flag for billing records (CDR's) */
55 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
56 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
58 struct ast_cdr_beitem {
62 AST_RWLIST_ENTRY(ast_cdr_beitem) list;
65 static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
67 struct ast_cdr_batch_item {
69 struct ast_cdr_batch_item *next;
72 static struct ast_cdr_batch {
74 struct ast_cdr_batch_item *head;
75 struct ast_cdr_batch_item *tail;
79 static int cdr_sequence = 0;
81 static int cdr_seq_inc(struct ast_cdr *cdr);
83 static struct ast_sched_context *sched;
84 static int cdr_sched = -1;
85 static pthread_t cdr_thread = AST_PTHREADT_NULL;
88 static const int ENABLED_DEFAULT = 1;
91 static const int BATCHMODE_DEFAULT = 0;
93 static int unanswered;
94 static const int UNANSWERED_DEFAULT = 0;
96 static int congestion;
97 static const int CONGESTION_DEFAULT = 0;
100 static const int BATCH_SIZE_DEFAULT = 100;
102 static int batchtime;
103 static const int BATCH_TIME_DEFAULT = 300;
105 static int batchscheduleronly;
106 static const int BATCH_SCHEDULER_ONLY_DEFAULT = 0;
108 static int batchsafeshutdown;
109 static const int BATCH_SAFE_SHUTDOWN_DEFAULT = 1;
111 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
113 /* these are used to wake up the CDR thread when there's work to do */
114 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
115 static ast_cond_t cdr_pending_cond;
117 int check_cdr_enabled(void)
123 * \brief Register a CDR driver. Each registered CDR driver generates a CDR
124 * \retval 0 on success.
125 * \retval -1 on error
127 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
129 struct ast_cdr_beitem *i = NULL;
135 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
139 AST_RWLIST_WRLOCK(&be_list);
140 AST_RWLIST_TRAVERSE(&be_list, i, list) {
141 if (!strcasecmp(name, i->name)) {
142 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
143 AST_RWLIST_UNLOCK(&be_list);
148 if (!(i = ast_calloc(1, sizeof(*i))))
152 ast_copy_string(i->name, name, sizeof(i->name));
153 ast_copy_string(i->desc, desc, sizeof(i->desc));
155 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
156 AST_RWLIST_UNLOCK(&be_list);
161 /*! unregister a CDR driver */
162 void ast_cdr_unregister(const char *name)
164 struct ast_cdr_beitem *i = NULL;
166 AST_RWLIST_WRLOCK(&be_list);
167 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
168 if (!strcasecmp(name, i->name)) {
169 AST_RWLIST_REMOVE_CURRENT(list);
173 AST_RWLIST_TRAVERSE_SAFE_END;
174 AST_RWLIST_UNLOCK(&be_list);
177 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
182 int ast_cdr_isset_unanswered(void)
187 int ast_cdr_isset_congestion(void)
192 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
194 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
202 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
204 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
212 /*! Duplicate a CDR record
213 \returns Pointer to new CDR record
215 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
217 struct ast_cdr *newcdr;
219 if (!cdr) /* don't die if we get a null cdr pointer */
221 newcdr = ast_cdr_alloc();
225 memcpy(newcdr, cdr, sizeof(*newcdr));
226 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
227 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
228 ast_cdr_copy_vars(newcdr, cdr);
234 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
236 if (ast_strlen_zero(name))
239 for (; cdr; cdr = recur ? cdr->next : NULL) {
240 struct ast_var_t *variables;
241 struct varshead *headp = &cdr->varshead;
242 AST_LIST_TRAVERSE(headp, variables, entries) {
243 if (!strcasecmp(name, ast_var_name(variables)))
244 return ast_var_value(variables);
251 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
253 if (fmt == NULL) { /* raw mode */
254 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
259 ast_localtime(&when, &tm, NULL);
260 ast_strftime(buf, bufsize, fmt, &tm);
265 /*! CDR channel variable retrieval */
266 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
268 const char *fmt = "%Y-%m-%d %T";
271 if (!cdr) /* don't die if the cdr is null */
275 /* special vars (the ones from the struct ast_cdr when requested by name)
276 I'd almost say we should convert all the stringed vals to vars */
278 if (!strcasecmp(name, "clid"))
279 ast_copy_string(workspace, cdr->clid, workspacelen);
280 else if (!strcasecmp(name, "src"))
281 ast_copy_string(workspace, cdr->src, workspacelen);
282 else if (!strcasecmp(name, "dst"))
283 ast_copy_string(workspace, cdr->dst, workspacelen);
284 else if (!strcasecmp(name, "dcontext"))
285 ast_copy_string(workspace, cdr->dcontext, workspacelen);
286 else if (!strcasecmp(name, "channel"))
287 ast_copy_string(workspace, cdr->channel, workspacelen);
288 else if (!strcasecmp(name, "dstchannel"))
289 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
290 else if (!strcasecmp(name, "lastapp"))
291 ast_copy_string(workspace, cdr->lastapp, workspacelen);
292 else if (!strcasecmp(name, "lastdata"))
293 ast_copy_string(workspace, cdr->lastdata, workspacelen);
294 else if (!strcasecmp(name, "start"))
295 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
296 else if (!strcasecmp(name, "answer"))
297 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
298 else if (!strcasecmp(name, "end"))
299 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
300 else if (!strcasecmp(name, "duration"))
301 snprintf(workspace, workspacelen, "%ld", cdr->duration ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
302 else if (!strcasecmp(name, "billsec"))
303 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
304 else if (!strcasecmp(name, "disposition")) {
306 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
308 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
310 } else if (!strcasecmp(name, "amaflags")) {
312 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
314 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
316 } else if (!strcasecmp(name, "accountcode"))
317 ast_copy_string(workspace, cdr->accountcode, workspacelen);
318 else if (!strcasecmp(name, "peeraccount"))
319 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
320 else if (!strcasecmp(name, "uniqueid"))
321 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
322 else if (!strcasecmp(name, "linkedid"))
323 ast_copy_string(workspace, cdr->linkedid, workspacelen);
324 else if (!strcasecmp(name, "userfield"))
325 ast_copy_string(workspace, cdr->userfield, workspacelen);
326 else if (!strcasecmp(name, "sequence"))
327 snprintf(workspace, workspacelen, "%d", cdr->sequence);
328 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
329 ast_copy_string(workspace, varbuf, workspacelen);
333 if (!ast_strlen_zero(workspace))
337 /* readonly cdr variables */
338 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
339 "lastapp", "lastdata", "start", "answer", "end", "duration",
340 "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
341 "userfield", "sequence", NULL };
342 /*! Set a CDR channel variable
343 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
345 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
347 struct ast_var_t *newvariable;
348 struct varshead *headp;
351 for (x = 0; cdr_readonly_vars[x]; x++) {
352 if (!strcasecmp(name, cdr_readonly_vars[x])) {
353 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
359 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
363 for (; cdr; cdr = recur ? cdr->next : NULL) {
364 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
366 headp = &cdr->varshead;
367 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
368 if (!strcasecmp(ast_var_name(newvariable), name)) {
369 /* there is already such a variable, delete it */
370 AST_LIST_REMOVE_CURRENT(entries);
371 ast_var_delete(newvariable);
375 AST_LIST_TRAVERSE_SAFE_END;
378 newvariable = ast_var_assign(name, value);
379 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
386 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
388 struct ast_var_t *variables, *newvariable = NULL;
389 struct varshead *headpa, *headpb;
390 const char *var, *val;
393 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
396 headpa = &from_cdr->varshead;
397 headpb = &to_cdr->varshead;
399 AST_LIST_TRAVERSE(headpa,variables,entries) {
401 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
402 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
403 newvariable = ast_var_assign(var, val);
404 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
412 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
414 struct ast_var_t *variables;
418 int total = 0, x = 0, i;
422 for (; cdr; cdr = recur ? cdr->next : NULL) {
424 ast_str_append(buf, 0, "\n");
426 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
427 if (!(var = ast_var_name(variables))) {
431 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
432 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
439 for (i = 0; cdr_readonly_vars[i]; i++) {
440 workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
441 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
445 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
446 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
457 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
460 /* clear variables */
461 for (; cdr; cdr = recur ? cdr->next : NULL) {
462 struct ast_var_t *vardata;
463 struct varshead *headp = &cdr->varshead;
464 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
465 ast_var_delete(vardata);
469 /*! \brief print a warning if cdr already posted */
470 static void check_post(struct ast_cdr *cdr)
474 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
475 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
478 void ast_cdr_free(struct ast_cdr *cdr)
482 struct ast_cdr *next = cdr->next;
484 ast_cdr_free_vars(cdr, 0);
490 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
491 void ast_cdr_discard(struct ast_cdr *cdr)
494 struct ast_cdr *next = cdr->next;
496 ast_cdr_free_vars(cdr, 0);
502 struct ast_cdr *ast_cdr_alloc(void)
505 x = ast_calloc(1, sizeof(*x));
507 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
511 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
513 struct ast_var_t *variablesfrom,*variablesto;
514 struct varshead *headpfrom = &to->varshead;
515 struct varshead *headpto = &from->varshead;
516 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
517 /* for every var in from, stick it in to */
518 const char *fromvarname, *fromvarval;
519 const char *tovarname = NULL, *tovarval = NULL;
520 fromvarname = ast_var_name(variablesfrom);
521 fromvarval = ast_var_value(variablesfrom);
524 /* now, quick see if that var is in the 'to' cdr already */
525 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
527 /* now, quick see if that var is in the 'to' cdr already */
528 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
529 tovarname = ast_var_name(variablesto);
530 tovarval = ast_var_value(variablesto);
534 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
535 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
537 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
540 /* rip this var out of the from cdr, and stick it in the to cdr */
541 AST_LIST_MOVE_CURRENT(headpto, entries);
543 AST_LIST_TRAVERSE_SAFE_END;
546 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
548 struct ast_cdr *zcdr;
549 struct ast_cdr *lto = NULL;
550 struct ast_cdr *lfrom = NULL;
551 int discard_from = 0;
556 /* don't merge into locked CDR's -- it's bad business */
557 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
558 zcdr = to; /* safety valve? */
564 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
565 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
566 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
571 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
572 struct ast_cdr *llfrom = NULL;
575 /* insert the from stuff after lto */
578 while (lfrom && lfrom->next) {
579 if (!lfrom->next->next)
583 /* rip off the last entry and put a copy of the to at the end */
587 /* save copy of the current *to cdr */
589 memcpy(&tcdr, to, sizeof(tcdr));
590 /* copy in the locked from cdr */
591 memcpy(to, from, sizeof(*to));
593 while (lfrom && lfrom->next) {
594 if (!lfrom->next->next)
599 /* rip off the last entry and put a copy of the to at the end */
601 to = to->next = ast_cdr_dup(&tcdr);
603 to = llfrom->next = ast_cdr_dup(&tcdr);
608 if (!ast_tvzero(from->start)) {
609 if (!ast_tvzero(to->start)) {
610 if (ast_tvcmp(to->start, from->start) > 0 ) {
611 to->start = from->start; /* use the earliest time */
612 from->start = ast_tv(0,0); /* we actively "steal" these values */
614 /* else nothing to do */
616 to->start = from->start;
617 from->start = ast_tv(0,0); /* we actively "steal" these values */
620 if (!ast_tvzero(from->answer)) {
621 if (!ast_tvzero(to->answer)) {
622 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
623 to->answer = from->answer; /* use the earliest time */
624 from->answer = ast_tv(0,0); /* we actively "steal" these values */
626 /* we got the earliest answer time, so we'll settle for that? */
628 to->answer = from->answer;
629 from->answer = ast_tv(0,0); /* we actively "steal" these values */
632 if (!ast_tvzero(from->end)) {
633 if (!ast_tvzero(to->end)) {
634 if (ast_tvcmp(to->end, from->end) < 0 ) {
635 to->end = from->end; /* use the latest time */
636 from->end = ast_tv(0,0); /* we actively "steal" these values */
637 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
638 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
640 /* else, nothing to do */
643 from->end = ast_tv(0,0); /* we actively "steal" these values */
644 to->duration = to->end.tv_sec - to->start.tv_sec;
645 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
648 if (to->disposition < from->disposition) {
649 to->disposition = from->disposition;
650 from->disposition = AST_CDR_NOANSWER;
652 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
653 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
654 from->lastapp[0] = 0; /* theft */
656 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
657 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
658 from->lastdata[0] = 0; /* theft */
660 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
661 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
662 from->dcontext[0] = 0; /* theft */
664 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
665 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
666 from->dstchannel[0] = 0; /* theft */
668 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
669 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
670 from->channel[0] = 0; /* theft */
672 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
673 ast_copy_string(to->src, from->src, sizeof(to->src));
674 from->src[0] = 0; /* theft */
676 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
677 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
678 from->clid[0] = 0; /* theft */
680 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
681 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
682 from->dst[0] = 0; /* theft */
685 to->amaflags = AST_CDR_DOCUMENTATION;
687 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
688 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
689 to->amaflags = from->amaflags;
691 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
692 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
694 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
695 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
697 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
698 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
700 /* flags, varsead, ? */
701 cdr_merge_vars(from, to);
703 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
704 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
705 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
706 ast_set_flag(to, AST_CDR_FLAG_POSTED);
707 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
708 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
709 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
710 ast_set_flag(to, AST_CDR_FLAG_CHILD);
711 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
712 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
714 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
716 /* just rip 'em off the 'from' and insert them on the 'to' */
718 from->next = zcdr->next;
720 /* zcdr is now ripped from the current list; */
721 ast_cdr_append(to, zcdr);
724 ast_cdr_discard(from);
727 void ast_cdr_start(struct ast_cdr *cdr)
729 for (; cdr; cdr = cdr->next) {
730 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
732 cdr->start = ast_tvnow();
737 void ast_cdr_answer(struct ast_cdr *cdr)
740 for (; cdr; cdr = cdr->next) {
741 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
743 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
746 if (cdr->disposition < AST_CDR_ANSWERED)
747 cdr->disposition = AST_CDR_ANSWERED;
748 if (ast_tvzero(cdr->answer))
749 cdr->answer = ast_tvnow();
753 void ast_cdr_busy(struct ast_cdr *cdr)
756 for (; cdr; cdr = cdr->next) {
757 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
759 cdr->disposition = AST_CDR_BUSY;
764 void ast_cdr_failed(struct ast_cdr *cdr)
766 for (; cdr; cdr = cdr->next) {
768 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
770 if (cdr->disposition < AST_CDR_FAILED)
771 cdr->disposition = AST_CDR_FAILED;
776 void ast_cdr_noanswer(struct ast_cdr *cdr)
779 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
781 cdr->disposition = AST_CDR_NOANSWER;
787 void ast_cdr_congestion(struct ast_cdr *cdr)
791 /* if congestion log is disabled, pass the buck to ast_cdr_failed */
796 while (cdr && congestion) {
797 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
798 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
800 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
801 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
804 if (cdr->disposition < AST_CDR_CONGESTION) {
805 cdr->disposition = AST_CDR_CONGESTION;
812 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
813 if ast_cdr_disposition returns a non-zero value */
815 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
819 for (; cdr; cdr = cdr->next) {
820 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
821 return -1 to set disposition to FAILED */
825 case AST_CAUSE_NO_ANSWER:
826 ast_cdr_noanswer(cdr);
828 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
829 ast_cdr_congestion(cdr);
831 case AST_CAUSE_NORMAL:
840 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
842 for (; cdr; cdr = cdr->next) {
843 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
845 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
850 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
853 for (; cdr; cdr = cdr->next) {
854 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
856 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
857 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
862 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
865 for (; cdr; cdr = cdr->next) {
866 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
868 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
875 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
878 for (; cdr; cdr = cdr->next) {
879 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
882 cdr->disposition = disposition;
886 /* set cid info for one record */
887 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
895 /* Grab source from ANI or normal Caller*ID */
896 num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
897 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
898 ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
899 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
900 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
901 ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
903 if (c->caller.id.subaddress.valid) {
904 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
906 if (c->dialed.subaddress.valid) {
907 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
911 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
913 for (; cdr; cdr = cdr->next) {
914 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
920 static int cdr_seq_inc(struct ast_cdr *cdr)
922 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
925 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
927 for ( ; cdr ; cdr = cdr->next) {
928 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
929 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
933 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
934 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
935 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
936 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
937 /* Destination information */
938 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
939 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
940 /* Unique call identifier */
941 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
942 /* Linked call identifier */
943 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
949 /* Three routines were "fixed" via 10668, and later shown that
950 users were depending on this behavior. ast_cdr_end,
951 ast_cdr_setvar and ast_cdr_answer are the three routines.
952 While most of the other routines would not touch
953 LOCKED cdr's, these three routines were designed to
954 operate on locked CDR's as a matter of course.
955 I now appreciate how this plays with the ForkCDR app,
956 which forms these cdr chains in the first place.
957 cdr_end is pretty key: all cdrs created are closed
958 together. They only vary by start time. Arithmetically,
959 users can calculate the subintervals they wish to track. */
961 void ast_cdr_end(struct ast_cdr *cdr)
963 for ( ; cdr ; cdr = cdr->next) {
964 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
967 if (ast_tvzero(cdr->end))
968 cdr->end = ast_tvnow();
969 if (ast_tvzero(cdr->start)) {
970 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
971 cdr->disposition = AST_CDR_FAILED;
973 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
974 if (ast_tvzero(cdr->answer)) {
975 if (cdr->disposition == AST_CDR_ANSWERED) {
976 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
977 cdr->disposition = AST_CDR_FAILED;
980 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
981 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
982 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
987 char *ast_cdr_disp2str(int disposition)
989 switch (disposition) {
991 return "NO ANSWER"; /* by default, for backward compatibility */
992 case AST_CDR_NOANSWER:
998 case AST_CDR_ANSWERED:
1000 case AST_CDR_CONGESTION:
1001 return "CONGESTION";
1006 /*! Converts AMA flag to printable string */
1007 char *ast_cdr_flags2str(int flag)
1012 case AST_CDR_BILLING:
1014 case AST_CDR_DOCUMENTATION:
1015 return "DOCUMENTATION";
1020 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1022 struct ast_cdr *cdr = chan->cdr;
1023 const char *old_acct = "";
1025 if (!ast_strlen_zero(chan->accountcode)) {
1026 old_acct = ast_strdupa(chan->accountcode);
1029 ast_string_field_set(chan, accountcode, account);
1030 for ( ; cdr ; cdr = cdr->next) {
1031 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1032 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1036 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1039 "AccountCode: %s\r\n"
1040 "OldAccountCode: %s\r\n",
1041 chan->name, chan->uniqueid, chan->accountcode, old_acct);
1046 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1048 struct ast_cdr *cdr = chan->cdr;
1049 const char *old_acct = "";
1051 if (!ast_strlen_zero(chan->peeraccount)) {
1052 old_acct = ast_strdupa(chan->peeraccount);
1055 ast_string_field_set(chan, peeraccount, account);
1056 for ( ; cdr ; cdr = cdr->next) {
1057 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1058 ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1062 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1065 "PeerAccount: %s\r\n"
1066 "OldPeerAccount: %s\r\n",
1067 chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1072 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1074 struct ast_cdr *cdr;
1075 int newflag = ast_cdr_amaflags2int(flag);
1077 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1078 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1079 cdr->amaflags = newflag;
1087 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1089 struct ast_cdr *cdr = chan->cdr;
1091 for ( ; cdr ; cdr = cdr->next) {
1092 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1093 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1099 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1101 struct ast_cdr *cdr = chan->cdr;
1103 for ( ; cdr ; cdr = cdr->next) {
1104 int len = strlen(cdr->userfield);
1106 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1107 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1113 int ast_cdr_update(struct ast_channel *c)
1115 struct ast_cdr *cdr = c->cdr;
1117 for ( ; cdr ; cdr = cdr->next) {
1118 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1119 set_one_cid(cdr, c);
1121 /* Copy account code et-al */
1122 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1123 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1124 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1126 /* Destination information */ /* XXX privilege macro* ? */
1127 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1128 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1135 int ast_cdr_amaflags2int(const char *flag)
1137 if (!strcasecmp(flag, "default"))
1139 if (!strcasecmp(flag, "omit"))
1140 return AST_CDR_OMIT;
1141 if (!strcasecmp(flag, "billing"))
1142 return AST_CDR_BILLING;
1143 if (!strcasecmp(flag, "documentation"))
1144 return AST_CDR_DOCUMENTATION;
1148 static void post_cdr(struct ast_cdr *cdr)
1150 struct ast_cdr_beitem *i;
1152 for ( ; cdr ; cdr = cdr->next) {
1153 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1154 /* For people, who don't want to see unanswered single-channel events */
1155 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1159 /* don't post CDRs that are for dialed channels unless those
1160 * channels were originated from asterisk (pbx_spool, manager,
1162 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1163 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1168 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1169 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1171 AST_RWLIST_RDLOCK(&be_list);
1172 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1175 AST_RWLIST_UNLOCK(&be_list);
1179 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1181 struct ast_cdr *duplicate;
1182 struct ast_flags flags = { 0 };
1185 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1187 for ( ; cdr ; cdr = cdr->next) {
1188 /* Detach if post is requested */
1189 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1190 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1192 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1193 ast_cdr_detach(duplicate);
1195 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1198 /* enable CDR only */
1199 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1200 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1204 /* clear variables */
1205 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1206 ast_cdr_free_vars(cdr, 0);
1209 /* Reset to initial state */
1210 ast_clear_flag(cdr, AST_FLAGS_ALL);
1211 memset(&cdr->start, 0, sizeof(cdr->start));
1212 memset(&cdr->end, 0, sizeof(cdr->end));
1213 memset(&cdr->answer, 0, sizeof(cdr->answer));
1217 cdr->disposition = AST_CDR_NOANSWER;
1222 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1224 struct ast_flags flags = { 0 };
1227 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1229 /* Reset to initial state */
1230 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1231 ast_clear_flag(cdr, AST_FLAGS_ALL);
1232 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1234 ast_clear_flag(cdr, AST_FLAGS_ALL);
1237 memset(&cdr->start, 0, sizeof(cdr->start));
1238 memset(&cdr->end, 0, sizeof(cdr->end));
1239 memset(&cdr->answer, 0, sizeof(cdr->answer));
1243 cdr->disposition = AST_CDR_NULL;
1246 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1248 struct ast_cdr *ret;
1263 /*! \note Don't call without cdr_batch_lock */
1264 static void reset_batch(void)
1271 /*! \note Don't call without cdr_batch_lock */
1272 static int init_batch(void)
1274 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1275 if (!(batch = ast_malloc(sizeof(*batch))))
1283 static void *do_batch_backend_process(void *data)
1285 struct ast_cdr_batch_item *processeditem;
1286 struct ast_cdr_batch_item *batchitem = data;
1288 /* Push each CDR into storage mechanism(s) and free all the memory */
1290 post_cdr(batchitem->cdr);
1291 ast_cdr_free(batchitem->cdr);
1292 processeditem = batchitem;
1293 batchitem = batchitem->next;
1294 ast_free(processeditem);
1300 void ast_cdr_submit_batch(int do_shutdown)
1302 struct ast_cdr_batch_item *oldbatchitems = NULL;
1303 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1305 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1306 if (!batch || !batch->head)
1309 /* move the old CDRs aside, and prepare a new CDR batch */
1310 ast_mutex_lock(&cdr_batch_lock);
1311 oldbatchitems = batch->head;
1313 ast_mutex_unlock(&cdr_batch_lock);
1315 /* if configured, spawn a new thread to post these CDRs,
1316 also try to save as much as possible if we are shutting down safely */
1317 if (batchscheduleronly || do_shutdown) {
1318 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1319 do_batch_backend_process(oldbatchitems);
1321 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1322 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1323 do_batch_backend_process(oldbatchitems);
1325 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1330 static int submit_scheduled_batch(const void *data)
1332 ast_cdr_submit_batch(0);
1333 /* manually reschedule from this point in time */
1334 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1335 /* returning zero so the scheduler does not automatically reschedule */
1339 static void submit_unscheduled_batch(void)
1341 /* this is okay since we are not being called from within the scheduler */
1342 AST_SCHED_DEL(sched, cdr_sched);
1343 /* schedule the submission to occur ASAP (1 ms) */
1344 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1345 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1346 ast_mutex_lock(&cdr_pending_lock);
1347 ast_cond_signal(&cdr_pending_cond);
1348 ast_mutex_unlock(&cdr_pending_lock);
1351 void ast_cdr_detach(struct ast_cdr *cdr)
1353 struct ast_cdr_batch_item *newtail;
1359 /* maybe they disabled CDR stuff completely, so just drop it */
1361 ast_debug(1, "Dropping CDR !\n");
1362 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1367 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1374 /* otherwise, each CDR gets put into a batch list (at the end) */
1375 ast_debug(1, "CDR detaching from this thread\n");
1377 /* we'll need a new tail for every CDR */
1378 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1384 /* don't traverse a whole list (just keep track of the tail) */
1385 ast_mutex_lock(&cdr_batch_lock);
1389 /* new batch is empty, so point the head at the new tail */
1390 batch->head = newtail;
1392 /* already got a batch with something in it, so just append a new tail */
1393 batch->tail->next = newtail;
1396 batch->tail = newtail;
1397 curr = batch->size++;
1398 ast_mutex_unlock(&cdr_batch_lock);
1400 /* if we have enough stuff to post, then do it */
1401 if (curr >= (batchsize - 1))
1402 submit_unscheduled_batch();
1405 static void *do_cdr(void *data)
1407 struct timespec timeout;
1413 schedms = ast_sched_wait(sched);
1414 /* this shouldn't happen, but provide a 1 second default just in case */
1417 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1418 timeout.tv_sec = now.tv_sec;
1419 timeout.tv_nsec = now.tv_usec * 1000;
1420 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1421 ast_mutex_lock(&cdr_pending_lock);
1422 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1423 numevents = ast_sched_runq(sched);
1424 ast_mutex_unlock(&cdr_pending_lock);
1425 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1431 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1433 struct ast_cdr_beitem *beitem=NULL;
1435 long nextbatchtime=0;
1439 e->command = "cdr show status";
1441 "Usage: cdr show status\n"
1442 " Displays the Call Detail Record engine system status.\n";
1449 return CLI_SHOWUSAGE;
1451 ast_cli(a->fd, "\n");
1452 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1453 ast_cli(a->fd, "----------------------------------\n");
1454 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1455 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1457 ast_cli(a->fd, " Log unanswered calls: %s\n", unanswered ? "Yes" : "No");
1458 ast_cli(a->fd, " Log congestion: %s\n\n", congestion ? "Yes" : "No");
1460 ast_cli(a->fd, "* Batch Mode Settings\n");
1461 ast_cli(a->fd, " -------------------\n");
1465 nextbatchtime = ast_sched_when(sched, cdr_sched);
1466 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1467 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1468 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1469 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1470 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1471 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1473 ast_cli(a->fd, "* Registered Backends\n");
1474 ast_cli(a->fd, " -------------------\n");
1475 AST_RWLIST_RDLOCK(&be_list);
1476 if (AST_RWLIST_EMPTY(&be_list)) {
1477 ast_cli(a->fd, " (none)\n");
1479 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1480 ast_cli(a->fd, " %s\n", beitem->name);
1483 AST_RWLIST_UNLOCK(&be_list);
1484 ast_cli(a->fd, "\n");
1490 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1494 e->command = "cdr submit";
1496 "Usage: cdr submit\n"
1497 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1503 return CLI_SHOWUSAGE;
1505 submit_unscheduled_batch();
1506 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1511 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1512 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1514 static int do_reload(int reload)
1516 struct ast_config *config;
1517 const char *enabled_value;
1518 const char *unanswered_value;
1519 const char *congestion_value;
1520 const char *batched_value;
1521 const char *scheduleronly_value;
1522 const char *batchsafeshutdown_value;
1523 const char *size_value;
1524 const char *time_value;
1525 const char *end_before_h_value;
1526 const char *initiatedseconds_value;
1532 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1534 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1538 ast_mutex_lock(&cdr_batch_lock);
1540 was_enabled = enabled;
1541 was_batchmode = batchmode;
1543 batchsize = BATCH_SIZE_DEFAULT;
1544 batchtime = BATCH_TIME_DEFAULT;
1545 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1546 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1547 enabled = ENABLED_DEFAULT;
1548 batchmode = BATCHMODE_DEFAULT;
1549 unanswered = UNANSWERED_DEFAULT;
1550 congestion = CONGESTION_DEFAULT;
1552 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1553 ast_mutex_unlock(&cdr_batch_lock);
1557 /* don't run the next scheduled CDR posting while reloading */
1558 AST_SCHED_DEL(sched, cdr_sched);
1561 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1562 enabled = ast_true(enabled_value);
1564 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1565 unanswered = ast_true(unanswered_value);
1567 if ((congestion_value = ast_variable_retrieve(config, "general", "congestion"))) {
1568 congestion = ast_true(congestion_value);
1570 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1571 batchmode = ast_true(batched_value);
1573 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1574 batchscheduleronly = ast_true(scheduleronly_value);
1576 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1577 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1579 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1580 if (sscanf(size_value, "%30d", &cfg_size) < 1)
1581 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1582 else if (cfg_size < 0)
1583 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1585 batchsize = cfg_size;
1587 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1588 if (sscanf(time_value, "%30d", &cfg_time) < 1)
1589 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1590 else if (cfg_time < 0)
1591 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1593 batchtime = cfg_time;
1595 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1596 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1597 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1598 ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1601 if (enabled && !batchmode) {
1602 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1603 } else if (enabled && batchmode) {
1604 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1605 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1607 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1610 /* if this reload enabled the CDR batch mode, create the background thread
1611 if it does not exist */
1612 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1613 ast_cond_init(&cdr_pending_cond, NULL);
1614 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1615 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1616 AST_SCHED_DEL(sched, cdr_sched);
1618 ast_cli_register(&cli_submit);
1619 ast_register_atexit(ast_cdr_engine_term);
1622 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1624 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1625 /* wake up the thread so it will exit */
1626 pthread_cancel(cdr_thread);
1627 pthread_kill(cdr_thread, SIGURG);
1628 pthread_join(cdr_thread, NULL);
1629 cdr_thread = AST_PTHREADT_NULL;
1630 ast_cond_destroy(&cdr_pending_cond);
1631 ast_cli_unregister(&cli_submit);
1632 ast_unregister_atexit(ast_cdr_engine_term);
1634 /* if leaving batch mode, then post the CDRs in the batch,
1635 and don't reschedule, since we are stopping CDR logging */
1636 if (!batchmode && was_batchmode) {
1637 ast_cdr_engine_term();
1643 ast_mutex_unlock(&cdr_batch_lock);
1644 ast_config_destroy(config);
1645 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1650 int ast_cdr_engine_init(void)
1654 sched = ast_sched_context_create();
1656 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1660 ast_cli_register(&cli_status);
1664 ast_mutex_lock(&cdr_batch_lock);
1666 ast_mutex_unlock(&cdr_batch_lock);
1672 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1673 hanging up channels, and then again, after the channel hangup timeout expires */
1674 void ast_cdr_engine_term(void)
1676 ast_cdr_submit_batch(batchsafeshutdown);
1679 int ast_cdr_engine_reload(void)
1681 return do_reload(1);
1684 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1686 struct ast_cdr *tmpcdr;
1687 struct ast_data *level;
1688 struct ast_var_t *variables;
1689 const char *var, *val;
1691 char workspace[256];
1698 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1699 level = ast_data_add_node(tree, "level");
1704 ast_data_add_int(level, "level_number", x);
1706 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1707 if (variables && (var = ast_var_name(variables)) &&
1708 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1709 && !ast_strlen_zero(val)) {
1710 ast_data_add_str(level, var, val);
1716 for (i = 0; cdr_readonly_vars[i]; i++) {
1717 workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
1718 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1722 ast_data_add_str(level, cdr_readonly_vars[i], tmp);