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;
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(void)
120 * \brief Register a CDR driver. Each registered CDR driver generates a CDR
121 * \retval 0 on success.
122 * \retval -1 on error
124 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
126 struct ast_cdr_beitem *i = NULL;
132 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
136 AST_RWLIST_WRLOCK(&be_list);
137 AST_RWLIST_TRAVERSE(&be_list, i, list) {
138 if (!strcasecmp(name, i->name)) {
139 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
140 AST_RWLIST_UNLOCK(&be_list);
145 if (!(i = ast_calloc(1, sizeof(*i))))
149 ast_copy_string(i->name, name, sizeof(i->name));
150 ast_copy_string(i->desc, desc, sizeof(i->desc));
152 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
153 AST_RWLIST_UNLOCK(&be_list);
158 /*! unregister a CDR driver */
159 void ast_cdr_unregister(const char *name)
161 struct ast_cdr_beitem *i = NULL;
163 AST_RWLIST_WRLOCK(&be_list);
164 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
165 if (!strcasecmp(name, i->name)) {
166 AST_RWLIST_REMOVE_CURRENT(list);
170 AST_RWLIST_TRAVERSE_SAFE_END;
171 AST_RWLIST_UNLOCK(&be_list);
174 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
179 int ast_cdr_isset_unanswered(void)
184 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
186 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
194 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
196 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
204 /*! Duplicate a CDR record
205 \returns Pointer to new CDR record
207 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
209 struct ast_cdr *newcdr;
211 if (!cdr) /* don't die if we get a null cdr pointer */
213 newcdr = ast_cdr_alloc();
217 memcpy(newcdr, cdr, sizeof(*newcdr));
218 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
219 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
220 ast_cdr_copy_vars(newcdr, cdr);
226 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
228 if (ast_strlen_zero(name))
231 for (; cdr; cdr = recur ? cdr->next : NULL) {
232 struct ast_var_t *variables;
233 struct varshead *headp = &cdr->varshead;
234 AST_LIST_TRAVERSE(headp, variables, entries) {
235 if (!strcasecmp(name, ast_var_name(variables)))
236 return ast_var_value(variables);
243 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
245 if (fmt == NULL) { /* raw mode */
246 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
251 ast_localtime(&when, &tm, NULL);
252 ast_strftime(buf, bufsize, fmt, &tm);
257 /*! CDR channel variable retrieval */
258 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
260 const char *fmt = "%Y-%m-%d %T";
263 if (!cdr) /* don't die if the cdr is null */
267 /* special vars (the ones from the struct ast_cdr when requested by name)
268 I'd almost say we should convert all the stringed vals to vars */
270 if (!strcasecmp(name, "clid"))
271 ast_copy_string(workspace, cdr->clid, workspacelen);
272 else if (!strcasecmp(name, "src"))
273 ast_copy_string(workspace, cdr->src, workspacelen);
274 else if (!strcasecmp(name, "dst"))
275 ast_copy_string(workspace, cdr->dst, workspacelen);
276 else if (!strcasecmp(name, "dcontext"))
277 ast_copy_string(workspace, cdr->dcontext, workspacelen);
278 else if (!strcasecmp(name, "channel"))
279 ast_copy_string(workspace, cdr->channel, workspacelen);
280 else if (!strcasecmp(name, "dstchannel"))
281 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
282 else if (!strcasecmp(name, "lastapp"))
283 ast_copy_string(workspace, cdr->lastapp, workspacelen);
284 else if (!strcasecmp(name, "lastdata"))
285 ast_copy_string(workspace, cdr->lastdata, workspacelen);
286 else if (!strcasecmp(name, "start"))
287 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
288 else if (!strcasecmp(name, "answer"))
289 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
290 else if (!strcasecmp(name, "end"))
291 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
292 else if (!strcasecmp(name, "duration"))
293 snprintf(workspace, workspacelen, "%ld", cdr->duration ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
294 else if (!strcasecmp(name, "billsec"))
295 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
296 else if (!strcasecmp(name, "disposition")) {
298 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
300 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
302 } else if (!strcasecmp(name, "amaflags")) {
304 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
306 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
308 } else if (!strcasecmp(name, "accountcode"))
309 ast_copy_string(workspace, cdr->accountcode, workspacelen);
310 else if (!strcasecmp(name, "peeraccount"))
311 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
312 else if (!strcasecmp(name, "uniqueid"))
313 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
314 else if (!strcasecmp(name, "linkedid"))
315 ast_copy_string(workspace, cdr->linkedid, workspacelen);
316 else if (!strcasecmp(name, "userfield"))
317 ast_copy_string(workspace, cdr->userfield, workspacelen);
318 else if (!strcasecmp(name, "sequence"))
319 snprintf(workspace, workspacelen, "%d", cdr->sequence);
320 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
321 ast_copy_string(workspace, varbuf, workspacelen);
325 if (!ast_strlen_zero(workspace))
329 /* readonly cdr variables */
330 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
331 "lastapp", "lastdata", "start", "answer", "end", "duration",
332 "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
333 "userfield", "sequence", NULL };
334 /*! Set a CDR channel variable
335 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
337 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
339 struct ast_var_t *newvariable;
340 struct varshead *headp;
343 for (x = 0; cdr_readonly_vars[x]; x++) {
344 if (!strcasecmp(name, cdr_readonly_vars[x])) {
345 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
351 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
355 for (; cdr; cdr = recur ? cdr->next : NULL) {
356 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
358 headp = &cdr->varshead;
359 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
360 if (!strcasecmp(ast_var_name(newvariable), name)) {
361 /* there is already such a variable, delete it */
362 AST_LIST_REMOVE_CURRENT(entries);
363 ast_var_delete(newvariable);
367 AST_LIST_TRAVERSE_SAFE_END;
370 newvariable = ast_var_assign(name, value);
371 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
378 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
380 struct ast_var_t *variables, *newvariable = NULL;
381 struct varshead *headpa, *headpb;
382 const char *var, *val;
385 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
388 headpa = &from_cdr->varshead;
389 headpb = &to_cdr->varshead;
391 AST_LIST_TRAVERSE(headpa,variables,entries) {
393 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
394 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
395 newvariable = ast_var_assign(var, val);
396 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
404 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
406 struct ast_var_t *variables;
410 int total = 0, x = 0, i;
414 for (; cdr; cdr = recur ? cdr->next : NULL) {
416 ast_str_append(buf, 0, "\n");
418 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
419 if (!(var = ast_var_name(variables))) {
423 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
424 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
431 for (i = 0; cdr_readonly_vars[i]; i++) {
432 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 */
433 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
437 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
438 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
449 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
452 /* clear variables */
453 for (; cdr; cdr = recur ? cdr->next : NULL) {
454 struct ast_var_t *vardata;
455 struct varshead *headp = &cdr->varshead;
456 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
457 ast_var_delete(vardata);
461 /*! \brief print a warning if cdr already posted */
462 static void check_post(struct ast_cdr *cdr)
466 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
467 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
470 void ast_cdr_free(struct ast_cdr *cdr)
474 struct ast_cdr *next = cdr->next;
476 ast_cdr_free_vars(cdr, 0);
482 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
483 void ast_cdr_discard(struct ast_cdr *cdr)
486 struct ast_cdr *next = cdr->next;
488 ast_cdr_free_vars(cdr, 0);
494 struct ast_cdr *ast_cdr_alloc(void)
497 x = ast_calloc(1, sizeof(*x));
499 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
503 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
505 struct ast_var_t *variablesfrom,*variablesto;
506 struct varshead *headpfrom = &to->varshead;
507 struct varshead *headpto = &from->varshead;
508 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
509 /* for every var in from, stick it in to */
510 const char *fromvarname, *fromvarval;
511 const char *tovarname = NULL, *tovarval = NULL;
512 fromvarname = ast_var_name(variablesfrom);
513 fromvarval = ast_var_value(variablesfrom);
516 /* now, quick see if that var is in the 'to' cdr already */
517 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
519 /* now, quick see if that var is in the 'to' cdr already */
520 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
521 tovarname = ast_var_name(variablesto);
522 tovarval = ast_var_value(variablesto);
526 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
527 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
529 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
532 /* rip this var out of the from cdr, and stick it in the to cdr */
533 AST_LIST_MOVE_CURRENT(headpto, entries);
535 AST_LIST_TRAVERSE_SAFE_END;
538 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
540 struct ast_cdr *zcdr;
541 struct ast_cdr *lto = NULL;
542 struct ast_cdr *lfrom = NULL;
543 int discard_from = 0;
548 /* don't merge into locked CDR's -- it's bad business */
549 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
550 zcdr = to; /* safety valve? */
556 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
557 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
558 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
563 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
564 struct ast_cdr *llfrom = NULL;
567 /* insert the from stuff after lto */
570 while (lfrom && lfrom->next) {
571 if (!lfrom->next->next)
575 /* rip off the last entry and put a copy of the to at the end */
579 /* save copy of the current *to cdr */
581 memcpy(&tcdr, to, sizeof(tcdr));
582 /* copy in the locked from cdr */
583 memcpy(to, from, sizeof(*to));
585 while (lfrom && lfrom->next) {
586 if (!lfrom->next->next)
591 /* rip off the last entry and put a copy of the to at the end */
593 to = to->next = ast_cdr_dup(&tcdr);
595 to = llfrom->next = ast_cdr_dup(&tcdr);
600 if (!ast_tvzero(from->start)) {
601 if (!ast_tvzero(to->start)) {
602 if (ast_tvcmp(to->start, from->start) > 0 ) {
603 to->start = from->start; /* use the earliest time */
604 from->start = ast_tv(0,0); /* we actively "steal" these values */
606 /* else nothing to do */
608 to->start = from->start;
609 from->start = ast_tv(0,0); /* we actively "steal" these values */
612 if (!ast_tvzero(from->answer)) {
613 if (!ast_tvzero(to->answer)) {
614 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
615 to->answer = from->answer; /* use the earliest time */
616 from->answer = ast_tv(0,0); /* we actively "steal" these values */
618 /* we got the earliest answer time, so we'll settle for that? */
620 to->answer = from->answer;
621 from->answer = ast_tv(0,0); /* we actively "steal" these values */
624 if (!ast_tvzero(from->end)) {
625 if (!ast_tvzero(to->end)) {
626 if (ast_tvcmp(to->end, from->end) < 0 ) {
627 to->end = from->end; /* use the latest time */
628 from->end = ast_tv(0,0); /* we actively "steal" these values */
629 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
630 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
632 /* else, nothing to do */
635 from->end = ast_tv(0,0); /* we actively "steal" these values */
636 to->duration = to->end.tv_sec - to->start.tv_sec;
637 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
640 if (to->disposition < from->disposition) {
641 to->disposition = from->disposition;
642 from->disposition = AST_CDR_NOANSWER;
644 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
645 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
646 from->lastapp[0] = 0; /* theft */
648 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
649 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
650 from->lastdata[0] = 0; /* theft */
652 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
653 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
654 from->dcontext[0] = 0; /* theft */
656 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
657 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
658 from->dstchannel[0] = 0; /* theft */
660 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
661 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
662 from->channel[0] = 0; /* theft */
664 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
665 ast_copy_string(to->src, from->src, sizeof(to->src));
666 from->src[0] = 0; /* theft */
668 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
669 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
670 from->clid[0] = 0; /* theft */
672 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
673 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
674 from->dst[0] = 0; /* theft */
677 to->amaflags = AST_CDR_DOCUMENTATION;
679 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
680 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
681 to->amaflags = from->amaflags;
683 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
684 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
686 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
687 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
689 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
690 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
692 /* flags, varsead, ? */
693 cdr_merge_vars(from, to);
695 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
696 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
697 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
698 ast_set_flag(to, AST_CDR_FLAG_POSTED);
699 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
700 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
701 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
702 ast_set_flag(to, AST_CDR_FLAG_CHILD);
703 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
704 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
706 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
708 /* just rip 'em off the 'from' and insert them on the 'to' */
710 from->next = zcdr->next;
712 /* zcdr is now ripped from the current list; */
713 ast_cdr_append(to, zcdr);
716 ast_cdr_discard(from);
719 void ast_cdr_start(struct ast_cdr *cdr)
721 for (; cdr; cdr = cdr->next) {
722 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
724 cdr->start = ast_tvnow();
729 void ast_cdr_answer(struct ast_cdr *cdr)
732 for (; cdr; cdr = cdr->next) {
733 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
735 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
738 if (cdr->disposition < AST_CDR_ANSWERED)
739 cdr->disposition = AST_CDR_ANSWERED;
740 if (ast_tvzero(cdr->answer))
741 cdr->answer = ast_tvnow();
745 void ast_cdr_busy(struct ast_cdr *cdr)
748 for (; cdr; cdr = cdr->next) {
749 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
751 cdr->disposition = AST_CDR_BUSY;
756 void ast_cdr_failed(struct ast_cdr *cdr)
758 for (; cdr; cdr = cdr->next) {
760 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
762 if (cdr->disposition < AST_CDR_FAILED)
763 cdr->disposition = AST_CDR_FAILED;
768 void ast_cdr_noanswer(struct ast_cdr *cdr)
771 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
773 cdr->disposition = AST_CDR_NOANSWER;
779 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
780 if ast_cdr_disposition returns a non-zero value */
782 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
786 for (; cdr; cdr = cdr->next) {
787 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
788 return -1 to set disposition to FAILED */
792 case AST_CAUSE_NO_ANSWER:
793 ast_cdr_noanswer(cdr);
795 case AST_CAUSE_NORMAL:
804 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
806 for (; cdr; cdr = cdr->next) {
807 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
809 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
814 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
817 for (; cdr; cdr = cdr->next) {
818 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
820 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
821 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
826 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
829 for (; cdr; cdr = cdr->next) {
830 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
832 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
839 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
842 for (; cdr; cdr = cdr->next) {
843 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
846 cdr->disposition = disposition;
850 /* set cid info for one record */
851 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
859 /* Grab source from ANI or normal Caller*ID */
860 num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
861 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
862 ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
863 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
864 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
865 ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
867 if (c->caller.id.subaddress.valid) {
868 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
870 if (c->dialed.subaddress.valid) {
871 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
875 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
877 for (; cdr; cdr = cdr->next) {
878 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
884 static int cdr_seq_inc(struct ast_cdr *cdr)
886 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
889 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
891 for ( ; cdr ; cdr = cdr->next) {
892 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
893 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
897 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
898 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
899 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
900 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
901 /* Destination information */
902 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
903 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
904 /* Unique call identifier */
905 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
906 /* Linked call identifier */
907 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
913 /* Three routines were "fixed" via 10668, and later shown that
914 users were depending on this behavior. ast_cdr_end,
915 ast_cdr_setvar and ast_cdr_answer are the three routines.
916 While most of the other routines would not touch
917 LOCKED cdr's, these three routines were designed to
918 operate on locked CDR's as a matter of course.
919 I now appreciate how this plays with the ForkCDR app,
920 which forms these cdr chains in the first place.
921 cdr_end is pretty key: all cdrs created are closed
922 together. They only vary by start time. Arithmetically,
923 users can calculate the subintervals they wish to track. */
925 void ast_cdr_end(struct ast_cdr *cdr)
927 for ( ; cdr ; cdr = cdr->next) {
928 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
931 if (ast_tvzero(cdr->end))
932 cdr->end = ast_tvnow();
933 if (ast_tvzero(cdr->start)) {
934 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
935 cdr->disposition = AST_CDR_FAILED;
937 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
938 if (ast_tvzero(cdr->answer)) {
939 if (cdr->disposition == AST_CDR_ANSWERED) {
940 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
941 cdr->disposition = AST_CDR_FAILED;
944 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
945 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
946 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
951 char *ast_cdr_disp2str(int disposition)
953 switch (disposition) {
955 return "NO ANSWER"; /* by default, for backward compatibility */
956 case AST_CDR_NOANSWER:
962 case AST_CDR_ANSWERED:
968 /*! Converts AMA flag to printable string */
969 char *ast_cdr_flags2str(int flag)
974 case AST_CDR_BILLING:
976 case AST_CDR_DOCUMENTATION:
977 return "DOCUMENTATION";
982 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
984 struct ast_cdr *cdr = chan->cdr;
985 const char *old_acct = "";
987 if (!ast_strlen_zero(chan->accountcode)) {
988 old_acct = ast_strdupa(chan->accountcode);
991 ast_string_field_set(chan, accountcode, account);
992 for ( ; cdr ; cdr = cdr->next) {
993 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
994 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
998 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1001 "AccountCode: %s\r\n"
1002 "OldAccountCode: %s\r\n",
1003 chan->name, chan->uniqueid, chan->accountcode, old_acct);
1008 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1010 struct ast_cdr *cdr = chan->cdr;
1011 const char *old_acct = "";
1013 if (!ast_strlen_zero(chan->peeraccount)) {
1014 old_acct = ast_strdupa(chan->peeraccount);
1017 ast_string_field_set(chan, peeraccount, account);
1018 for ( ; cdr ; cdr = cdr->next) {
1019 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1020 ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1024 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1027 "PeerAccount: %s\r\n"
1028 "OldPeerAccount: %s\r\n",
1029 chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1034 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1036 struct ast_cdr *cdr;
1037 int newflag = ast_cdr_amaflags2int(flag);
1039 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1040 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1041 cdr->amaflags = newflag;
1049 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1051 struct ast_cdr *cdr = chan->cdr;
1053 for ( ; cdr ; cdr = cdr->next) {
1054 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1055 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1061 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1063 struct ast_cdr *cdr = chan->cdr;
1065 for ( ; cdr ; cdr = cdr->next) {
1066 int len = strlen(cdr->userfield);
1068 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1069 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1075 int ast_cdr_update(struct ast_channel *c)
1077 struct ast_cdr *cdr = c->cdr;
1079 for ( ; cdr ; cdr = cdr->next) {
1080 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1081 set_one_cid(cdr, c);
1083 /* Copy account code et-al */
1084 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1085 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1086 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1088 /* Destination information */ /* XXX privilege macro* ? */
1089 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1090 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1097 int ast_cdr_amaflags2int(const char *flag)
1099 if (!strcasecmp(flag, "default"))
1101 if (!strcasecmp(flag, "omit"))
1102 return AST_CDR_OMIT;
1103 if (!strcasecmp(flag, "billing"))
1104 return AST_CDR_BILLING;
1105 if (!strcasecmp(flag, "documentation"))
1106 return AST_CDR_DOCUMENTATION;
1110 static void post_cdr(struct ast_cdr *cdr)
1112 struct ast_cdr_beitem *i;
1114 for ( ; cdr ; cdr = cdr->next) {
1115 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1116 /* For people, who don't want to see unanswered single-channel events */
1117 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1121 /* don't post CDRs that are for dialed channels unless those
1122 * channels were originated from asterisk (pbx_spool, manager,
1124 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1125 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1130 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1131 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1133 AST_RWLIST_RDLOCK(&be_list);
1134 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1137 AST_RWLIST_UNLOCK(&be_list);
1141 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1143 struct ast_cdr *duplicate;
1144 struct ast_flags flags = { 0 };
1147 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1149 for ( ; cdr ; cdr = cdr->next) {
1150 /* Detach if post is requested */
1151 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1152 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1154 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1155 ast_cdr_detach(duplicate);
1157 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1160 /* enable CDR only */
1161 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1162 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1166 /* clear variables */
1167 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1168 ast_cdr_free_vars(cdr, 0);
1171 /* Reset to initial state */
1172 ast_clear_flag(cdr, AST_FLAGS_ALL);
1173 memset(&cdr->start, 0, sizeof(cdr->start));
1174 memset(&cdr->end, 0, sizeof(cdr->end));
1175 memset(&cdr->answer, 0, sizeof(cdr->answer));
1179 cdr->disposition = AST_CDR_NOANSWER;
1184 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1186 struct ast_flags flags = { 0 };
1189 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1191 /* Reset to initial state */
1192 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1193 ast_clear_flag(cdr, AST_FLAGS_ALL);
1194 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1196 ast_clear_flag(cdr, AST_FLAGS_ALL);
1199 memset(&cdr->start, 0, sizeof(cdr->start));
1200 memset(&cdr->end, 0, sizeof(cdr->end));
1201 memset(&cdr->answer, 0, sizeof(cdr->answer));
1205 cdr->disposition = AST_CDR_NULL;
1208 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1210 struct ast_cdr *ret;
1225 /*! \note Don't call without cdr_batch_lock */
1226 static void reset_batch(void)
1233 /*! \note Don't call without cdr_batch_lock */
1234 static int init_batch(void)
1236 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1237 if (!(batch = ast_malloc(sizeof(*batch))))
1245 static void *do_batch_backend_process(void *data)
1247 struct ast_cdr_batch_item *processeditem;
1248 struct ast_cdr_batch_item *batchitem = data;
1250 /* Push each CDR into storage mechanism(s) and free all the memory */
1252 post_cdr(batchitem->cdr);
1253 ast_cdr_free(batchitem->cdr);
1254 processeditem = batchitem;
1255 batchitem = batchitem->next;
1256 ast_free(processeditem);
1262 void ast_cdr_submit_batch(int do_shutdown)
1264 struct ast_cdr_batch_item *oldbatchitems = NULL;
1265 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1267 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1268 if (!batch || !batch->head)
1271 /* move the old CDRs aside, and prepare a new CDR batch */
1272 ast_mutex_lock(&cdr_batch_lock);
1273 oldbatchitems = batch->head;
1275 ast_mutex_unlock(&cdr_batch_lock);
1277 /* if configured, spawn a new thread to post these CDRs,
1278 also try to save as much as possible if we are shutting down safely */
1279 if (batchscheduleronly || do_shutdown) {
1280 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1281 do_batch_backend_process(oldbatchitems);
1283 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1284 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1285 do_batch_backend_process(oldbatchitems);
1287 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1292 static int submit_scheduled_batch(const void *data)
1294 ast_cdr_submit_batch(0);
1295 /* manually reschedule from this point in time */
1296 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1297 /* returning zero so the scheduler does not automatically reschedule */
1301 static void submit_unscheduled_batch(void)
1303 /* this is okay since we are not being called from within the scheduler */
1304 AST_SCHED_DEL(sched, cdr_sched);
1305 /* schedule the submission to occur ASAP (1 ms) */
1306 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1307 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1308 ast_mutex_lock(&cdr_pending_lock);
1309 ast_cond_signal(&cdr_pending_cond);
1310 ast_mutex_unlock(&cdr_pending_lock);
1313 void ast_cdr_detach(struct ast_cdr *cdr)
1315 struct ast_cdr_batch_item *newtail;
1321 /* maybe they disabled CDR stuff completely, so just drop it */
1323 ast_debug(1, "Dropping CDR !\n");
1324 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1329 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1336 /* otherwise, each CDR gets put into a batch list (at the end) */
1337 ast_debug(1, "CDR detaching from this thread\n");
1339 /* we'll need a new tail for every CDR */
1340 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1346 /* don't traverse a whole list (just keep track of the tail) */
1347 ast_mutex_lock(&cdr_batch_lock);
1351 /* new batch is empty, so point the head at the new tail */
1352 batch->head = newtail;
1354 /* already got a batch with something in it, so just append a new tail */
1355 batch->tail->next = newtail;
1358 batch->tail = newtail;
1359 curr = batch->size++;
1360 ast_mutex_unlock(&cdr_batch_lock);
1362 /* if we have enough stuff to post, then do it */
1363 if (curr >= (batchsize - 1))
1364 submit_unscheduled_batch();
1367 static void *do_cdr(void *data)
1369 struct timespec timeout;
1375 schedms = ast_sched_wait(sched);
1376 /* this shouldn't happen, but provide a 1 second default just in case */
1379 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1380 timeout.tv_sec = now.tv_sec;
1381 timeout.tv_nsec = now.tv_usec * 1000;
1382 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1383 ast_mutex_lock(&cdr_pending_lock);
1384 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1385 numevents = ast_sched_runq(sched);
1386 ast_mutex_unlock(&cdr_pending_lock);
1387 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1393 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1395 struct ast_cdr_beitem *beitem=NULL;
1397 long nextbatchtime=0;
1401 e->command = "cdr show status";
1403 "Usage: cdr show status\n"
1404 " Displays the Call Detail Record engine system status.\n";
1411 return CLI_SHOWUSAGE;
1413 ast_cli(a->fd, "\n");
1414 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1415 ast_cli(a->fd, "----------------------------------\n");
1416 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1417 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1419 ast_cli(a->fd, " Log unanswered calls: %s\n\n", unanswered ? "Yes" : "No");
1421 ast_cli(a->fd, "* Batch Mode Settings\n");
1422 ast_cli(a->fd, " -------------------\n");
1426 nextbatchtime = ast_sched_when(sched, cdr_sched);
1427 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1428 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1429 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1430 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1431 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1432 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1434 ast_cli(a->fd, "* Registered Backends\n");
1435 ast_cli(a->fd, " -------------------\n");
1436 AST_RWLIST_RDLOCK(&be_list);
1437 if (AST_RWLIST_EMPTY(&be_list)) {
1438 ast_cli(a->fd, " (none)\n");
1440 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1441 ast_cli(a->fd, " %s\n", beitem->name);
1444 AST_RWLIST_UNLOCK(&be_list);
1445 ast_cli(a->fd, "\n");
1451 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1455 e->command = "cdr submit";
1457 "Usage: cdr submit\n"
1458 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1464 return CLI_SHOWUSAGE;
1466 submit_unscheduled_batch();
1467 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1472 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1473 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1475 static int do_reload(int reload)
1477 struct ast_config *config;
1478 const char *enabled_value;
1479 const char *unanswered_value;
1480 const char *batched_value;
1481 const char *scheduleronly_value;
1482 const char *batchsafeshutdown_value;
1483 const char *size_value;
1484 const char *time_value;
1485 const char *end_before_h_value;
1486 const char *initiatedseconds_value;
1492 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1494 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1498 ast_mutex_lock(&cdr_batch_lock);
1500 was_enabled = enabled;
1501 was_batchmode = batchmode;
1503 batchsize = BATCH_SIZE_DEFAULT;
1504 batchtime = BATCH_TIME_DEFAULT;
1505 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1506 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1507 enabled = ENABLED_DEFAULT;
1508 batchmode = BATCHMODE_DEFAULT;
1509 unanswered = UNANSWERED_DEFAULT;
1511 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1512 ast_mutex_unlock(&cdr_batch_lock);
1516 /* don't run the next scheduled CDR posting while reloading */
1517 AST_SCHED_DEL(sched, cdr_sched);
1520 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1521 enabled = ast_true(enabled_value);
1523 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1524 unanswered = ast_true(unanswered_value);
1526 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1527 batchmode = ast_true(batched_value);
1529 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1530 batchscheduleronly = ast_true(scheduleronly_value);
1532 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1533 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1535 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1536 if (sscanf(size_value, "%30d", &cfg_size) < 1)
1537 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1538 else if (cfg_size < 0)
1539 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1541 batchsize = cfg_size;
1543 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1544 if (sscanf(time_value, "%30d", &cfg_time) < 1)
1545 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1546 else if (cfg_time < 0)
1547 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1549 batchtime = cfg_time;
1551 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1552 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1553 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1554 ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1557 if (enabled && !batchmode) {
1558 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1559 } else if (enabled && batchmode) {
1560 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1561 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1563 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1566 /* if this reload enabled the CDR batch mode, create the background thread
1567 if it does not exist */
1568 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1569 ast_cond_init(&cdr_pending_cond, NULL);
1570 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1571 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1572 AST_SCHED_DEL(sched, cdr_sched);
1574 ast_cli_register(&cli_submit);
1575 ast_register_atexit(ast_cdr_engine_term);
1578 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1580 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1581 /* wake up the thread so it will exit */
1582 pthread_cancel(cdr_thread);
1583 pthread_kill(cdr_thread, SIGURG);
1584 pthread_join(cdr_thread, NULL);
1585 cdr_thread = AST_PTHREADT_NULL;
1586 ast_cond_destroy(&cdr_pending_cond);
1587 ast_cli_unregister(&cli_submit);
1588 ast_unregister_atexit(ast_cdr_engine_term);
1590 /* if leaving batch mode, then post the CDRs in the batch,
1591 and don't reschedule, since we are stopping CDR logging */
1592 if (!batchmode && was_batchmode) {
1593 ast_cdr_engine_term();
1599 ast_mutex_unlock(&cdr_batch_lock);
1600 ast_config_destroy(config);
1601 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1606 int ast_cdr_engine_init(void)
1610 sched = ast_sched_context_create();
1612 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1616 ast_cli_register(&cli_status);
1620 ast_mutex_lock(&cdr_batch_lock);
1622 ast_mutex_unlock(&cdr_batch_lock);
1628 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1629 hanging up channels, and then again, after the channel hangup timeout expires */
1630 void ast_cdr_engine_term(void)
1632 ast_cdr_submit_batch(batchsafeshutdown);
1635 int ast_cdr_engine_reload(void)
1637 return do_reload(1);
1640 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1642 struct ast_cdr *tmpcdr;
1643 struct ast_data *level;
1644 struct ast_var_t *variables;
1645 const char *var, *val;
1647 char workspace[256];
1654 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1655 level = ast_data_add_node(tree, "level");
1660 ast_data_add_int(level, "level_number", x);
1662 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1663 if (variables && (var = ast_var_name(variables)) &&
1664 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1665 && !ast_strlen_zero(val)) {
1666 ast_data_add_str(level, var, val);
1672 for (i = 0; cdr_readonly_vars[i]; i++) {
1673 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 */
1674 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1678 ast_data_add_str(level, cdr_readonly_vars[i], tmp);