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.
35 <support_level>core</support_level>
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/callerid.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/causes.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/config.h"
54 #include "asterisk/cli.h"
55 #include "asterisk/stringfields.h"
56 #include "asterisk/data.h"
58 /*! Default AMA flag for billing records (CDR's) */
59 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
60 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
62 struct ast_cdr_beitem {
66 AST_RWLIST_ENTRY(ast_cdr_beitem) list;
69 static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
71 struct ast_cdr_batch_item {
73 struct ast_cdr_batch_item *next;
76 static struct ast_cdr_batch {
78 struct ast_cdr_batch_item *head;
79 struct ast_cdr_batch_item *tail;
83 static int cdr_sequence = 0;
85 static int cdr_seq_inc(struct ast_cdr *cdr);
87 static struct ast_sched_context *sched;
88 static int cdr_sched = -1;
89 static pthread_t cdr_thread = AST_PTHREADT_NULL;
92 static const int ENABLED_DEFAULT = 1;
95 static const int BATCHMODE_DEFAULT = 0;
97 static int unanswered;
98 static const int UNANSWERED_DEFAULT = 0;
100 static int congestion;
101 static const int CONGESTION_DEFAULT = 0;
103 static int batchsize;
104 static const int BATCH_SIZE_DEFAULT = 100;
106 static int batchtime;
107 static const int BATCH_TIME_DEFAULT = 300;
109 static int batchscheduleronly;
110 static const int BATCH_SCHEDULER_ONLY_DEFAULT = 0;
112 static int batchsafeshutdown;
113 static const int BATCH_SAFE_SHUTDOWN_DEFAULT = 1;
115 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
117 /* these are used to wake up the CDR thread when there's work to do */
118 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
119 static ast_cond_t cdr_pending_cond;
121 int check_cdr_enabled(void)
127 * \brief Register a CDR driver. Each registered CDR driver generates a CDR
128 * \retval 0 on success.
129 * \retval -1 on error
131 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
133 struct ast_cdr_beitem *i = NULL;
139 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
143 AST_RWLIST_WRLOCK(&be_list);
144 AST_RWLIST_TRAVERSE(&be_list, i, list) {
145 if (!strcasecmp(name, i->name)) {
146 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
147 AST_RWLIST_UNLOCK(&be_list);
152 if (!(i = ast_calloc(1, sizeof(*i))))
156 ast_copy_string(i->name, name, sizeof(i->name));
157 ast_copy_string(i->desc, desc, sizeof(i->desc));
159 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
160 AST_RWLIST_UNLOCK(&be_list);
165 /*! unregister a CDR driver */
166 void ast_cdr_unregister(const char *name)
168 struct ast_cdr_beitem *i = NULL;
170 AST_RWLIST_WRLOCK(&be_list);
171 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
172 if (!strcasecmp(name, i->name)) {
173 AST_RWLIST_REMOVE_CURRENT(list);
177 AST_RWLIST_TRAVERSE_SAFE_END;
178 AST_RWLIST_UNLOCK(&be_list);
181 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
186 int ast_cdr_isset_unanswered(void)
191 int ast_cdr_isset_congestion(void)
196 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
198 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
206 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
208 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
216 /*! Duplicate a CDR record
217 \returns Pointer to new CDR record
219 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
221 struct ast_cdr *newcdr;
223 if (!cdr) /* don't die if we get a null cdr pointer */
225 newcdr = ast_cdr_alloc();
229 memcpy(newcdr, cdr, sizeof(*newcdr));
230 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
231 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
232 ast_cdr_copy_vars(newcdr, cdr);
238 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
240 if (ast_strlen_zero(name))
243 for (; cdr; cdr = recur ? cdr->next : NULL) {
244 struct ast_var_t *variables;
245 struct varshead *headp = &cdr->varshead;
246 AST_LIST_TRAVERSE(headp, variables, entries) {
247 if (!strcasecmp(name, ast_var_name(variables)))
248 return ast_var_value(variables);
255 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
257 if (fmt == NULL) { /* raw mode */
258 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
263 ast_localtime(&when, &tm, NULL);
264 ast_strftime(buf, bufsize, fmt, &tm);
269 /*! CDR channel variable retrieval */
270 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
272 const char *fmt = "%Y-%m-%d %T";
275 if (!cdr) /* don't die if the cdr is null */
279 /* special vars (the ones from the struct ast_cdr when requested by name)
280 I'd almost say we should convert all the stringed vals to vars */
282 if (!strcasecmp(name, "clid"))
283 ast_copy_string(workspace, cdr->clid, workspacelen);
284 else if (!strcasecmp(name, "src"))
285 ast_copy_string(workspace, cdr->src, workspacelen);
286 else if (!strcasecmp(name, "dst"))
287 ast_copy_string(workspace, cdr->dst, workspacelen);
288 else if (!strcasecmp(name, "dcontext"))
289 ast_copy_string(workspace, cdr->dcontext, workspacelen);
290 else if (!strcasecmp(name, "channel"))
291 ast_copy_string(workspace, cdr->channel, workspacelen);
292 else if (!strcasecmp(name, "dstchannel"))
293 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
294 else if (!strcasecmp(name, "lastapp"))
295 ast_copy_string(workspace, cdr->lastapp, workspacelen);
296 else if (!strcasecmp(name, "lastdata"))
297 ast_copy_string(workspace, cdr->lastdata, workspacelen);
298 else if (!strcasecmp(name, "start"))
299 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
300 else if (!strcasecmp(name, "answer"))
301 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
302 else if (!strcasecmp(name, "end"))
303 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
304 else if (!strcasecmp(name, "duration")) {
305 snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
306 } else if (!strcasecmp(name, "billsec"))
307 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
308 else if (!strcasecmp(name, "disposition")) {
310 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
312 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
314 } else if (!strcasecmp(name, "amaflags")) {
316 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
318 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
320 } else if (!strcasecmp(name, "accountcode"))
321 ast_copy_string(workspace, cdr->accountcode, workspacelen);
322 else if (!strcasecmp(name, "peeraccount"))
323 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
324 else if (!strcasecmp(name, "uniqueid"))
325 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
326 else if (!strcasecmp(name, "linkedid"))
327 ast_copy_string(workspace, cdr->linkedid, workspacelen);
328 else if (!strcasecmp(name, "userfield"))
329 ast_copy_string(workspace, cdr->userfield, workspacelen);
330 else if (!strcasecmp(name, "sequence"))
331 snprintf(workspace, workspacelen, "%d", cdr->sequence);
332 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
333 ast_copy_string(workspace, varbuf, workspacelen);
337 if (!ast_strlen_zero(workspace))
341 /* readonly cdr variables */
342 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
343 "lastapp", "lastdata", "start", "answer", "end", "duration",
344 "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
345 "userfield", "sequence", NULL };
346 /*! Set a CDR channel variable
347 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
349 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
351 struct ast_var_t *newvariable;
352 struct varshead *headp;
355 for (x = 0; cdr_readonly_vars[x]; x++) {
356 if (!strcasecmp(name, cdr_readonly_vars[x])) {
357 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
363 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
367 for (; cdr; cdr = recur ? cdr->next : NULL) {
368 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
370 headp = &cdr->varshead;
371 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
372 if (!strcasecmp(ast_var_name(newvariable), name)) {
373 /* there is already such a variable, delete it */
374 AST_LIST_REMOVE_CURRENT(entries);
375 ast_var_delete(newvariable);
379 AST_LIST_TRAVERSE_SAFE_END;
382 newvariable = ast_var_assign(name, value);
383 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
390 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
392 struct ast_var_t *variables, *newvariable = NULL;
393 struct varshead *headpa, *headpb;
394 const char *var, *val;
397 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
400 headpa = &from_cdr->varshead;
401 headpb = &to_cdr->varshead;
403 AST_LIST_TRAVERSE(headpa,variables,entries) {
405 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
406 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
407 newvariable = ast_var_assign(var, val);
408 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
416 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
418 struct ast_var_t *variables;
422 int total = 0, x = 0, i;
426 for (; cdr; cdr = recur ? cdr->next : NULL) {
428 ast_str_append(buf, 0, "\n");
430 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
431 if (!(var = ast_var_name(variables))) {
435 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
436 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
443 for (i = 0; cdr_readonly_vars[i]; i++) {
444 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 */
445 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
449 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
450 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
461 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
464 /* clear variables */
465 for (; cdr; cdr = recur ? cdr->next : NULL) {
466 struct ast_var_t *vardata;
467 struct varshead *headp = &cdr->varshead;
468 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
469 ast_var_delete(vardata);
473 /*! \brief print a warning if cdr already posted */
474 static void check_post(struct ast_cdr *cdr)
478 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
479 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
482 void ast_cdr_free(struct ast_cdr *cdr)
486 struct ast_cdr *next = cdr->next;
488 ast_cdr_free_vars(cdr, 0);
494 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
495 void ast_cdr_discard(struct ast_cdr *cdr)
498 struct ast_cdr *next = cdr->next;
500 ast_cdr_free_vars(cdr, 0);
506 struct ast_cdr *ast_cdr_alloc(void)
509 x = ast_calloc(1, sizeof(*x));
511 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
515 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
517 struct ast_var_t *variablesfrom,*variablesto;
518 struct varshead *headpfrom = &to->varshead;
519 struct varshead *headpto = &from->varshead;
520 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
521 /* for every var in from, stick it in to */
522 const char *fromvarname, *fromvarval;
523 const char *tovarname = NULL, *tovarval = NULL;
524 fromvarname = ast_var_name(variablesfrom);
525 fromvarval = ast_var_value(variablesfrom);
528 /* now, quick see if that var is in the 'to' cdr already */
529 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
531 /* now, quick see if that var is in the 'to' cdr already */
532 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
533 tovarname = ast_var_name(variablesto);
534 tovarval = ast_var_value(variablesto);
538 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
539 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
541 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
544 /* rip this var out of the from cdr, and stick it in the to cdr */
545 AST_LIST_MOVE_CURRENT(headpto, entries);
547 AST_LIST_TRAVERSE_SAFE_END;
550 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
552 struct ast_cdr *zcdr;
553 struct ast_cdr *lto = NULL;
554 struct ast_cdr *lfrom = NULL;
555 int discard_from = 0;
560 /* don't merge into locked CDR's -- it's bad business */
561 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
562 zcdr = to; /* safety valve? */
568 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
569 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.\n");
570 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
575 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
576 struct ast_cdr *llfrom = NULL;
579 /* insert the from stuff after lto */
582 while (lfrom && lfrom->next) {
583 if (!lfrom->next->next)
587 /* rip off the last entry and put a copy of the to at the end */
593 /* save copy of the current *to cdr */
595 memcpy(&tcdr, to, sizeof(tcdr));
596 /* copy in the locked from cdr */
597 memcpy(to, from, sizeof(*to));
599 while (lfrom && lfrom->next) {
600 if (!lfrom->next->next)
605 /* rip off the last entry and put a copy of the to at the end */
606 if (llfrom == from) {
607 to = to->next = ast_cdr_dup(&tcdr);
609 to = llfrom->next = ast_cdr_dup(&tcdr);
615 if (!ast_tvzero(from->start)) {
616 if (!ast_tvzero(to->start)) {
617 if (ast_tvcmp(to->start, from->start) > 0 ) {
618 to->start = from->start; /* use the earliest time */
619 from->start = ast_tv(0,0); /* we actively "steal" these values */
621 /* else nothing to do */
623 to->start = from->start;
624 from->start = ast_tv(0,0); /* we actively "steal" these values */
627 if (!ast_tvzero(from->answer)) {
628 if (!ast_tvzero(to->answer)) {
629 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
630 to->answer = from->answer; /* use the earliest time */
631 from->answer = ast_tv(0,0); /* we actively "steal" these values */
633 /* we got the earliest answer time, so we'll settle for that? */
635 to->answer = from->answer;
636 from->answer = ast_tv(0,0); /* we actively "steal" these values */
639 if (!ast_tvzero(from->end)) {
640 if (!ast_tvzero(to->end)) {
641 if (ast_tvcmp(to->end, from->end) < 0 ) {
642 to->end = from->end; /* use the latest time */
643 from->end = ast_tv(0,0); /* we actively "steal" these values */
644 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
645 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
647 /* else, nothing to do */
650 from->end = ast_tv(0,0); /* we actively "steal" these values */
651 to->duration = to->end.tv_sec - to->start.tv_sec;
652 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
655 if (to->disposition < from->disposition) {
656 to->disposition = from->disposition;
657 from->disposition = AST_CDR_NOANSWER;
659 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
660 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
661 from->lastapp[0] = 0; /* theft */
663 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
664 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
665 from->lastdata[0] = 0; /* theft */
667 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
668 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
669 from->dcontext[0] = 0; /* theft */
671 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
672 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
673 from->dstchannel[0] = 0; /* theft */
675 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
676 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
677 from->channel[0] = 0; /* theft */
679 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
680 ast_copy_string(to->src, from->src, sizeof(to->src));
681 from->src[0] = 0; /* theft */
683 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
684 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
685 from->clid[0] = 0; /* theft */
687 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
688 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
689 from->dst[0] = 0; /* theft */
692 to->amaflags = AST_CDR_DOCUMENTATION;
694 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
695 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
696 to->amaflags = from->amaflags;
698 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
699 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
701 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
702 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
704 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
705 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
707 /* flags, varsead, ? */
708 cdr_merge_vars(from, to);
710 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
711 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
712 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
713 ast_set_flag(to, AST_CDR_FLAG_POSTED);
714 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
715 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
716 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
717 ast_set_flag(to, AST_CDR_FLAG_CHILD);
718 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
719 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
721 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
723 /* just rip 'em off the 'from' and insert them on the 'to' */
725 from->next = zcdr->next;
727 /* zcdr is now ripped from the current list; */
728 ast_cdr_append(to, zcdr);
731 ast_cdr_discard(from);
734 void ast_cdr_start(struct ast_cdr *cdr)
736 for (; cdr; cdr = cdr->next) {
737 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
739 cdr->start = ast_tvnow();
744 void ast_cdr_answer(struct ast_cdr *cdr)
747 for (; cdr; cdr = cdr->next) {
748 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
750 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
753 if (cdr->disposition < AST_CDR_ANSWERED)
754 cdr->disposition = AST_CDR_ANSWERED;
755 if (ast_tvzero(cdr->answer))
756 cdr->answer = ast_tvnow();
760 void ast_cdr_busy(struct ast_cdr *cdr)
763 for (; cdr; cdr = cdr->next) {
764 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
766 cdr->disposition = AST_CDR_BUSY;
771 void ast_cdr_failed(struct ast_cdr *cdr)
773 for (; cdr; cdr = cdr->next) {
775 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
777 if (cdr->disposition < AST_CDR_FAILED)
778 cdr->disposition = AST_CDR_FAILED;
783 void ast_cdr_noanswer(struct ast_cdr *cdr)
786 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
788 cdr->disposition = AST_CDR_NOANSWER;
794 void ast_cdr_congestion(struct ast_cdr *cdr)
798 /* if congestion log is disabled, pass the buck to ast_cdr_failed */
803 while (cdr && congestion) {
804 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
805 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
807 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
808 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
811 if (cdr->disposition < AST_CDR_CONGESTION) {
812 cdr->disposition = AST_CDR_CONGESTION;
819 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
820 if ast_cdr_disposition returns a non-zero value */
822 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
826 for (; cdr; cdr = cdr->next) {
827 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
828 return -1 to set disposition to FAILED */
832 case AST_CAUSE_NO_ANSWER:
833 ast_cdr_noanswer(cdr);
835 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
836 ast_cdr_congestion(cdr);
838 case AST_CAUSE_NORMAL:
847 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
849 for (; cdr; cdr = cdr->next) {
850 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
852 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
857 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
860 for (; cdr; cdr = cdr->next) {
861 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
863 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
864 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
869 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
872 for (; cdr; cdr = cdr->next) {
873 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
875 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
882 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
885 for (; cdr; cdr = cdr->next) {
886 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
889 cdr->disposition = disposition;
893 /* set cid info for one record */
894 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
902 /* Grab source from ANI or normal Caller*ID */
903 num = S_COR(ast_channel_caller(c)->ani.number.valid, ast_channel_caller(c)->ani.number.str,
904 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL));
905 ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
906 S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, NULL), num, "");
907 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
908 ast_cdr_setvar(cdr, "dnid", S_OR(ast_channel_dialed(c)->number.str, ""), 0);
910 if (ast_channel_caller(c)->id.subaddress.valid) {
911 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(ast_channel_caller(c)->id.subaddress.str, ""), 0);
913 if (ast_channel_dialed(c)->subaddress.valid) {
914 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(ast_channel_dialed(c)->subaddress.str, ""), 0);
918 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
920 for (; cdr; cdr = cdr->next) {
921 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
927 static int cdr_seq_inc(struct ast_cdr *cdr)
929 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
932 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
934 for ( ; cdr ; cdr = cdr->next) {
935 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
936 ast_copy_string(cdr->channel, ast_channel_name(c), sizeof(cdr->channel));
940 cdr->disposition = (ast_channel_state(c) == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
941 cdr->amaflags = ast_channel_amaflags(c) ? ast_channel_amaflags(c) : ast_default_amaflags;
942 ast_copy_string(cdr->accountcode, ast_channel_accountcode(c), sizeof(cdr->accountcode));
943 ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(c), sizeof(cdr->peeraccount));
944 /* Destination information */
945 ast_copy_string(cdr->dst, S_OR(ast_channel_macroexten(c),ast_channel_exten(c)), sizeof(cdr->dst));
946 ast_copy_string(cdr->dcontext, S_OR(ast_channel_macrocontext(c),ast_channel_context(c)), sizeof(cdr->dcontext));
947 /* Unique call identifier */
948 ast_copy_string(cdr->uniqueid, ast_channel_uniqueid(c), sizeof(cdr->uniqueid));
949 /* Linked call identifier */
950 ast_copy_string(cdr->linkedid, ast_channel_linkedid(c), sizeof(cdr->linkedid));
956 /* Three routines were "fixed" via 10668, and later shown that
957 users were depending on this behavior. ast_cdr_end,
958 ast_cdr_setvar and ast_cdr_answer are the three routines.
959 While most of the other routines would not touch
960 LOCKED cdr's, these three routines were designed to
961 operate on locked CDR's as a matter of course.
962 I now appreciate how this plays with the ForkCDR app,
963 which forms these cdr chains in the first place.
964 cdr_end is pretty key: all cdrs created are closed
965 together. They only vary by start time. Arithmetically,
966 users can calculate the subintervals they wish to track. */
968 void ast_cdr_end(struct ast_cdr *cdr)
970 for ( ; cdr ; cdr = cdr->next) {
971 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
974 if (ast_tvzero(cdr->end))
975 cdr->end = ast_tvnow();
976 if (ast_tvzero(cdr->start)) {
977 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
978 cdr->disposition = AST_CDR_FAILED;
980 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
981 if (ast_tvzero(cdr->answer)) {
982 if (cdr->disposition == AST_CDR_ANSWERED) {
983 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
984 cdr->disposition = AST_CDR_FAILED;
987 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
988 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
989 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
994 char *ast_cdr_disp2str(int disposition)
996 switch (disposition) {
998 return "NO ANSWER"; /* by default, for backward compatibility */
999 case AST_CDR_NOANSWER:
1001 case AST_CDR_FAILED:
1005 case AST_CDR_ANSWERED:
1007 case AST_CDR_CONGESTION:
1008 return "CONGESTION";
1013 /*! Converts AMA flag to printable string */
1014 char *ast_cdr_flags2str(int flag)
1019 case AST_CDR_BILLING:
1021 case AST_CDR_DOCUMENTATION:
1022 return "DOCUMENTATION";
1027 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1029 struct ast_cdr *cdr = ast_channel_cdr(chan);
1030 const char *old_acct = "";
1032 if (!ast_strlen_zero(ast_channel_accountcode(chan))) {
1033 old_acct = ast_strdupa(ast_channel_accountcode(chan));
1036 ast_channel_accountcode_set(chan, account);
1037 for ( ; cdr ; cdr = cdr->next) {
1038 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1039 ast_copy_string(cdr->accountcode, ast_channel_accountcode(chan), sizeof(cdr->accountcode));
1043 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1046 "AccountCode: %s\r\n"
1047 "OldAccountCode: %s\r\n",
1048 ast_channel_name(chan), ast_channel_uniqueid(chan), ast_channel_accountcode(chan), old_acct);
1053 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1055 struct ast_cdr *cdr = ast_channel_cdr(chan);
1056 const char *old_acct = "";
1058 if (!ast_strlen_zero(ast_channel_peeraccount(chan))) {
1059 old_acct = ast_strdupa(ast_channel_peeraccount(chan));
1062 ast_channel_peeraccount_set(chan, account);
1063 for ( ; cdr ; cdr = cdr->next) {
1064 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1065 ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(chan), sizeof(cdr->peeraccount));
1069 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1072 "PeerAccount: %s\r\n"
1073 "OldPeerAccount: %s\r\n",
1074 ast_channel_name(chan), ast_channel_uniqueid(chan), ast_channel_peeraccount(chan), old_acct);
1079 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1081 struct ast_cdr *cdr;
1082 int newflag = ast_cdr_amaflags2int(flag);
1084 for (cdr = ast_channel_cdr(chan); cdr; cdr = cdr->next) {
1085 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1086 cdr->amaflags = newflag;
1094 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1096 struct ast_cdr *cdr = ast_channel_cdr(chan);
1098 for ( ; cdr ; cdr = cdr->next) {
1099 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1100 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1106 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1108 struct ast_cdr *cdr = ast_channel_cdr(chan);
1110 for ( ; cdr ; cdr = cdr->next) {
1111 int len = strlen(cdr->userfield);
1113 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1114 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1120 int ast_cdr_update(struct ast_channel *c)
1122 struct ast_cdr *cdr = ast_channel_cdr(c);
1124 for ( ; cdr ; cdr = cdr->next) {
1125 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1126 set_one_cid(cdr, c);
1128 /* Copy account code et-al */
1129 ast_copy_string(cdr->accountcode, ast_channel_accountcode(c), sizeof(cdr->accountcode));
1130 ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(c), sizeof(cdr->peeraccount));
1131 ast_copy_string(cdr->linkedid, ast_channel_linkedid(c), sizeof(cdr->linkedid));
1133 /* Destination information */ /* XXX privilege macro* ? */
1134 ast_copy_string(cdr->dst, S_OR(ast_channel_macroexten(c), ast_channel_exten(c)), sizeof(cdr->dst));
1135 ast_copy_string(cdr->dcontext, S_OR(ast_channel_macrocontext(c), ast_channel_context(c)), sizeof(cdr->dcontext));
1142 int ast_cdr_amaflags2int(const char *flag)
1144 if (!strcasecmp(flag, "default"))
1146 if (!strcasecmp(flag, "omit"))
1147 return AST_CDR_OMIT;
1148 if (!strcasecmp(flag, "billing"))
1149 return AST_CDR_BILLING;
1150 if (!strcasecmp(flag, "documentation"))
1151 return AST_CDR_DOCUMENTATION;
1155 static void post_cdr(struct ast_cdr *cdr)
1157 struct ast_cdr_beitem *i;
1159 for ( ; cdr ; cdr = cdr->next) {
1160 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1161 /* For people, who don't want to see unanswered single-channel events */
1162 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1166 /* don't post CDRs that are for dialed channels unless those
1167 * channels were originated from asterisk (pbx_spool, manager,
1169 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1170 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1175 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1176 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1178 AST_RWLIST_RDLOCK(&be_list);
1179 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1182 AST_RWLIST_UNLOCK(&be_list);
1186 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1188 struct ast_cdr *duplicate;
1189 struct ast_flags flags = { 0 };
1192 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1194 for ( ; cdr ; cdr = cdr->next) {
1195 /* Detach if post is requested */
1196 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1197 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1199 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1200 ast_cdr_detach(duplicate);
1202 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1205 /* enable CDR only */
1206 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1207 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1211 /* clear variables */
1212 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1213 ast_cdr_free_vars(cdr, 0);
1216 /* Reset to initial state */
1217 ast_clear_flag(cdr, AST_FLAGS_ALL);
1218 memset(&cdr->start, 0, sizeof(cdr->start));
1219 memset(&cdr->end, 0, sizeof(cdr->end));
1220 memset(&cdr->answer, 0, sizeof(cdr->answer));
1224 cdr->disposition = AST_CDR_NOANSWER;
1229 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1231 struct ast_flags flags = { 0 };
1234 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1236 /* Reset to initial state */
1237 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1238 ast_clear_flag(cdr, AST_FLAGS_ALL);
1239 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1241 ast_clear_flag(cdr, AST_FLAGS_ALL);
1244 memset(&cdr->start, 0, sizeof(cdr->start));
1245 memset(&cdr->end, 0, sizeof(cdr->end));
1246 memset(&cdr->answer, 0, sizeof(cdr->answer));
1250 cdr->disposition = AST_CDR_NULL;
1253 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1255 struct ast_cdr *ret;
1270 /*! \note Don't call without cdr_batch_lock */
1271 static void reset_batch(void)
1278 /*! \note Don't call without cdr_batch_lock */
1279 static int init_batch(void)
1281 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1282 if (!(batch = ast_malloc(sizeof(*batch))))
1290 static void *do_batch_backend_process(void *data)
1292 struct ast_cdr_batch_item *processeditem;
1293 struct ast_cdr_batch_item *batchitem = data;
1295 /* Push each CDR into storage mechanism(s) and free all the memory */
1297 post_cdr(batchitem->cdr);
1298 ast_cdr_free(batchitem->cdr);
1299 processeditem = batchitem;
1300 batchitem = batchitem->next;
1301 ast_free(processeditem);
1307 void ast_cdr_submit_batch(int do_shutdown)
1309 struct ast_cdr_batch_item *oldbatchitems = NULL;
1310 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1312 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1313 if (!batch || !batch->head)
1316 /* move the old CDRs aside, and prepare a new CDR batch */
1317 ast_mutex_lock(&cdr_batch_lock);
1318 oldbatchitems = batch->head;
1320 ast_mutex_unlock(&cdr_batch_lock);
1322 /* if configured, spawn a new thread to post these CDRs,
1323 also try to save as much as possible if we are shutting down safely */
1324 if (batchscheduleronly || do_shutdown) {
1325 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1326 do_batch_backend_process(oldbatchitems);
1328 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1329 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1330 do_batch_backend_process(oldbatchitems);
1332 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1337 static int submit_scheduled_batch(const void *data)
1339 ast_cdr_submit_batch(0);
1340 /* manually reschedule from this point in time */
1341 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1342 /* returning zero so the scheduler does not automatically reschedule */
1346 static void submit_unscheduled_batch(void)
1348 /* this is okay since we are not being called from within the scheduler */
1349 AST_SCHED_DEL(sched, cdr_sched);
1350 /* schedule the submission to occur ASAP (1 ms) */
1351 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1352 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1353 ast_mutex_lock(&cdr_pending_lock);
1354 ast_cond_signal(&cdr_pending_cond);
1355 ast_mutex_unlock(&cdr_pending_lock);
1358 void ast_cdr_detach(struct ast_cdr *cdr)
1360 struct ast_cdr_batch_item *newtail;
1366 /* maybe they disabled CDR stuff completely, so just drop it */
1368 ast_debug(1, "Dropping CDR !\n");
1369 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1374 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1381 /* otherwise, each CDR gets put into a batch list (at the end) */
1382 ast_debug(1, "CDR detaching from this thread\n");
1384 /* we'll need a new tail for every CDR */
1385 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1391 /* don't traverse a whole list (just keep track of the tail) */
1392 ast_mutex_lock(&cdr_batch_lock);
1396 /* new batch is empty, so point the head at the new tail */
1397 batch->head = newtail;
1399 /* already got a batch with something in it, so just append a new tail */
1400 batch->tail->next = newtail;
1403 batch->tail = newtail;
1404 curr = batch->size++;
1405 ast_mutex_unlock(&cdr_batch_lock);
1407 /* if we have enough stuff to post, then do it */
1408 if (curr >= (batchsize - 1))
1409 submit_unscheduled_batch();
1412 static void *do_cdr(void *data)
1414 struct timespec timeout;
1420 schedms = ast_sched_wait(sched);
1421 /* this shouldn't happen, but provide a 1 second default just in case */
1424 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1425 timeout.tv_sec = now.tv_sec;
1426 timeout.tv_nsec = now.tv_usec * 1000;
1427 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1428 ast_mutex_lock(&cdr_pending_lock);
1429 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1430 numevents = ast_sched_runq(sched);
1431 ast_mutex_unlock(&cdr_pending_lock);
1432 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1438 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1440 struct ast_cdr_beitem *beitem=NULL;
1442 long nextbatchtime=0;
1446 e->command = "cdr show status";
1448 "Usage: cdr show status\n"
1449 " Displays the Call Detail Record engine system status.\n";
1456 return CLI_SHOWUSAGE;
1458 ast_cli(a->fd, "\n");
1459 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1460 ast_cli(a->fd, "----------------------------------\n");
1461 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1462 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1464 ast_cli(a->fd, " Log unanswered calls: %s\n", unanswered ? "Yes" : "No");
1465 ast_cli(a->fd, " Log congestion: %s\n\n", congestion ? "Yes" : "No");
1467 ast_cli(a->fd, "* Batch Mode Settings\n");
1468 ast_cli(a->fd, " -------------------\n");
1472 nextbatchtime = ast_sched_when(sched, cdr_sched);
1473 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1474 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1475 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1476 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1477 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1478 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1480 ast_cli(a->fd, "* Registered Backends\n");
1481 ast_cli(a->fd, " -------------------\n");
1482 AST_RWLIST_RDLOCK(&be_list);
1483 if (AST_RWLIST_EMPTY(&be_list)) {
1484 ast_cli(a->fd, " (none)\n");
1486 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1487 ast_cli(a->fd, " %s\n", beitem->name);
1490 AST_RWLIST_UNLOCK(&be_list);
1491 ast_cli(a->fd, "\n");
1497 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1501 e->command = "cdr submit";
1503 "Usage: cdr submit\n"
1504 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1510 return CLI_SHOWUSAGE;
1512 submit_unscheduled_batch();
1513 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1518 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1519 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1521 static int do_reload(int reload)
1523 struct ast_config *config;
1524 struct ast_variable *v;
1530 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1532 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1536 ast_mutex_lock(&cdr_batch_lock);
1538 was_enabled = enabled;
1539 was_batchmode = batchmode;
1541 batchsize = BATCH_SIZE_DEFAULT;
1542 batchtime = BATCH_TIME_DEFAULT;
1543 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1544 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1545 enabled = ENABLED_DEFAULT;
1546 batchmode = BATCHMODE_DEFAULT;
1547 unanswered = UNANSWERED_DEFAULT;
1548 congestion = CONGESTION_DEFAULT;
1550 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1551 ast_mutex_unlock(&cdr_batch_lock);
1555 /* don't run the next scheduled CDR posting while reloading */
1556 AST_SCHED_DEL(sched, cdr_sched);
1558 for (v = ast_variable_browse(config, "general"); v; v = v->next) {
1559 if (!strcasecmp(v->name, "enable")) {
1560 enabled = ast_true(v->value);
1561 } else if (!strcasecmp(v->name, "unanswered")) {
1562 unanswered = ast_true(v->value);
1563 } else if (!strcasecmp(v->name, "congestion")) {
1564 congestion = ast_true(v->value);
1565 } else if (!strcasecmp(v->name, "batch")) {
1566 batchmode = ast_true(v->value);
1567 } else if (!strcasecmp(v->name, "scheduleronly")) {
1568 batchscheduleronly = ast_true(v->value);
1569 } else if (!strcasecmp(v->name, "safeshutdown")) {
1570 batchsafeshutdown = ast_true(v->value);
1571 } else if (!strcasecmp(v->name, "size")) {
1572 if (sscanf(v->value, "%30d", &cfg_size) < 1) {
1573 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1574 } else if (cfg_size < 0) {
1575 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1577 batchsize = cfg_size;
1579 } else if (!strcasecmp(v->name, "time")) {
1580 if (sscanf(v->value, "%30d", &cfg_time) < 1) {
1581 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1582 } else if (cfg_time < 0) {
1583 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1585 batchtime = cfg_time;
1587 } else if (!strcasecmp(v->name, "endbeforehexten")) {
1588 ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1589 } else if (!strcasecmp(v->name, "initiatedseconds")) {
1590 ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INITIATED_SECONDS);
1594 if (enabled && !batchmode) {
1595 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1596 } else if (enabled && batchmode) {
1597 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1598 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1600 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1603 /* if this reload enabled the CDR batch mode, create the background thread
1604 if it does not exist */
1605 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1606 ast_cond_init(&cdr_pending_cond, NULL);
1607 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1608 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1609 AST_SCHED_DEL(sched, cdr_sched);
1611 ast_cli_register(&cli_submit);
1612 ast_register_atexit(ast_cdr_engine_term);
1615 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1617 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1618 /* wake up the thread so it will exit */
1619 pthread_cancel(cdr_thread);
1620 pthread_kill(cdr_thread, SIGURG);
1621 pthread_join(cdr_thread, NULL);
1622 cdr_thread = AST_PTHREADT_NULL;
1623 ast_cond_destroy(&cdr_pending_cond);
1624 ast_cli_unregister(&cli_submit);
1625 ast_unregister_atexit(ast_cdr_engine_term);
1627 /* if leaving batch mode, then post the CDRs in the batch,
1628 and don't reschedule, since we are stopping CDR logging */
1629 if (!batchmode && was_batchmode) {
1630 ast_cdr_engine_term();
1636 ast_mutex_unlock(&cdr_batch_lock);
1637 ast_config_destroy(config);
1638 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1643 int ast_cdr_engine_init(void)
1647 sched = ast_sched_context_create();
1649 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1653 ast_cli_register(&cli_status);
1657 ast_mutex_lock(&cdr_batch_lock);
1659 ast_mutex_unlock(&cdr_batch_lock);
1665 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1666 hanging up channels, and then again, after the channel hangup timeout expires */
1667 void ast_cdr_engine_term(void)
1669 ast_cdr_submit_batch(batchsafeshutdown);
1672 int ast_cdr_engine_reload(void)
1674 return do_reload(1);
1677 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1679 struct ast_cdr *tmpcdr;
1680 struct ast_data *level;
1681 struct ast_var_t *variables;
1682 const char *var, *val;
1684 char workspace[256];
1691 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1692 level = ast_data_add_node(tree, "level");
1697 ast_data_add_int(level, "level_number", x);
1699 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1700 if (variables && (var = ast_var_name(variables)) &&
1701 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1702 && !ast_strlen_zero(val)) {
1703 ast_data_add_str(level, var, val);
1709 for (i = 0; cdr_readonly_vars[i]; i++) {
1710 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 */
1711 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1715 ast_data_add_str(level, cdr_readonly_vars[i], tmp);