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 ast_verb (1, "congestion value: %d\n INYOURFACE", congestion);
794 /* if congestion log is disabled, pass the buck to ast_cdr_failed */
799 while (cdr && congestion) {
800 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
801 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
803 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
804 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
807 if (cdr->disposition < AST_CDR_CONGESTION) {
808 cdr->disposition = AST_CDR_CONGESTION;
815 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
816 if ast_cdr_disposition returns a non-zero value */
818 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
822 for (; cdr; cdr = cdr->next) {
823 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
824 return -1 to set disposition to FAILED */
828 case AST_CAUSE_NO_ANSWER:
829 ast_cdr_noanswer(cdr);
831 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
832 ast_cdr_congestion(cdr);
834 case AST_CAUSE_NORMAL:
843 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
845 for (; cdr; cdr = cdr->next) {
846 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
848 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
853 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
856 for (; cdr; cdr = cdr->next) {
857 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
859 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
860 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
865 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
868 for (; cdr; cdr = cdr->next) {
869 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
871 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
878 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
881 for (; cdr; cdr = cdr->next) {
882 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
885 cdr->disposition = disposition;
889 /* set cid info for one record */
890 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
898 /* Grab source from ANI or normal Caller*ID */
899 num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
900 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
901 ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
902 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
903 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
904 ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
906 if (c->caller.id.subaddress.valid) {
907 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
909 if (c->dialed.subaddress.valid) {
910 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
914 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
916 for (; cdr; cdr = cdr->next) {
917 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
923 static int cdr_seq_inc(struct ast_cdr *cdr)
925 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
928 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
930 for ( ; cdr ; cdr = cdr->next) {
931 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
932 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
936 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
937 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
938 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
939 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
940 /* Destination information */
941 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
942 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
943 /* Unique call identifier */
944 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
945 /* Linked call identifier */
946 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
952 /* Three routines were "fixed" via 10668, and later shown that
953 users were depending on this behavior. ast_cdr_end,
954 ast_cdr_setvar and ast_cdr_answer are the three routines.
955 While most of the other routines would not touch
956 LOCKED cdr's, these three routines were designed to
957 operate on locked CDR's as a matter of course.
958 I now appreciate how this plays with the ForkCDR app,
959 which forms these cdr chains in the first place.
960 cdr_end is pretty key: all cdrs created are closed
961 together. They only vary by start time. Arithmetically,
962 users can calculate the subintervals they wish to track. */
964 void ast_cdr_end(struct ast_cdr *cdr)
966 for ( ; cdr ; cdr = cdr->next) {
967 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
970 if (ast_tvzero(cdr->end))
971 cdr->end = ast_tvnow();
972 if (ast_tvzero(cdr->start)) {
973 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
974 cdr->disposition = AST_CDR_FAILED;
976 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
977 if (ast_tvzero(cdr->answer)) {
978 if (cdr->disposition == AST_CDR_ANSWERED) {
979 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
980 cdr->disposition = AST_CDR_FAILED;
983 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
984 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
985 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
990 char *ast_cdr_disp2str(int disposition)
992 switch (disposition) {
994 return "NO ANSWER"; /* by default, for backward compatibility */
995 case AST_CDR_NOANSWER:
1001 case AST_CDR_ANSWERED:
1003 case AST_CDR_CONGESTION:
1004 return "CONGESTION";
1009 /*! Converts AMA flag to printable string */
1010 char *ast_cdr_flags2str(int flag)
1015 case AST_CDR_BILLING:
1017 case AST_CDR_DOCUMENTATION:
1018 return "DOCUMENTATION";
1023 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1025 struct ast_cdr *cdr = chan->cdr;
1026 const char *old_acct = "";
1028 if (!ast_strlen_zero(chan->accountcode)) {
1029 old_acct = ast_strdupa(chan->accountcode);
1032 ast_string_field_set(chan, accountcode, account);
1033 for ( ; cdr ; cdr = cdr->next) {
1034 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1035 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1039 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1042 "AccountCode: %s\r\n"
1043 "OldAccountCode: %s\r\n",
1044 chan->name, chan->uniqueid, chan->accountcode, old_acct);
1049 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1051 struct ast_cdr *cdr = chan->cdr;
1052 const char *old_acct = "";
1054 if (!ast_strlen_zero(chan->peeraccount)) {
1055 old_acct = ast_strdupa(chan->peeraccount);
1058 ast_string_field_set(chan, peeraccount, account);
1059 for ( ; cdr ; cdr = cdr->next) {
1060 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1061 ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1065 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1068 "PeerAccount: %s\r\n"
1069 "OldPeerAccount: %s\r\n",
1070 chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1075 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1077 struct ast_cdr *cdr;
1078 int newflag = ast_cdr_amaflags2int(flag);
1080 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1081 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1082 cdr->amaflags = newflag;
1090 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1092 struct ast_cdr *cdr = chan->cdr;
1094 for ( ; cdr ; cdr = cdr->next) {
1095 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1096 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1102 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1104 struct ast_cdr *cdr = chan->cdr;
1106 for ( ; cdr ; cdr = cdr->next) {
1107 int len = strlen(cdr->userfield);
1109 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1110 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1116 int ast_cdr_update(struct ast_channel *c)
1118 struct ast_cdr *cdr = c->cdr;
1120 for ( ; cdr ; cdr = cdr->next) {
1121 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1122 set_one_cid(cdr, c);
1124 /* Copy account code et-al */
1125 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1126 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1127 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1129 /* Destination information */ /* XXX privilege macro* ? */
1130 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1131 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1138 int ast_cdr_amaflags2int(const char *flag)
1140 if (!strcasecmp(flag, "default"))
1142 if (!strcasecmp(flag, "omit"))
1143 return AST_CDR_OMIT;
1144 if (!strcasecmp(flag, "billing"))
1145 return AST_CDR_BILLING;
1146 if (!strcasecmp(flag, "documentation"))
1147 return AST_CDR_DOCUMENTATION;
1151 static void post_cdr(struct ast_cdr *cdr)
1153 struct ast_cdr_beitem *i;
1155 for ( ; cdr ; cdr = cdr->next) {
1156 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1157 /* For people, who don't want to see unanswered single-channel events */
1158 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1162 /* don't post CDRs that are for dialed channels unless those
1163 * channels were originated from asterisk (pbx_spool, manager,
1165 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1166 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1171 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1172 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1174 AST_RWLIST_RDLOCK(&be_list);
1175 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1178 AST_RWLIST_UNLOCK(&be_list);
1182 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1184 struct ast_cdr *duplicate;
1185 struct ast_flags flags = { 0 };
1188 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1190 for ( ; cdr ; cdr = cdr->next) {
1191 /* Detach if post is requested */
1192 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1193 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1195 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1196 ast_cdr_detach(duplicate);
1198 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1201 /* enable CDR only */
1202 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1203 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1207 /* clear variables */
1208 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1209 ast_cdr_free_vars(cdr, 0);
1212 /* Reset to initial state */
1213 ast_clear_flag(cdr, AST_FLAGS_ALL);
1214 memset(&cdr->start, 0, sizeof(cdr->start));
1215 memset(&cdr->end, 0, sizeof(cdr->end));
1216 memset(&cdr->answer, 0, sizeof(cdr->answer));
1220 cdr->disposition = AST_CDR_NOANSWER;
1225 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1227 struct ast_flags flags = { 0 };
1230 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1232 /* Reset to initial state */
1233 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1234 ast_clear_flag(cdr, AST_FLAGS_ALL);
1235 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1237 ast_clear_flag(cdr, AST_FLAGS_ALL);
1240 memset(&cdr->start, 0, sizeof(cdr->start));
1241 memset(&cdr->end, 0, sizeof(cdr->end));
1242 memset(&cdr->answer, 0, sizeof(cdr->answer));
1246 cdr->disposition = AST_CDR_NULL;
1249 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1251 struct ast_cdr *ret;
1266 /*! \note Don't call without cdr_batch_lock */
1267 static void reset_batch(void)
1274 /*! \note Don't call without cdr_batch_lock */
1275 static int init_batch(void)
1277 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1278 if (!(batch = ast_malloc(sizeof(*batch))))
1286 static void *do_batch_backend_process(void *data)
1288 struct ast_cdr_batch_item *processeditem;
1289 struct ast_cdr_batch_item *batchitem = data;
1291 /* Push each CDR into storage mechanism(s) and free all the memory */
1293 post_cdr(batchitem->cdr);
1294 ast_cdr_free(batchitem->cdr);
1295 processeditem = batchitem;
1296 batchitem = batchitem->next;
1297 ast_free(processeditem);
1303 void ast_cdr_submit_batch(int do_shutdown)
1305 struct ast_cdr_batch_item *oldbatchitems = NULL;
1306 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1308 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1309 if (!batch || !batch->head)
1312 /* move the old CDRs aside, and prepare a new CDR batch */
1313 ast_mutex_lock(&cdr_batch_lock);
1314 oldbatchitems = batch->head;
1316 ast_mutex_unlock(&cdr_batch_lock);
1318 /* if configured, spawn a new thread to post these CDRs,
1319 also try to save as much as possible if we are shutting down safely */
1320 if (batchscheduleronly || do_shutdown) {
1321 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1322 do_batch_backend_process(oldbatchitems);
1324 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1325 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1326 do_batch_backend_process(oldbatchitems);
1328 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1333 static int submit_scheduled_batch(const void *data)
1335 ast_cdr_submit_batch(0);
1336 /* manually reschedule from this point in time */
1337 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1338 /* returning zero so the scheduler does not automatically reschedule */
1342 static void submit_unscheduled_batch(void)
1344 /* this is okay since we are not being called from within the scheduler */
1345 AST_SCHED_DEL(sched, cdr_sched);
1346 /* schedule the submission to occur ASAP (1 ms) */
1347 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1348 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1349 ast_mutex_lock(&cdr_pending_lock);
1350 ast_cond_signal(&cdr_pending_cond);
1351 ast_mutex_unlock(&cdr_pending_lock);
1354 void ast_cdr_detach(struct ast_cdr *cdr)
1356 struct ast_cdr_batch_item *newtail;
1362 /* maybe they disabled CDR stuff completely, so just drop it */
1364 ast_debug(1, "Dropping CDR !\n");
1365 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1370 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1377 /* otherwise, each CDR gets put into a batch list (at the end) */
1378 ast_debug(1, "CDR detaching from this thread\n");
1380 /* we'll need a new tail for every CDR */
1381 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1387 /* don't traverse a whole list (just keep track of the tail) */
1388 ast_mutex_lock(&cdr_batch_lock);
1392 /* new batch is empty, so point the head at the new tail */
1393 batch->head = newtail;
1395 /* already got a batch with something in it, so just append a new tail */
1396 batch->tail->next = newtail;
1399 batch->tail = newtail;
1400 curr = batch->size++;
1401 ast_mutex_unlock(&cdr_batch_lock);
1403 /* if we have enough stuff to post, then do it */
1404 if (curr >= (batchsize - 1))
1405 submit_unscheduled_batch();
1408 static void *do_cdr(void *data)
1410 struct timespec timeout;
1416 schedms = ast_sched_wait(sched);
1417 /* this shouldn't happen, but provide a 1 second default just in case */
1420 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1421 timeout.tv_sec = now.tv_sec;
1422 timeout.tv_nsec = now.tv_usec * 1000;
1423 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1424 ast_mutex_lock(&cdr_pending_lock);
1425 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1426 numevents = ast_sched_runq(sched);
1427 ast_mutex_unlock(&cdr_pending_lock);
1428 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1434 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1436 struct ast_cdr_beitem *beitem=NULL;
1438 long nextbatchtime=0;
1442 e->command = "cdr show status";
1444 "Usage: cdr show status\n"
1445 " Displays the Call Detail Record engine system status.\n";
1452 return CLI_SHOWUSAGE;
1454 ast_cli(a->fd, "\n");
1455 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1456 ast_cli(a->fd, "----------------------------------\n");
1457 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1458 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1460 ast_cli(a->fd, " Log unanswered calls: %s\n", unanswered ? "Yes" : "No");
1461 ast_cli(a->fd, " Log congestion: %s\n\n", congestion ? "Yes" : "No");
1463 ast_cli(a->fd, "* Batch Mode Settings\n");
1464 ast_cli(a->fd, " -------------------\n");
1468 nextbatchtime = ast_sched_when(sched, cdr_sched);
1469 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1470 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1471 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1472 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1473 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1474 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1476 ast_cli(a->fd, "* Registered Backends\n");
1477 ast_cli(a->fd, " -------------------\n");
1478 AST_RWLIST_RDLOCK(&be_list);
1479 if (AST_RWLIST_EMPTY(&be_list)) {
1480 ast_cli(a->fd, " (none)\n");
1482 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1483 ast_cli(a->fd, " %s\n", beitem->name);
1486 AST_RWLIST_UNLOCK(&be_list);
1487 ast_cli(a->fd, "\n");
1493 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1497 e->command = "cdr submit";
1499 "Usage: cdr submit\n"
1500 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1506 return CLI_SHOWUSAGE;
1508 submit_unscheduled_batch();
1509 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1514 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1515 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1517 static int do_reload(int reload)
1519 struct ast_config *config;
1520 const char *enabled_value;
1521 const char *unanswered_value;
1522 const char *congestion_value;
1523 const char *batched_value;
1524 const char *scheduleronly_value;
1525 const char *batchsafeshutdown_value;
1526 const char *size_value;
1527 const char *time_value;
1528 const char *end_before_h_value;
1529 const char *initiatedseconds_value;
1535 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1537 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1541 ast_mutex_lock(&cdr_batch_lock);
1543 was_enabled = enabled;
1544 was_batchmode = batchmode;
1546 batchsize = BATCH_SIZE_DEFAULT;
1547 batchtime = BATCH_TIME_DEFAULT;
1548 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1549 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1550 enabled = ENABLED_DEFAULT;
1551 batchmode = BATCHMODE_DEFAULT;
1552 unanswered = UNANSWERED_DEFAULT;
1553 congestion = CONGESTION_DEFAULT;
1555 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1556 ast_mutex_unlock(&cdr_batch_lock);
1560 /* don't run the next scheduled CDR posting while reloading */
1561 AST_SCHED_DEL(sched, cdr_sched);
1564 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1565 enabled = ast_true(enabled_value);
1567 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1568 unanswered = ast_true(unanswered_value);
1570 if ((congestion_value = ast_variable_retrieve(config, "general", "congestion"))) {
1571 ast_verb(1, "INTHEFACEPUNCH!\n");
1572 congestion = ast_true(congestion_value);
1574 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1575 batchmode = ast_true(batched_value);
1577 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1578 batchscheduleronly = ast_true(scheduleronly_value);
1580 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1581 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1583 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1584 if (sscanf(size_value, "%30d", &cfg_size) < 1)
1585 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1586 else if (cfg_size < 0)
1587 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1589 batchsize = cfg_size;
1591 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1592 if (sscanf(time_value, "%30d", &cfg_time) < 1)
1593 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1594 else if (cfg_time < 0)
1595 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1597 batchtime = cfg_time;
1599 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1600 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1601 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1602 ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1605 if (enabled && !batchmode) {
1606 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1607 } else if (enabled && batchmode) {
1608 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1609 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1611 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1614 /* if this reload enabled the CDR batch mode, create the background thread
1615 if it does not exist */
1616 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1617 ast_cond_init(&cdr_pending_cond, NULL);
1618 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1619 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1620 AST_SCHED_DEL(sched, cdr_sched);
1622 ast_cli_register(&cli_submit);
1623 ast_register_atexit(ast_cdr_engine_term);
1626 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1628 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1629 /* wake up the thread so it will exit */
1630 pthread_cancel(cdr_thread);
1631 pthread_kill(cdr_thread, SIGURG);
1632 pthread_join(cdr_thread, NULL);
1633 cdr_thread = AST_PTHREADT_NULL;
1634 ast_cond_destroy(&cdr_pending_cond);
1635 ast_cli_unregister(&cli_submit);
1636 ast_unregister_atexit(ast_cdr_engine_term);
1638 /* if leaving batch mode, then post the CDRs in the batch,
1639 and don't reschedule, since we are stopping CDR logging */
1640 if (!batchmode && was_batchmode) {
1641 ast_cdr_engine_term();
1647 ast_mutex_unlock(&cdr_batch_lock);
1648 ast_config_destroy(config);
1649 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1654 int ast_cdr_engine_init(void)
1658 sched = ast_sched_context_create();
1660 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1664 ast_cli_register(&cli_status);
1668 ast_mutex_lock(&cdr_batch_lock);
1670 ast_mutex_unlock(&cdr_batch_lock);
1676 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1677 hanging up channels, and then again, after the channel hangup timeout expires */
1678 void ast_cdr_engine_term(void)
1680 ast_cdr_submit_batch(batchsafeshutdown);
1683 int ast_cdr_engine_reload(void)
1685 return do_reload(1);
1688 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1690 struct ast_cdr *tmpcdr;
1691 struct ast_data *level;
1692 struct ast_var_t *variables;
1693 const char *var, *val;
1695 char workspace[256];
1702 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1703 level = ast_data_add_node(tree, "level");
1708 ast_data_add_int(level, "level_number", x);
1710 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1711 if (variables && (var = ast_var_name(variables)) &&
1712 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1713 && !ast_strlen_zero(val)) {
1714 ast_data_add_str(level, var, val);
1720 for (i = 0; cdr_readonly_vars[i]; i++) {
1721 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 */
1722 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1726 ast_data_add_str(level, cdr_readonly_vars[i], tmp);