Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
cel_manager.c
Go to the documentation of this file.
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/*** DOCUMENTATION
37 <managerEvent language="en_US" name="CEL">
38 <managerEventInstance class="EVENT_FLAG_CEL">
39 <since>
40 <version>13.2.0</version>
41 </since>
42 <synopsis>Raised when a Channel Event Log is generated for a channel.</synopsis>
43 <syntax>
44 <parameter name="EventName">
45 <para>
46 The name of the CEL event being raised. This can include
47 both the system defined CEL events, as well as user defined
48 events.
49 </para>
50 <note>
51 <para>All events listed here may not be raised, depending
52 on the configuration in <filename>cel.conf</filename>.</para>
53 </note>
54 <enumlist>
55 <enum name="CHAN_START">
56 <para>A channel was created.</para>
57 </enum>
58 <enum name="CHAN_END">
59 <para>A channel was terminated.</para>
60 </enum>
61 <enum name="ANSWER">
62 <para>A channel answered.</para>
63 </enum>
64 <enum name="HANGUP">
65 <para>A channel was hung up.</para>
66 </enum>
67 <enum name="BRIDGE_ENTER">
68 <para>A channel entered a bridge.</para>
69 </enum>
70 <enum name="BRIDGE_EXIT">
71 <para>A channel left a bridge.</para>
72 </enum>
73 <enum name="APP_START">
74 <para>A channel entered into a tracked application.</para>
75 </enum>
76 <enum name="APP_END">
77 <para>A channel left a tracked application.</para>
78 </enum>
79 <enum name="PARK_START">
80 <para>A channel was parked.</para>
81 </enum>
82 <enum name="PARK_END">
83 <para>A channel was unparked.</para>
84 </enum>
85 <enum name="BLINDTRANSFER">
86 <para>A channel initiated a blind transfer.</para>
87 </enum>
88 <enum name="ATTENDEDTRANSFER">
89 <para>A channel initiated an attended transfer.</para>
90 </enum>
91 <enum name="PICKUP">
92 <para>A channel initated a call pickup.</para>
93 </enum>
94 <enum name="FORWARD">
95 <para>A channel is being forwarded to another destination.</para>
96 </enum>
97 <enum name="LINKEDID_END">
98 <para>The linked ID associated with this channel is being retired.</para>
99 </enum>
100 <enum name="LOCAL_OPTIMIZE">
101 <para>A Local channel optimization has occurred.</para>
102 </enum>
103 <enum name="USER_DEFINED">
104 <para>A user defined type.</para>
105 <note>
106 <para>
107 This event is only present if <literal>show_user_defined</literal>
108 in <filename>cel.conf</filename> is <literal>True</literal>. Otherwise,
109 the user defined event will be placed directly in the
110 <replaceable>EventName</replaceable> field.
111 </para>
112 </note>
113 </enum>
114 </enumlist>
115 </parameter>
116 <parameter name="AccountCode">
117 <para>The channel's account code.</para>
118 </parameter>
119 <parameter name="CallerIDnum">
120 <para>The Caller ID number.</para>
121 </parameter>
122 <parameter name="CallerIDname">
123 <para>The Caller ID name.</para>
124 </parameter>
125 <parameter name="CallerIDani">
126 <para>The Caller ID Automatic Number Identification.</para>
127 </parameter>
128 <parameter name="CallerIDrdnis">
129 <para>The Caller ID Redirected Dialed Number Identification Service.</para>
130 </parameter>
131 <parameter name="CallerIDdnid">
132 <para>The Caller ID Dialed Number Identifier.</para>
133 </parameter>
134 <parameter name="Exten">
135 <para>The dialplan extension the channel is currently executing in.</para>
136 </parameter>
137 <parameter name="Context">
138 <para>The dialplan context the channel is currently executing in.</para>
139 </parameter>
140 <parameter name="Application">
141 <para>The dialplan application the channel is currently executing.</para>
142 </parameter>
143 <parameter name="AppData">
144 <para>The arguments passed to the dialplan <replaceable>Application</replaceable>.</para>
145 </parameter>
146 <parameter name="EventTime">
147 <para>The time the CEL event occurred.</para>
148 </parameter>
149 <parameter name="AMAFlags">
150 <para>A flag that informs a billing system how to treat the CEL.</para>
151 <enumlist>
152 <enum name="OMIT">
153 <para>This event should be ignored.</para>
154 </enum>
155 <enum name="BILLING">
156 <para>This event contains valid billing data.</para>
157 </enum>
158 <enum name="DOCUMENTATION">
159 <para>This event is for documentation purposes.</para>
160 </enum>
161 </enumlist>
162 </parameter>
163 <parameter name="UniqueID">
164 <para>The unique ID of the channel.</para>
165 </parameter>
166 <parameter name="LinkedID">
167 <para>The linked ID of the channel, which ties this event to other related channel's events.</para>
168 </parameter>
169 <parameter name="UserField">
170 <para>
171 A user defined field set on a channel, containing arbitrary
172 application specific data.
173 </para>
174 </parameter>
175 <parameter name="Peer">
176 <para>
177 If this channel is in a bridge, the channel that it is in
178 a bridge with.
179 </para>
180 </parameter>
181 <parameter name="PeerAccount">
182 <para>
183 If this channel is in a bridge, the accountcode of the
184 channel it is in a bridge with.
185 </para>
186 </parameter>
187 <parameter name="Extra">
188 <para>
189 Some events will have event specific data that accompanies the CEL record.
190 This extra data is JSON encoded, and is dependent on the event in
191 question.
192 </para>
193 </parameter>
194 </syntax>
195 </managerEventInstance>
196 </managerEvent>
197 ***/
198
199#include "asterisk.h"
200
201#include "asterisk/channel.h"
202#include "asterisk/cel.h"
203#include "asterisk/module.h"
204#include "asterisk/logger.h"
205#include "asterisk/utils.h"
206#include "asterisk/manager.h"
207#include "asterisk/config.h"
208
209static const char DATE_FORMAT[] = "%Y-%m-%d %T";
210
211static const char CONF_FILE[] = "cel.conf";
212
213/*! \brief AMI CEL is off by default */
214#define CEL_AMI_ENABLED_DEFAULT 0
215
216static int enablecel;
217
218/*! \brief show_user_def is off by default */
219#define CEL_SHOW_USERDEF_DEFAULT 0
220
221#define MANAGER_BACKEND_NAME "Manager Event Logging"
222
223/*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
224static unsigned char cel_show_user_def;
225
226static void manager_log(struct ast_event *event)
227{
228 struct ast_tm timeresult;
229 char start_time[80] = "";
230 char user_defined_header[160];
231 const char *event_name;
232 struct ast_cel_event_record record = {
234 };
235 RAII_VAR(char *, tenant_id, NULL, ast_free);
236
237 if (!enablecel) {
238 return;
239 }
240
241 if (ast_cel_fill_record(event, &record)) {
242 return;
243 }
244
245 ast_localtime(&record.event_time, &timeresult, NULL);
246 ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
247
248 event_name = record.event_name;
249 user_defined_header[0] = '\0';
250 if (record.event_type == AST_CEL_USER_DEFINED) {
251 if (cel_show_user_def) {
252 snprintf(user_defined_header, sizeof(user_defined_header),
253 "UserDefType: %s\r\n", record.user_defined_name);
254 } else {
256 }
257 }
258
259 if (!ast_strlen_zero(record.tenant_id)) {
260 ast_asprintf(&tenant_id, "TenantID: %s\r\n", record.tenant_id);
261 }
262
264 "EventName: %s\r\n"
265 "AccountCode: %s\r\n"
266 "CallerIDnum: %s\r\n"
267 "CallerIDname: %s\r\n"
268 "CallerIDani: %s\r\n"
269 "CallerIDrdnis: %s\r\n"
270 "CallerIDdnid: %s\r\n"
271 "Exten: %s\r\n"
272 "Context: %s\r\n"
273 "Channel: %s\r\n"
274 "Application: %s\r\n"
275 "AppData: %s\r\n"
276 "EventTime: %s\r\n"
277 "AMAFlags: %s\r\n"
278 "UniqueID: %s\r\n"
279 "LinkedID: %s\r\n"
280 "%s"
281 "Userfield: %s\r\n"
282 "Peer: %s\r\n"
283 "PeerAccount: %s\r\n"
284 "%s"
285 "Extra: %s\r\n",
287 record.account_code,
288 record.caller_id_num,
289 record.caller_id_name,
290 record.caller_id_ani,
291 record.caller_id_rdnis,
292 record.caller_id_dnid,
293 record.extension,
294 record.context,
295 record.channel_name,
296 record.application_name,
297 record.application_data,
298 start_time,
300 record.unique_id,
301 record.linked_id,
303 record.user_field,
304 record.peer,
305 record.peer_account,
306 user_defined_header,
307 record.extra);
308}
309
310static int load_config(int reload)
311{
312 const char *cat = NULL;
313 struct ast_config *cfg;
314 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
315 struct ast_variable *v;
316 int newenablecel = CEL_AMI_ENABLED_DEFAULT;
317 int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
318
319 cfg = ast_config_load(CONF_FILE, config_flags);
320 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
321 return 0;
322 }
323
324 if (cfg == CONFIG_STATUS_FILEINVALID) {
325 ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
326 CONF_FILE);
327 enablecel = 0;
328 return -1;
329 } else if (!cfg) {
330 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
331 enablecel = 0;
332 return -1;
333 }
334
335 while ((cat = ast_category_browse(cfg, cat))) {
336 if (strcasecmp(cat, "manager")) {
337 continue;
338 }
339
340 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
341 if (!strcasecmp(v->name, "enabled")) {
342 newenablecel = ast_true(v->value) ? 1 : 0;
343 } else if (!strcasecmp(v->name, "show_user_defined")) {
344 new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
345 } else {
346 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
347 "for cel_manager.\n", v->name);
348 }
349 }
350 }
351
353
354 cel_show_user_def = new_cel_show_user_def;
355 if (enablecel && !newenablecel) {
357 } else if (!enablecel && newenablecel) {
359 ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
360 }
361 }
362 enablecel = newenablecel;
363
364 return 0;
365}
366
367static int unload_module(void)
368{
370 return 0;
371}
372
373static int load_module(void)
374{
375 if (load_config(0)) {
377 }
378
380}
381
382static int reload(void)
383{
384 return load_config(1);
385}
386
387AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
388 .support_level = AST_MODULE_SUPPORT_CORE,
389 .load = load_module,
390 .unload = unload_module,
391 .reload = reload,
392 .load_pri = AST_MODPRI_CDR_DRIVER,
393 .requires = "cel",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_log
Definition: astobj2.c:42
Call Event Logging API.
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
Definition: cel.c:1786
@ AST_CEL_USER_DEFINED
a user-defined event, the event name field should be set
Definition: cel.h:69
int ast_cel_fill_record(const struct ast_event *event, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
Definition: cel.c:837
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Definition: cel.h:143
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
Definition: cel.c:1798
#define CEL_SHOW_USERDEF_DEFAULT
show_user_def is off by default
Definition: cel_manager.c:219
static const char DATE_FORMAT[]
Definition: cel_manager.c:209
static void manager_log(struct ast_event *event)
Definition: cel_manager.c:226
#define CEL_AMI_ENABLED_DEFAULT
AMI CEL is off by default.
Definition: cel_manager.c:214
static unsigned char cel_show_user_def
Definition: cel_manager.c:224
static int enablecel
Definition: cel_manager.c:216
static int load_module(void)
Definition: cel_manager.c:373
static int unload_module(void)
Definition: cel_manager.c:367
static int load_config(int reload)
Definition: cel_manager.c:310
static int reload(void)
Definition: cel_manager.c:382
#define MANAGER_BACKEND_NAME
Definition: cel_manager.c:221
static const char CONF_FILE[]
Definition: cel_manager.c:211
General Asterisk PBX channel definitions.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4390
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
@ CONFIG_FLAG_FILEUNCHANGED
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
Support for logging to various files, console and syslog Configuration in file logger....
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:254
#define EVENT_FLAG_CALL
Definition: manager.h:76
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CDR_DRIVER
Definition: module.h:345
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Helper struct for getting the fields out of a CEL event.
Definition: cel.h:138
const char * caller_id_dnid
Definition: cel.h:157
const char * application_data
Definition: cel.h:162
const char * account_code
Definition: cel.h:163
const char * caller_id_rdnis
Definition: cel.h:156
const char * extra
Definition: cel.h:171
const char * extension
Definition: cel.h:158
const char * caller_id_num
Definition: cel.h:154
const char * channel_name
Definition: cel.h:160
const char * linked_id
Definition: cel.h:166
const char * peer_account
Definition: cel.h:164
const char * peer
Definition: cel.h:170
enum ast_cel_event_type event_type
Definition: cel.h:149
const char * unique_id
Definition: cel.h:165
const char * user_defined_name
Definition: cel.h:152
const char * context
Definition: cel.h:159
const char * application_name
Definition: cel.h:161
struct timeval event_time
Definition: cel.h:150
uint32_t version
struct ABI version
Definition: cel.h:148
const char * tenant_id
Definition: cel.h:167
const char * user_field
Definition: cel.h:169
const char * caller_id_ani
Definition: cel.h:155
const char * caller_id_name
Definition: cel.h:153
const char * event_name
Definition: cel.h:151
An event.
Definition: event.c:81
Structure used to handle boolean flags.
Definition: utils.h:199
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Definition: astman.c:222
Utility functions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941