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 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;
97 static const int BATCH_SIZE_DEFAULT = 100;
100 static const int BATCH_TIME_DEFAULT = 300;
102 static int batchscheduleronly;
103 static const int BATCH_SCHEDULER_ONLY_DEFAULT = 0;
105 static int batchsafeshutdown;
106 static const int BATCH_SAFE_SHUTDOWN_DEFAULT = 1;
108 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
110 /* these are used to wake up the CDR thread when there's work to do */
111 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
112 static ast_cond_t cdr_pending_cond;
114 int check_cdr_enabled()
119 /*! Register a CDR driver. Each registered CDR driver generates a CDR
120 \return 0 on success, -1 on failure
122 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
124 struct ast_cdr_beitem *i = NULL;
130 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
134 AST_RWLIST_WRLOCK(&be_list);
135 AST_RWLIST_TRAVERSE(&be_list, i, list) {
136 if (!strcasecmp(name, i->name)) {
137 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
138 AST_RWLIST_UNLOCK(&be_list);
143 if (!(i = ast_calloc(1, sizeof(*i))))
147 ast_copy_string(i->name, name, sizeof(i->name));
148 ast_copy_string(i->desc, desc, sizeof(i->desc));
150 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
151 AST_RWLIST_UNLOCK(&be_list);
156 /*! unregister a CDR driver */
157 void ast_cdr_unregister(const char *name)
159 struct ast_cdr_beitem *i = NULL;
161 AST_RWLIST_WRLOCK(&be_list);
162 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
163 if (!strcasecmp(name, i->name)) {
164 AST_RWLIST_REMOVE_CURRENT(list);
168 AST_RWLIST_TRAVERSE_SAFE_END;
169 AST_RWLIST_UNLOCK(&be_list);
172 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
177 int ast_cdr_isset_unanswered(void)
182 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
184 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
192 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
194 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
202 /*! Duplicate a CDR record
203 \returns Pointer to new CDR record
205 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
207 struct ast_cdr *newcdr;
209 if (!cdr) /* don't die if we get a null cdr pointer */
211 newcdr = ast_cdr_alloc();
215 memcpy(newcdr, cdr, sizeof(*newcdr));
216 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
217 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
218 ast_cdr_copy_vars(newcdr, cdr);
224 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
226 if (ast_strlen_zero(name))
229 for (; cdr; cdr = recur ? cdr->next : NULL) {
230 struct ast_var_t *variables;
231 struct varshead *headp = &cdr->varshead;
232 AST_LIST_TRAVERSE(headp, variables, entries) {
233 if (!strcasecmp(name, ast_var_name(variables)))
234 return ast_var_value(variables);
241 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
243 if (fmt == NULL) { /* raw mode */
244 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
249 ast_localtime(&when, &tm, NULL);
250 ast_strftime(buf, bufsize, fmt, &tm);
255 /*! CDR channel variable retrieval */
256 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
258 const char *fmt = "%Y-%m-%d %T";
261 if (!cdr) /* don't die if the cdr is null */
265 /* special vars (the ones from the struct ast_cdr when requested by name)
266 I'd almost say we should convert all the stringed vals to vars */
268 if (!strcasecmp(name, "clid"))
269 ast_copy_string(workspace, cdr->clid, workspacelen);
270 else if (!strcasecmp(name, "src"))
271 ast_copy_string(workspace, cdr->src, workspacelen);
272 else if (!strcasecmp(name, "dst"))
273 ast_copy_string(workspace, cdr->dst, workspacelen);
274 else if (!strcasecmp(name, "dcontext"))
275 ast_copy_string(workspace, cdr->dcontext, workspacelen);
276 else if (!strcasecmp(name, "channel"))
277 ast_copy_string(workspace, cdr->channel, workspacelen);
278 else if (!strcasecmp(name, "dstchannel"))
279 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
280 else if (!strcasecmp(name, "lastapp"))
281 ast_copy_string(workspace, cdr->lastapp, workspacelen);
282 else if (!strcasecmp(name, "lastdata"))
283 ast_copy_string(workspace, cdr->lastdata, workspacelen);
284 else if (!strcasecmp(name, "start"))
285 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
286 else if (!strcasecmp(name, "answer"))
287 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
288 else if (!strcasecmp(name, "end"))
289 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
290 else if (!strcasecmp(name, "duration"))
291 snprintf(workspace, workspacelen, "%ld", cdr->duration ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
292 else if (!strcasecmp(name, "billsec"))
293 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
294 else if (!strcasecmp(name, "disposition")) {
296 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
298 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
300 } else if (!strcasecmp(name, "amaflags")) {
302 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
304 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
306 } else if (!strcasecmp(name, "accountcode"))
307 ast_copy_string(workspace, cdr->accountcode, workspacelen);
308 else if (!strcasecmp(name, "peeraccount"))
309 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
310 else if (!strcasecmp(name, "uniqueid"))
311 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
312 else if (!strcasecmp(name, "linkedid"))
313 ast_copy_string(workspace, cdr->linkedid, workspacelen);
314 else if (!strcasecmp(name, "userfield"))
315 ast_copy_string(workspace, cdr->userfield, workspacelen);
316 else if (!strcasecmp(name, "sequence"))
317 snprintf(workspace, workspacelen, "%d", cdr->sequence);
318 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
319 ast_copy_string(workspace, varbuf, workspacelen);
323 if (!ast_strlen_zero(workspace))
327 /* readonly cdr variables */
328 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
329 "lastapp", "lastdata", "start", "answer", "end", "duration",
330 "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
331 "userfield", "sequence", NULL };
332 /*! Set a CDR channel variable
333 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
335 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
337 struct ast_var_t *newvariable;
338 struct varshead *headp;
341 for (x = 0; cdr_readonly_vars[x]; x++) {
342 if (!strcasecmp(name, cdr_readonly_vars[x])) {
343 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
349 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
353 for (; cdr; cdr = recur ? cdr->next : NULL) {
354 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
356 headp = &cdr->varshead;
357 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
358 if (!strcasecmp(ast_var_name(newvariable), name)) {
359 /* there is already such a variable, delete it */
360 AST_LIST_REMOVE_CURRENT(entries);
361 ast_var_delete(newvariable);
365 AST_LIST_TRAVERSE_SAFE_END;
368 newvariable = ast_var_assign(name, value);
369 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
376 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
378 struct ast_var_t *variables, *newvariable = NULL;
379 struct varshead *headpa, *headpb;
380 const char *var, *val;
383 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
386 headpa = &from_cdr->varshead;
387 headpb = &to_cdr->varshead;
389 AST_LIST_TRAVERSE(headpa,variables,entries) {
391 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
392 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
393 newvariable = ast_var_assign(var, val);
394 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
402 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
404 struct ast_var_t *variables;
408 int total = 0, x = 0, i;
412 for (; cdr; cdr = recur ? cdr->next : NULL) {
414 ast_str_append(buf, 0, "\n");
416 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
417 if (!(var = ast_var_name(variables))) {
421 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
422 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
429 for (i = 0; cdr_readonly_vars[i]; i++) {
430 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 */
431 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
435 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
436 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
447 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
450 /* clear variables */
451 for (; cdr; cdr = recur ? cdr->next : NULL) {
452 struct ast_var_t *vardata;
453 struct varshead *headp = &cdr->varshead;
454 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
455 ast_var_delete(vardata);
459 /*! \brief print a warning if cdr already posted */
460 static void check_post(struct ast_cdr *cdr)
464 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
465 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
468 void ast_cdr_free(struct ast_cdr *cdr)
472 struct ast_cdr *next = cdr->next;
474 ast_cdr_free_vars(cdr, 0);
480 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
481 void ast_cdr_discard(struct ast_cdr *cdr)
484 struct ast_cdr *next = cdr->next;
486 ast_cdr_free_vars(cdr, 0);
492 struct ast_cdr *ast_cdr_alloc(void)
495 x = ast_calloc(1, sizeof(*x));
497 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
501 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
503 struct ast_var_t *variablesfrom,*variablesto;
504 struct varshead *headpfrom = &to->varshead;
505 struct varshead *headpto = &from->varshead;
506 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
507 /* for every var in from, stick it in to */
508 const char *fromvarname, *fromvarval;
509 const char *tovarname = NULL, *tovarval = NULL;
510 fromvarname = ast_var_name(variablesfrom);
511 fromvarval = ast_var_value(variablesfrom);
514 /* now, quick see if that var is in the 'to' cdr already */
515 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
517 /* now, quick see if that var is in the 'to' cdr already */
518 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
519 tovarname = ast_var_name(variablesto);
520 tovarval = ast_var_value(variablesto);
524 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
525 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
527 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
530 /* rip this var out of the from cdr, and stick it in the to cdr */
531 AST_LIST_MOVE_CURRENT(headpto, entries);
533 AST_LIST_TRAVERSE_SAFE_END;
536 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
538 struct ast_cdr *zcdr;
539 struct ast_cdr *lto = NULL;
540 struct ast_cdr *lfrom = NULL;
541 int discard_from = 0;
546 /* don't merge into locked CDR's -- it's bad business */
547 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
548 zcdr = to; /* safety valve? */
554 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
555 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
556 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
561 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
562 struct ast_cdr *llfrom = NULL;
565 /* insert the from stuff after lto */
568 while (lfrom && lfrom->next) {
569 if (!lfrom->next->next)
573 /* rip off the last entry and put a copy of the to at the end */
577 /* save copy of the current *to cdr */
579 memcpy(&tcdr, to, sizeof(tcdr));
580 /* copy in the locked from cdr */
581 memcpy(to, from, sizeof(*to));
583 while (lfrom && lfrom->next) {
584 if (!lfrom->next->next)
589 /* rip off the last entry and put a copy of the to at the end */
591 to = to->next = ast_cdr_dup(&tcdr);
593 to = llfrom->next = ast_cdr_dup(&tcdr);
598 if (!ast_tvzero(from->start)) {
599 if (!ast_tvzero(to->start)) {
600 if (ast_tvcmp(to->start, from->start) > 0 ) {
601 to->start = from->start; /* use the earliest time */
602 from->start = ast_tv(0,0); /* we actively "steal" these values */
604 /* else nothing to do */
606 to->start = from->start;
607 from->start = ast_tv(0,0); /* we actively "steal" these values */
610 if (!ast_tvzero(from->answer)) {
611 if (!ast_tvzero(to->answer)) {
612 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
613 to->answer = from->answer; /* use the earliest time */
614 from->answer = ast_tv(0,0); /* we actively "steal" these values */
616 /* we got the earliest answer time, so we'll settle for that? */
618 to->answer = from->answer;
619 from->answer = ast_tv(0,0); /* we actively "steal" these values */
622 if (!ast_tvzero(from->end)) {
623 if (!ast_tvzero(to->end)) {
624 if (ast_tvcmp(to->end, from->end) < 0 ) {
625 to->end = from->end; /* use the latest time */
626 from->end = ast_tv(0,0); /* we actively "steal" these values */
627 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
628 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
630 /* else, nothing to do */
633 from->end = ast_tv(0,0); /* we actively "steal" these values */
634 to->duration = to->end.tv_sec - to->start.tv_sec;
635 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
638 if (to->disposition < from->disposition) {
639 to->disposition = from->disposition;
640 from->disposition = AST_CDR_NOANSWER;
642 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
643 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
644 from->lastapp[0] = 0; /* theft */
646 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
647 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
648 from->lastdata[0] = 0; /* theft */
650 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
651 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
652 from->dcontext[0] = 0; /* theft */
654 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
655 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
656 from->dstchannel[0] = 0; /* theft */
658 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
659 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
660 from->channel[0] = 0; /* theft */
662 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
663 ast_copy_string(to->src, from->src, sizeof(to->src));
664 from->src[0] = 0; /* theft */
666 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
667 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
668 from->clid[0] = 0; /* theft */
670 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
671 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
672 from->dst[0] = 0; /* theft */
675 to->amaflags = AST_CDR_DOCUMENTATION;
677 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
678 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
679 to->amaflags = from->amaflags;
681 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
682 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
684 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
685 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
687 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
688 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
690 /* flags, varsead, ? */
691 cdr_merge_vars(from, to);
693 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
694 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
695 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
696 ast_set_flag(to, AST_CDR_FLAG_POSTED);
697 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
698 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
699 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
700 ast_set_flag(to, AST_CDR_FLAG_CHILD);
701 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
702 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
704 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
706 /* just rip 'em off the 'from' and insert them on the 'to' */
708 from->next = zcdr->next;
710 /* zcdr is now ripped from the current list; */
711 ast_cdr_append(to, zcdr);
714 ast_cdr_discard(from);
717 void ast_cdr_start(struct ast_cdr *cdr)
721 for (; cdr; cdr = cdr->next) {
722 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
723 chan = S_OR(cdr->channel, "<unknown>");
725 cdr->start = ast_tvnow();
730 void ast_cdr_answer(struct ast_cdr *cdr)
733 for (; cdr; cdr = cdr->next) {
734 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
736 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
739 if (cdr->disposition < AST_CDR_ANSWERED)
740 cdr->disposition = AST_CDR_ANSWERED;
741 if (ast_tvzero(cdr->answer))
742 cdr->answer = ast_tvnow();
746 void ast_cdr_busy(struct ast_cdr *cdr)
749 for (; cdr; cdr = cdr->next) {
750 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
752 cdr->disposition = AST_CDR_BUSY;
757 void ast_cdr_failed(struct ast_cdr *cdr)
759 for (; cdr; cdr = cdr->next) {
761 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
763 if (cdr->disposition < AST_CDR_FAILED)
764 cdr->disposition = AST_CDR_FAILED;
769 void ast_cdr_noanswer(struct ast_cdr *cdr)
774 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
775 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
777 cdr->disposition = AST_CDR_NOANSWER;
783 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
784 if ast_cdr_disposition returns a non-zero value */
786 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
790 for (; cdr; cdr = cdr->next) {
791 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
792 return -1 to set disposition to FAILED */
796 case AST_CAUSE_NO_ANSWER:
797 ast_cdr_noanswer(cdr);
799 case AST_CAUSE_NORMAL:
808 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
810 for (; cdr; cdr = cdr->next) {
811 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
813 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
818 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
821 for (; cdr; cdr = cdr->next) {
822 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
824 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
825 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
830 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
833 for (; cdr; cdr = cdr->next) {
834 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
836 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
843 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
846 for (; cdr; cdr = cdr->next) {
847 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
850 cdr->disposition = disposition;
854 /* set cid info for one record */
855 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
863 /* Grab source from ANI or normal Caller*ID */
864 num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
865 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
866 ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
867 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
868 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
869 ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
871 if (c->caller.id.subaddress.valid) {
872 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
874 if (c->dialed.subaddress.valid) {
875 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
879 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
881 for (; cdr; cdr = cdr->next) {
882 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
888 static int cdr_seq_inc(struct ast_cdr *cdr)
890 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
893 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
897 for ( ; cdr ; cdr = cdr->next) {
898 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
899 chan = S_OR(cdr->channel, "<unknown>");
900 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
904 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
905 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
906 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
907 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
908 /* Destination information */
909 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
910 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
911 /* Unique call identifier */
912 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
913 /* Linked call identifier */
914 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
920 /* Three routines were "fixed" via 10668, and later shown that
921 users were depending on this behavior. ast_cdr_end,
922 ast_cdr_setvar and ast_cdr_answer are the three routines.
923 While most of the other routines would not touch
924 LOCKED cdr's, these three routines were designed to
925 operate on locked CDR's as a matter of course.
926 I now appreciate how this plays with the ForkCDR app,
927 which forms these cdr chains in the first place.
928 cdr_end is pretty key: all cdrs created are closed
929 together. They only vary by start time. Arithmetically,
930 users can calculate the subintervals they wish to track. */
932 void ast_cdr_end(struct ast_cdr *cdr)
934 for ( ; cdr ; cdr = cdr->next) {
935 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
938 if (ast_tvzero(cdr->end))
939 cdr->end = ast_tvnow();
940 if (ast_tvzero(cdr->start)) {
941 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
942 cdr->disposition = AST_CDR_FAILED;
944 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
945 if (ast_tvzero(cdr->answer)) {
946 if (cdr->disposition == AST_CDR_ANSWERED) {
947 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
948 cdr->disposition = AST_CDR_FAILED;
951 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
952 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
953 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
958 char *ast_cdr_disp2str(int disposition)
960 switch (disposition) {
962 return "NO ANSWER"; /* by default, for backward compatibility */
963 case AST_CDR_NOANSWER:
969 case AST_CDR_ANSWERED:
975 /*! Converts AMA flag to printable string */
976 char *ast_cdr_flags2str(int flag)
981 case AST_CDR_BILLING:
983 case AST_CDR_DOCUMENTATION:
984 return "DOCUMENTATION";
989 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
991 struct ast_cdr *cdr = chan->cdr;
992 const char *old_acct = "";
994 if (!ast_strlen_zero(chan->accountcode)) {
995 old_acct = ast_strdupa(chan->accountcode);
998 ast_string_field_set(chan, accountcode, account);
999 for ( ; cdr ; cdr = cdr->next) {
1000 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1001 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1005 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1008 "AccountCode: %s\r\n"
1009 "OldAccountCode: %s\r\n",
1010 chan->name, chan->uniqueid, chan->accountcode, old_acct);
1015 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1017 struct ast_cdr *cdr = chan->cdr;
1018 const char *old_acct = "";
1020 if (!ast_strlen_zero(chan->peeraccount)) {
1021 old_acct = ast_strdupa(chan->peeraccount);
1024 ast_string_field_set(chan, peeraccount, account);
1025 for ( ; cdr ; cdr = cdr->next) {
1026 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1027 ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1031 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1034 "PeerAccount: %s\r\n"
1035 "OldPeerAccount: %s\r\n",
1036 chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1041 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1043 struct ast_cdr *cdr;
1044 int newflag = ast_cdr_amaflags2int(flag);
1046 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1047 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1048 cdr->amaflags = newflag;
1056 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1058 struct ast_cdr *cdr = chan->cdr;
1060 for ( ; cdr ; cdr = cdr->next) {
1061 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1062 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1068 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1070 struct ast_cdr *cdr = chan->cdr;
1072 for ( ; cdr ; cdr = cdr->next) {
1073 int len = strlen(cdr->userfield);
1075 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1076 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1082 int ast_cdr_update(struct ast_channel *c)
1084 struct ast_cdr *cdr = c->cdr;
1086 for ( ; cdr ; cdr = cdr->next) {
1087 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1088 set_one_cid(cdr, c);
1090 /* Copy account code et-al */
1091 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1092 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1093 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1095 /* Destination information */ /* XXX privilege macro* ? */
1096 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1097 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1104 int ast_cdr_amaflags2int(const char *flag)
1106 if (!strcasecmp(flag, "default"))
1108 if (!strcasecmp(flag, "omit"))
1109 return AST_CDR_OMIT;
1110 if (!strcasecmp(flag, "billing"))
1111 return AST_CDR_BILLING;
1112 if (!strcasecmp(flag, "documentation"))
1113 return AST_CDR_DOCUMENTATION;
1117 static void post_cdr(struct ast_cdr *cdr)
1120 struct ast_cdr_beitem *i;
1122 for ( ; cdr ; cdr = cdr->next) {
1123 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1124 /* For people, who don't want to see unanswered single-channel events */
1125 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1129 /* don't post CDRs that are for dialed channels unless those
1130 * channels were originated from asterisk (pbx_spool, manager,
1132 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1133 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1137 chan = S_OR(cdr->channel, "<unknown>");
1139 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1140 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1142 AST_RWLIST_RDLOCK(&be_list);
1143 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1146 AST_RWLIST_UNLOCK(&be_list);
1150 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1152 struct ast_cdr *duplicate;
1153 struct ast_flags flags = { 0 };
1156 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1158 for ( ; cdr ; cdr = cdr->next) {
1159 /* Detach if post is requested */
1160 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1161 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1163 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1164 ast_cdr_detach(duplicate);
1166 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1169 /* enable CDR only */
1170 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1171 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1175 /* clear variables */
1176 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1177 ast_cdr_free_vars(cdr, 0);
1180 /* Reset to initial state */
1181 ast_clear_flag(cdr, AST_FLAGS_ALL);
1182 memset(&cdr->start, 0, sizeof(cdr->start));
1183 memset(&cdr->end, 0, sizeof(cdr->end));
1184 memset(&cdr->answer, 0, sizeof(cdr->answer));
1188 cdr->disposition = AST_CDR_NOANSWER;
1193 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1195 struct ast_flags flags = { 0 };
1198 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1200 /* Reset to initial state */
1201 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1202 ast_clear_flag(cdr, AST_FLAGS_ALL);
1203 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1205 ast_clear_flag(cdr, AST_FLAGS_ALL);
1208 memset(&cdr->start, 0, sizeof(cdr->start));
1209 memset(&cdr->end, 0, sizeof(cdr->end));
1210 memset(&cdr->answer, 0, sizeof(cdr->answer));
1214 cdr->disposition = AST_CDR_NULL;
1217 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1219 struct ast_cdr *ret;
1234 /*! \note Don't call without cdr_batch_lock */
1235 static void reset_batch(void)
1242 /*! \note Don't call without cdr_batch_lock */
1243 static int init_batch(void)
1245 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1246 if (!(batch = ast_malloc(sizeof(*batch))))
1254 static void *do_batch_backend_process(void *data)
1256 struct ast_cdr_batch_item *processeditem;
1257 struct ast_cdr_batch_item *batchitem = data;
1259 /* Push each CDR into storage mechanism(s) and free all the memory */
1261 post_cdr(batchitem->cdr);
1262 ast_cdr_free(batchitem->cdr);
1263 processeditem = batchitem;
1264 batchitem = batchitem->next;
1265 ast_free(processeditem);
1271 void ast_cdr_submit_batch(int do_shutdown)
1273 struct ast_cdr_batch_item *oldbatchitems = NULL;
1274 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1276 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1277 if (!batch || !batch->head)
1280 /* move the old CDRs aside, and prepare a new CDR batch */
1281 ast_mutex_lock(&cdr_batch_lock);
1282 oldbatchitems = batch->head;
1284 ast_mutex_unlock(&cdr_batch_lock);
1286 /* if configured, spawn a new thread to post these CDRs,
1287 also try to save as much as possible if we are shutting down safely */
1288 if (batchscheduleronly || do_shutdown) {
1289 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1290 do_batch_backend_process(oldbatchitems);
1292 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1293 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1294 do_batch_backend_process(oldbatchitems);
1296 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1301 static int submit_scheduled_batch(const void *data)
1303 ast_cdr_submit_batch(0);
1304 /* manually reschedule from this point in time */
1305 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1306 /* returning zero so the scheduler does not automatically reschedule */
1310 static void submit_unscheduled_batch(void)
1312 /* this is okay since we are not being called from within the scheduler */
1313 AST_SCHED_DEL(sched, cdr_sched);
1314 /* schedule the submission to occur ASAP (1 ms) */
1315 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1316 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1317 ast_mutex_lock(&cdr_pending_lock);
1318 ast_cond_signal(&cdr_pending_cond);
1319 ast_mutex_unlock(&cdr_pending_lock);
1322 void ast_cdr_detach(struct ast_cdr *cdr)
1324 struct ast_cdr_batch_item *newtail;
1330 /* maybe they disabled CDR stuff completely, so just drop it */
1332 ast_debug(1, "Dropping CDR !\n");
1333 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1338 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1345 /* otherwise, each CDR gets put into a batch list (at the end) */
1346 ast_debug(1, "CDR detaching from this thread\n");
1348 /* we'll need a new tail for every CDR */
1349 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1355 /* don't traverse a whole list (just keep track of the tail) */
1356 ast_mutex_lock(&cdr_batch_lock);
1360 /* new batch is empty, so point the head at the new tail */
1361 batch->head = newtail;
1363 /* already got a batch with something in it, so just append a new tail */
1364 batch->tail->next = newtail;
1367 batch->tail = newtail;
1368 curr = batch->size++;
1369 ast_mutex_unlock(&cdr_batch_lock);
1371 /* if we have enough stuff to post, then do it */
1372 if (curr >= (batchsize - 1))
1373 submit_unscheduled_batch();
1376 static void *do_cdr(void *data)
1378 struct timespec timeout;
1384 schedms = ast_sched_wait(sched);
1385 /* this shouldn't happen, but provide a 1 second default just in case */
1388 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1389 timeout.tv_sec = now.tv_sec;
1390 timeout.tv_nsec = now.tv_usec * 1000;
1391 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1392 ast_mutex_lock(&cdr_pending_lock);
1393 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1394 numevents = ast_sched_runq(sched);
1395 ast_mutex_unlock(&cdr_pending_lock);
1396 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1402 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1404 struct ast_cdr_beitem *beitem=NULL;
1406 long nextbatchtime=0;
1410 e->command = "cdr show status";
1412 "Usage: cdr show status\n"
1413 " Displays the Call Detail Record engine system status.\n";
1420 return CLI_SHOWUSAGE;
1422 ast_cli(a->fd, "\n");
1423 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1424 ast_cli(a->fd, "----------------------------------\n");
1425 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1426 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1428 ast_cli(a->fd, " Log unanswered calls: %s\n\n", unanswered ? "Yes" : "No");
1430 ast_cli(a->fd, "* Batch Mode Settings\n");
1431 ast_cli(a->fd, " -------------------\n");
1435 nextbatchtime = ast_sched_when(sched, cdr_sched);
1436 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1437 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1438 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1439 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1440 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1441 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1443 ast_cli(a->fd, "* Registered Backends\n");
1444 ast_cli(a->fd, " -------------------\n");
1445 AST_RWLIST_RDLOCK(&be_list);
1446 if (AST_RWLIST_EMPTY(&be_list)) {
1447 ast_cli(a->fd, " (none)\n");
1449 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1450 ast_cli(a->fd, " %s\n", beitem->name);
1453 AST_RWLIST_UNLOCK(&be_list);
1454 ast_cli(a->fd, "\n");
1460 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1464 e->command = "cdr submit";
1466 "Usage: cdr submit\n"
1467 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1473 return CLI_SHOWUSAGE;
1475 submit_unscheduled_batch();
1476 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1481 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1482 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1484 static int do_reload(int reload)
1486 struct ast_config *config;
1487 const char *enabled_value;
1488 const char *unanswered_value;
1489 const char *batched_value;
1490 const char *scheduleronly_value;
1491 const char *batchsafeshutdown_value;
1492 const char *size_value;
1493 const char *time_value;
1494 const char *end_before_h_value;
1495 const char *initiatedseconds_value;
1501 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1503 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1507 ast_mutex_lock(&cdr_batch_lock);
1509 was_enabled = enabled;
1510 was_batchmode = batchmode;
1512 batchsize = BATCH_SIZE_DEFAULT;
1513 batchtime = BATCH_TIME_DEFAULT;
1514 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1515 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1516 enabled = ENABLED_DEFAULT;
1517 batchmode = BATCHMODE_DEFAULT;
1518 unanswered = UNANSWERED_DEFAULT;
1520 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1521 ast_mutex_unlock(&cdr_batch_lock);
1525 /* don't run the next scheduled CDR posting while reloading */
1526 AST_SCHED_DEL(sched, cdr_sched);
1529 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1530 enabled = ast_true(enabled_value);
1532 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1533 unanswered = ast_true(unanswered_value);
1535 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1536 batchmode = ast_true(batched_value);
1538 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1539 batchscheduleronly = ast_true(scheduleronly_value);
1541 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1542 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1544 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1545 if (sscanf(size_value, "%30d", &cfg_size) < 1)
1546 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1547 else if (cfg_size < 0)
1548 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1550 batchsize = cfg_size;
1552 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1553 if (sscanf(time_value, "%30d", &cfg_time) < 1)
1554 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1555 else if (cfg_time < 0)
1556 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1558 batchtime = cfg_time;
1560 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1561 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1562 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1563 ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1566 if (enabled && !batchmode) {
1567 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1568 } else if (enabled && batchmode) {
1569 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1570 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1572 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1575 /* if this reload enabled the CDR batch mode, create the background thread
1576 if it does not exist */
1577 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1578 ast_cond_init(&cdr_pending_cond, NULL);
1579 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1580 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1581 AST_SCHED_DEL(sched, cdr_sched);
1583 ast_cli_register(&cli_submit);
1584 ast_register_atexit(ast_cdr_engine_term);
1587 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1589 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1590 /* wake up the thread so it will exit */
1591 pthread_cancel(cdr_thread);
1592 pthread_kill(cdr_thread, SIGURG);
1593 pthread_join(cdr_thread, NULL);
1594 cdr_thread = AST_PTHREADT_NULL;
1595 ast_cond_destroy(&cdr_pending_cond);
1596 ast_cli_unregister(&cli_submit);
1597 ast_unregister_atexit(ast_cdr_engine_term);
1599 /* if leaving batch mode, then post the CDRs in the batch,
1600 and don't reschedule, since we are stopping CDR logging */
1601 if (!batchmode && was_batchmode) {
1602 ast_cdr_engine_term();
1608 ast_mutex_unlock(&cdr_batch_lock);
1609 ast_config_destroy(config);
1610 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1615 int ast_cdr_engine_init(void)
1619 sched = sched_context_create();
1621 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1625 ast_cli_register(&cli_status);
1629 ast_mutex_lock(&cdr_batch_lock);
1631 ast_mutex_unlock(&cdr_batch_lock);
1637 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1638 hanging up channels, and then again, after the channel hangup timeout expires */
1639 void ast_cdr_engine_term(void)
1641 ast_cdr_submit_batch(batchsafeshutdown);
1644 int ast_cdr_engine_reload(void)
1646 return do_reload(1);
1649 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1651 struct ast_cdr *tmpcdr;
1652 struct ast_data *level;
1653 struct ast_var_t *variables;
1654 const char *var, *val;
1656 char workspace[256];
1663 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1664 level = ast_data_add_node(tree, "level");
1669 ast_data_add_int(level, "level_number", x);
1671 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1672 if (variables && (var = ast_var_name(variables)) &&
1673 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1674 && !ast_strlen_zero(val)) {
1675 ast_data_add_str(level, var, val);
1681 for (i = 0; cdr_readonly_vars[i]; i++) {
1682 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 */
1683 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1687 ast_data_add_str(level, cdr_readonly_vars[i], tmp);