2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2009, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
20 * \brief Channel Event Logging API
22 * \author Steve Murphy <murf@digium.com>
23 * \author Russell Bryant <russell@digium.com>
25 * \todo Do thorough testing of all transfer methods to ensure that BLINDTRANSFER,
26 * ATTENDEDTRANSFER, BRIDGE_START, and BRIDGE_END events are all reported
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/cel.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/config.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/astobj2.h"
46 /*! Is the CEL subsystem enabled ? */
47 static unsigned char cel_enabled;
49 /*! \brief CEL is off by default */
50 static const unsigned char CEL_ENALBED_DEFAULT = 0;
53 * \brief which events we want to track
55 * \note bit field, up to 64 events
57 static int64_t eventset;
60 * \brief Maximum possible CEL event IDs
61 * \note This limit is currently imposed by the eventset definition
63 #define CEL_MAX_EVENT_IDS 64
66 * \brief Track no events by default.
68 static const int64_t CEL_DEFAULT_EVENTS = 0;
71 * \brief Number of buckets for the appset container
73 static const int NUM_APP_BUCKETS = 97;
76 * \brief Container of Asterisk application names
78 * The apps in this container are the applications that were specified
79 * in the configuration as applications that CEL events should be generated
80 * for when they start and end on a channel.
82 static struct ao2_container *appset;
85 * \brief Configured date format for event timestamps
87 static char cel_dateformat[256];
90 * \brief Map of ast_cel_event_type to strings
92 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
94 [AST_CEL_CHANNEL_START] = "CHAN_START",
95 [AST_CEL_CHANNEL_END] = "CHAN_END",
96 [AST_CEL_ANSWER] = "ANSWER",
97 [AST_CEL_HANGUP] = "HANGUP",
98 [AST_CEL_APP_START] = "APP_START",
99 [AST_CEL_APP_END] = "APP_END",
100 [AST_CEL_BRIDGE_START] = "BRIDGE_START",
101 [AST_CEL_BRIDGE_END] = "BRIDGE_END",
102 [AST_CEL_BRIDGE_UPDATE] = "BRIDGE_UPDATE",
103 [AST_CEL_CONF_START] = "CONF_START",
104 [AST_CEL_CONF_END] = "CONF_END",
105 [AST_CEL_PARK_START] = "PARK_START",
106 [AST_CEL_PARK_END] = "PARK_END",
107 [AST_CEL_TRANSFER] = "TRANSFER",
108 [AST_CEL_USER_DEFINED] = "USER_DEFINED",
109 [AST_CEL_CONF_ENTER] = "CONF_ENTER",
110 [AST_CEL_CONF_EXIT] = "CONF_EXIT",
111 [AST_CEL_BLINDTRANSFER] = "BLINDTRANSFER",
112 [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
113 [AST_CEL_PICKUP] = "PICKUP",
114 [AST_CEL_FORWARD] = "FORWARD",
115 [AST_CEL_3WAY_START] = "3WAY_START",
116 [AST_CEL_3WAY_END] = "3WAY_END",
117 [AST_CEL_HOOKFLASH] = "HOOKFLASH",
118 [AST_CEL_LINKEDID_END] = "LINKEDID_END",
122 * \brief Map of ast_cel_ama_flags to strings
124 static const char * const cel_ama_flags[AST_CEL_AMA_FLAG_TOTAL] = {
125 [AST_CEL_AMA_FLAG_NONE] = "NONE",
126 [AST_CEL_AMA_FLAG_OMIT] = "OMIT",
127 [AST_CEL_AMA_FLAG_BILLING] = "BILLING",
128 [AST_CEL_AMA_FLAG_DOCUMENTATION] = "DOCUMENTATION",
131 unsigned int ast_cel_check_enabled(void)
136 static int print_app(void *obj, void *arg, int flags)
138 struct ast_cli_args *a = arg;
140 ast_cli(a->fd, "CEL Tracking Application: %s\n", (const char *) obj);
145 static void print_cel_sub(const struct ast_event *event, void *data)
147 struct ast_cli_args *a = data;
149 ast_cli(a->fd, "CEL Event Subscriber: %s\n",
150 ast_event_get_ie_str(event, AST_EVENT_IE_DESCRIPTION));
153 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
156 struct ast_event_sub *sub;
160 e->command = "cel show status";
162 "Usage: cel show status\n"
163 " Displays the Channel Event Logging system status.\n";
172 return CLI_SHOWUSAGE;
175 ast_cli(a->fd, "CEL Logging: %s\n", cel_enabled ? "Enabled" : "Disabled");
181 for (i = 0; i < (sizeof(eventset) * 8); i++) {
184 if (!(eventset & ((int64_t) 1 << i))) {
188 name = ast_cel_get_type_name(i);
189 if (strcasecmp(name, "Unknown")) {
190 ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
194 ao2_callback(appset, OBJ_NODATA, print_app, a);
196 if (!(sub = ast_event_subscribe_new(AST_EVENT_SUB, print_cel_sub, a))) {
199 ast_event_sub_append_ie_uint(sub, AST_EVENT_IE_EVENTTYPE, AST_EVENT_CEL);
200 ast_event_report_subs(sub);
201 ast_event_sub_destroy(sub);
207 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
209 enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
213 for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
214 if (!cel_event_types[i]) {
218 if (!strcasecmp(name, cel_event_types[i])) {
226 static int ast_cel_track_event(enum ast_cel_event_type et)
228 return (eventset & ((int64_t) 1 << et));
231 static void parse_events(const char *val)
233 char *events = ast_strdupa(val);
236 while ((cur_event = strsep(&events, ","))) {
237 enum ast_cel_event_type event_type;
239 cur_event = ast_strip(cur_event);
240 if (ast_strlen_zero(cur_event)) {
244 event_type = ast_cel_str_to_event_type(cur_event);
246 if (event_type == 0) {
248 eventset = (int64_t) -1;
249 } else if (event_type == -1) {
250 ast_log(LOG_WARNING, "Unknown event name '%s'\n",
253 eventset |= ((int64_t) 1 << event_type);
258 static void parse_apps(const char *val)
260 char *apps = ast_strdupa(val);
263 if (!ast_cel_track_event(AST_CEL_APP_START) && !ast_cel_track_event(AST_CEL_APP_END)) {
264 ast_log(LOG_WARNING, "An apps= config line, but not tracking APP events\n");
268 while ((cur_app = strsep(&apps, ","))) {
271 cur_app = ast_strip(cur_app);
272 if (ast_strlen_zero(cur_app)) {
276 if (!(app = ao2_alloc(strlen(cur_app) + 1, NULL))) {
279 strcpy(app, cur_app);
281 ao2_link(appset, app);
287 AST_MUTEX_DEFINE_STATIC(reload_lock);
289 static int do_reload(void)
291 struct ast_config *config;
292 const char *enabled_value;
295 struct ast_flags config_flags = { 0, };
298 ast_mutex_lock(&reload_lock);
300 /* Reset all settings before reloading configuration */
301 cel_enabled = CEL_ENALBED_DEFAULT;
302 eventset = CEL_DEFAULT_EVENTS;
303 *cel_dateformat = '\0';
304 ao2_callback(appset, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
306 config = ast_config_load2("cel.conf", "cel", config_flags);
308 if (config == CONFIG_STATUS_FILEMISSING) {
313 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
314 cel_enabled = ast_true(enabled_value);
321 /* get the date format for logging */
322 if ((s = ast_variable_retrieve(config, "general", "dateformat"))) {
323 ast_copy_string(cel_dateformat, s, sizeof(cel_dateformat));
326 if ((val = ast_variable_retrieve(config, "general", "events"))) {
330 if ((val = ast_variable_retrieve(config, "general", "apps"))) {
335 ast_verb(3, "CEL logging %sabled.\n", cel_enabled ? "en" : "dis");
337 ast_mutex_unlock(&reload_lock);
340 ast_config_destroy(config);
346 const char *ast_cel_get_type_name(enum ast_cel_event_type type)
348 return S_OR(cel_event_types[type], "Unknown");
351 const char *ast_cel_get_ama_flag_name(enum ast_cel_ama_flag flag)
353 if (flag < 0 || flag >= ARRAY_LEN(cel_ama_flags)) {
354 ast_log(LOG_WARNING, "Invalid AMA flag: %d\n", flag);
358 return S_OR(cel_ama_flags[flag], "Unknown");
361 /* called whenever a channel is destroyed or a linkedid is changed to
362 * potentially emit a CEL_LINKEDID_END event */
364 struct channel_find_data {
365 const struct ast_channel *chan;
366 const char *linkedid;
369 static int linkedid_match(void *obj, void *arg, void *data, int flags)
371 struct ast_channel *c = obj;
372 struct channel_find_data *find_dat = data;
376 res = (c != find_dat->chan && c->linkedid && !strcmp(find_dat->linkedid, c->linkedid));
377 ast_channel_unlock(c);
379 return res ? CMP_MATCH | CMP_STOP : 0;
382 void ast_cel_check_retire_linkedid(struct ast_channel *chan)
384 const char *linkedid = chan->linkedid;
385 struct channel_find_data find_dat;
387 /* make sure we need to do all this work */
389 if (!ast_strlen_zero(linkedid) && ast_cel_track_event(AST_CEL_LINKEDID_END)) {
390 struct ast_channel *tmp = NULL;
391 find_dat.chan = chan;
392 find_dat.linkedid = linkedid;
393 if ((tmp = ast_channel_callback(linkedid_match, NULL, &find_dat, 0))) {
394 tmp = ast_channel_unref(tmp);
396 ast_cel_report_event(chan, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
401 struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
403 struct varshead *headp;
404 struct ast_var_t *newvariable;
406 struct ast_channel *tchan;
407 struct ast_cel_event_record record = {
408 .version = AST_CEL_EVENT_RECORD_VERSION,
411 /* do not call ast_channel_alloc because this is not really a real channel */
412 if (!(tchan = ast_dummy_channel_alloc())) {
416 headp = &tchan->varshead;
418 /* first, get the variables from the event */
419 if (ast_cel_fill_record(event, &record)) {
420 ast_channel_unref(tchan);
424 /* next, fill the channel with their data */
425 if ((newvariable = ast_var_assign("eventtype", record.event_name))) {
426 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
429 if (ast_strlen_zero(cel_dateformat)) {
430 snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
431 (long) record.event_time.tv_usec);
434 ast_localtime(&record.event_time, &tm, NULL);
435 ast_strftime(timebuf, sizeof(timebuf), cel_dateformat, &tm);
438 if ((newvariable = ast_var_assign("eventtime", timebuf))) {
439 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
442 if ((newvariable = ast_var_assign("eventextra", record.extra))) {
443 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
446 tchan->caller.id.name.valid = 1;
447 tchan->caller.id.name.str = ast_strdup(record.caller_id_name);
448 tchan->caller.id.number.valid = 1;
449 tchan->caller.id.number.str = ast_strdup(record.caller_id_num);
450 tchan->caller.ani.number.valid = 1;
451 tchan->caller.ani.number.str = ast_strdup(record.caller_id_ani);
452 tchan->redirecting.from.number.valid = 1;
453 tchan->redirecting.from.number.str = ast_strdup(record.caller_id_rdnis);
454 tchan->dialed.number.str = ast_strdup(record.caller_id_dnid);
456 ast_copy_string(tchan->exten, record.extension, sizeof(tchan->exten));
457 ast_copy_string(tchan->context, record.context, sizeof(tchan->context));
458 ast_channel_name_set(tchan, record.channel_name);
459 ast_string_field_set(tchan, uniqueid, record.unique_id);
460 ast_string_field_set(tchan, linkedid, record.linked_id);
461 ast_string_field_set(tchan, accountcode, record.account_code);
462 ast_string_field_set(tchan, peeraccount, record.peer_account);
463 ast_string_field_set(tchan, userfield, record.user_field);
465 pbx_builtin_setvar_helper(tchan, "BRIDGEPEER", record.peer);
467 tchan->appl = ast_strdup(record.application_name);
468 tchan->data = ast_strdup(record.application_data);
469 tchan->amaflags = record.amaflag;
474 int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event_type,
475 const char *userdefevname, const char *extra, struct ast_channel *peer2)
477 struct timeval eventtime;
478 struct ast_event *ev;
479 const char *peername = "";
480 struct ast_channel *peer;
482 ast_channel_lock(chan);
483 peer = ast_bridged_channel(chan);
485 ast_channel_ref(peer);
487 ast_channel_unlock(chan);
489 /* Make sure a reload is not occurring while we're checking to see if this
490 * is an event that we care about. We could lose an important event in this
491 * process otherwise. */
492 ast_mutex_lock(&reload_lock);
494 if (!cel_enabled || !ast_cel_track_event(event_type)) {
495 ast_mutex_unlock(&reload_lock);
497 ast_channel_unref(peer);
502 if (event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END) {
504 if (!(app = ao2_find(appset, (char *) chan->appl, OBJ_POINTER))) {
505 ast_mutex_unlock(&reload_lock);
507 ast_channel_unref(peer);
514 ast_mutex_unlock(&reload_lock);
517 ast_channel_lock(peer);
518 peername = ast_strdupa(ast_channel_name(peer));
519 ast_channel_unlock(peer);
521 ast_channel_lock(peer2);
522 peername = ast_strdupa(ast_channel_name(peer2));
523 ast_channel_unlock(peer2);
526 if (!userdefevname) {
534 eventtime = ast_tvnow();
536 ast_channel_lock(chan);
538 ev = ast_event_new(AST_EVENT_CEL,
539 AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
540 AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
541 AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
542 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, userdefevname,
543 AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR,
544 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
545 AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR,
546 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
547 AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR,
548 S_COR(chan->caller.ani.number.valid, chan->caller.ani.number.str, ""),
549 AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR,
550 S_COR(chan->redirecting.from.number.valid, chan->redirecting.from.number.str, ""),
551 AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR,
552 S_OR(chan->dialed.number.str, ""),
553 AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, chan->exten,
554 AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, chan->context,
555 AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, ast_channel_name(chan),
556 AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->appl, ""),
557 AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->data, ""),
558 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, chan->amaflags,
559 AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, chan->accountcode,
560 AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, chan->peeraccount,
561 AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, chan->uniqueid,
562 AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, chan->linkedid,
563 AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, chan->userfield,
564 AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, extra,
565 AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, peername,
568 ast_channel_unlock(chan);
571 peer = ast_channel_unref(peer);
574 if (ev && ast_event_queue(ev)) {
575 ast_event_destroy(ev);
582 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
584 if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
585 ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record. "
586 "Please ensure all modules were compiled for "
587 "this version of Asterisk.\n");
591 r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
593 r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
594 r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
596 r->user_defined_name = "";
598 if (r->event_type == AST_CEL_USER_DEFINED) {
599 r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
600 r->event_name = r->user_defined_name;
602 r->event_name = ast_cel_get_type_name(r->event_type);
605 r->caller_id_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
606 r->caller_id_num = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
607 r->caller_id_ani = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
608 r->caller_id_rdnis = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
609 r->caller_id_dnid = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
610 r->extension = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
611 r->context = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
612 r->channel_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
613 r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
614 r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
615 r->account_code = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
616 r->peer_account = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
617 r->unique_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
618 r->linked_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
619 r->amaflag = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
620 r->user_field = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
621 r->peer = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
622 r->extra = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
627 static int app_hash(const void *obj, const int flags)
629 return ast_str_case_hash((const char *) obj);
632 static int app_cmp(void *obj, void *arg, int flags)
634 const char *app1 = obj, *app2 = arg;
636 return !strcasecmp(app1, app2) ? CMP_MATCH | CMP_STOP : 0;
639 static void ast_cel_engine_term(void)
647 int ast_cel_engine_init(void)
649 if (!(appset = ao2_container_alloc(NUM_APP_BUCKETS, app_hash, app_cmp))) {
659 if (ast_cli_register(&cli_status)) {
665 ast_register_atexit(ast_cel_engine_term);
670 int ast_cel_engine_reload(void)