Add missing CEL logging fields to various CEL backends.
[asterisk/asterisk.git] / cel / cel_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008 - 2009, Digium, Inc.
5  *
6  * Steve Murphy <murf@digium.com>
7  * who freely borrowed code from the cdr equivalents
8  *     (see cdr/cdr_manager.c)
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Asterisk Channel Event records.
24  *
25  * See also
26  * \arg \ref AstCDR
27  * \arg \ref AstAMI
28  * \arg \ref Config_ami
29  * \ingroup cel_drivers
30  */
31
32 /*** MODULEINFO
33         <support_level>core</support_level>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/channel.h"
41 #include "asterisk/cel.h"
42 #include "asterisk/module.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/config.h"
47
48 static const char DATE_FORMAT[] = "%Y-%m-%d %T";
49
50 static const char CONF_FILE[] = "cel.conf";
51
52 static int enablecel;
53
54 static struct ast_event_sub *event_sub;
55
56 static void manager_log(const struct ast_event *event, void *userdata)
57 {
58         struct ast_tm timeresult;
59         char start_time[80] = "";
60         struct ast_cel_event_record record = {
61                 .version = AST_CEL_EVENT_RECORD_VERSION,
62         };
63
64         if (!enablecel) {
65                 return;
66         }
67
68         if (ast_cel_fill_record(event, &record)) {
69                 return;
70         }
71
72         ast_localtime(&record.event_time, &timeresult, NULL);
73         ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
74
75         manager_event(EVENT_FLAG_CALL, "CEL",
76                 "EventName: %s\r\n"
77                 "AccountCode: %s\r\n"
78                 "CallerIDnum: %s\r\n"
79                 "CallerIDname: %s\r\n"
80                 "CallerIDani: %s\r\n"
81                 "CallerIDrdnis: %s\r\n"
82                 "CallerIDdnid: %s\r\n"
83                 "Exten: %s\r\n"
84                 "Context: %s\r\n"
85                 "Channel: %s\r\n"
86                 "Application: %s\r\n"
87                 "AppData: %s\r\n"
88                 "EventTime: %s\r\n"
89                 "AMAFlags: %s\r\n"
90                 "UniqueID: %s\r\n"
91                 "LinkedID: %s\r\n"
92                 "Userfield: %s\r\n"
93                 "Peer: %s\r\n"
94                 "PeerAccount: %s\r\n"
95                 "Extra: %s\r\n",
96                 record.event_name,
97                 record.account_code,
98                 record.caller_id_num,
99                 record.caller_id_name,
100                 record.caller_id_ani,
101                 record.caller_id_rdnis,
102                 record.caller_id_dnid,
103                 record.extension,
104                 record.context,
105                 record.channel_name,
106                 record.application_name,
107                 record.application_data,
108                 start_time,
109                 ast_cel_get_ama_flag_name(record.amaflag),
110                 record.unique_id,
111                 record.linked_id,
112                 record.user_field,
113                 record.peer,
114                 record.peer_account,
115                 record.extra);
116 }
117
118 static int load_config(int reload)
119 {
120         const char *cat = NULL;
121         struct ast_config *cfg;
122         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
123         struct ast_variable *v;
124         int newenablecel = 0;
125
126         cfg = ast_config_load(CONF_FILE, config_flags);
127         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
128                 return 0;
129         }
130
131         if (!cfg) {
132                 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
133                 enablecel = 0;
134                 return -1;
135         }
136
137         while ((cat = ast_category_browse(cfg, cat))) {
138                 if (strcasecmp(cat, "manager")) {
139                         continue;
140                 }
141
142                 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
143                         if (!strcasecmp(v->name, "enabled")) {
144                                 newenablecel = ast_true(v->value);
145                         } else {
146                                 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
147                                                 "for cel_manager.\n", v->name);
148                         }
149                 }
150         }
151
152         ast_config_destroy(cfg);
153
154         if (enablecel && !newenablecel) {
155                 if (event_sub) {
156                         event_sub = ast_event_unsubscribe(event_sub);
157                 }
158         } else if (!enablecel && newenablecel) {
159                 event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
160                 if (!event_sub) {
161                         ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
162                 }
163         }
164         enablecel = newenablecel;
165
166         return 0;
167 }
168
169 static int unload_module(void)
170 {
171         if (event_sub) {
172                 event_sub = ast_event_unsubscribe(event_sub);
173         }
174         return 0;
175 }
176
177 static int load_module(void)
178 {
179         if (load_config(0)) {
180                 return AST_MODULE_LOAD_DECLINE;
181         }
182
183         return AST_MODULE_LOAD_SUCCESS;
184 }
185
186 static int reload(void)
187 {
188         return load_config(1);
189 }
190
191 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
192         .load = load_module,
193         .unload = unload_module,
194         .reload = reload,
195         .load_pri = AST_MODPRI_CDR_DRIVER,
196 );