Asterisk - The Open Source Telephony Project GIT-master-f36a736
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 <synopsis>Raised when a Channel Event Log is generated for a channel.</synopsis>
40 <syntax>
41 <parameter name="EventName">
42 <para>
43 The name of the CEL event being raised. This can include
44 both the system defined CEL events, as well as user defined
45 events.
46 </para>
47 <note>
48 <para>All events listed here may not be raised, depending
49 on the configuration in <filename>cel.conf</filename>.</para>
50 </note>
51 <enumlist>
52 <enum name="CHAN_START">
53 <para>A channel was created.</para>
54 </enum>
55 <enum name="CHAN_END">
56 <para>A channel was terminated.</para>
57 </enum>
58 <enum name="ANSWER">
59 <para>A channel answered.</para>
60 </enum>
61 <enum name="HANGUP">
62 <para>A channel was hung up.</para>
63 </enum>
64 <enum name="BRIDGE_ENTER">
65 <para>A channel entered a bridge.</para>
66 </enum>
67 <enum name="BRIDGE_EXIT">
68 <para>A channel left a bridge.</para>
69 </enum>
70 <enum name="APP_START">
71 <para>A channel entered into a tracked application.</para>
72 </enum>
73 <enum name="APP_END">
74 <para>A channel left a tracked application.</para>
75 </enum>
76 <enum name="PARK_START">
77 <para>A channel was parked.</para>
78 </enum>
79 <enum name="PARK_END">
80 <para>A channel was unparked.</para>
81 </enum>
82 <enum name="BLINDTRANSFER">
83 <para>A channel initiated a blind transfer.</para>
84 </enum>
85 <enum name="ATTENDEDTRANSFER">
86 <para>A channel initiated an attended transfer.</para>
87 </enum>
88 <enum name="PICKUP">
89 <para>A channel initated a call pickup.</para>
90 </enum>
91 <enum name="FORWARD">
92 <para>A channel is being forwarded to another destination.</para>
93 </enum>
94 <enum name="LINKEDID_END">
95 <para>The linked ID associated with this channel is being retired.</para>
96 </enum>
97 <enum name="LOCAL_OPTIMIZE">
98 <para>A Local channel optimization has occurred.</para>
99 </enum>
100 <enum name="USER_DEFINED">
101 <para>A user defined type.</para>
102 <note>
103 <para>
104 This event is only present if <literal>show_user_defined</literal>
105 in <filename>cel.conf</filename> is <literal>True</literal>. Otherwise,
106 the user defined event will be placed directly in the
107 <replaceable>EventName</replaceable> field.
108 </para>
109 </note>
110 </enum>
111 </enumlist>
112 </parameter>
113 <parameter name="AccountCode">
114 <para>The channel's account code.</para>
115 </parameter>
116 <parameter name="CallerIDnum">
117 <para>The Caller ID number.</para>
118 </parameter>
119 <parameter name="CallerIDname">
120 <para>The Caller ID name.</para>
121 </parameter>
122 <parameter name="CallerIDani">
123 <para>The Caller ID Automatic Number Identification.</para>
124 </parameter>
125 <parameter name="CallerIDrdnis">
126 <para>The Caller ID Redirected Dialed Number Identification Service.</para>
127 </parameter>
128 <parameter name="CallerIDdnid">
129 <para>The Caller ID Dialed Number Identifier.</para>
130 </parameter>
131 <parameter name="Exten">
132 <para>The dialplan extension the channel is currently executing in.</para>
133 </parameter>
134 <parameter name="Context">
135 <para>The dialplan context the channel is currently executing in.</para>
136 </parameter>
137 <parameter name="Application">
138 <para>The dialplan application the channel is currently executing.</para>
139 </parameter>
140 <parameter name="AppData">
141 <para>The arguments passed to the dialplan <replaceable>Application</replaceable>.</para>
142 </parameter>
143 <parameter name="EventTime">
144 <para>The time the CEL event occurred.</para>
145 </parameter>
146 <parameter name="AMAFlags">
147 <para>A flag that informs a billing system how to treat the CEL.</para>
148 <enumlist>
149 <enum name="OMIT">
150 <para>This event should be ignored.</para>
151 </enum>
152 <enum name="BILLING">
153 <para>This event contains valid billing data.</para>
154 </enum>
155 <enum name="DOCUMENTATION">
156 <para>This event is for documentation purposes.</para>
157 </enum>
158 </enumlist>
159 </parameter>
160 <parameter name="UniqueID">
161 <para>The unique ID of the channel.</para>
162 </parameter>
163 <parameter name="LinkedID">
164 <para>The linked ID of the channel, which ties this event to other related channel's events.</para>
165 </parameter>
166 <parameter name="UserField">
167 <para>
168 A user defined field set on a channel, containing arbitrary
169 application specific data.
170 </para>
171 </parameter>
172 <parameter name="Peer">
173 <para>
174 If this channel is in a bridge, the channel that it is in
175 a bridge with.
176 </para>
177 </parameter>
178 <parameter name="PeerAccount">
179 <para>
180 If this channel is in a bridge, the accountcode of the
181 channel it is in a bridge with.
182 </para>
183 </parameter>
184 <parameter name="Extra">
185 <para>
186 Some events will have event specific data that accompanies the CEL record.
187 This extra data is JSON encoded, and is dependent on the event in
188 question.
189 </para>
190 </parameter>
191 </syntax>
192 </managerEventInstance>
193 </managerEvent>
194 ***/
195
196#include "asterisk.h"
197
198#include "asterisk/channel.h"
199#include "asterisk/cel.h"
200#include "asterisk/module.h"
201#include "asterisk/logger.h"
202#include "asterisk/utils.h"
203#include "asterisk/manager.h"
204#include "asterisk/config.h"
205
206static const char DATE_FORMAT[] = "%Y-%m-%d %T";
207
208static const char CONF_FILE[] = "cel.conf";
209
210/*! \brief AMI CEL is off by default */
211#define CEL_AMI_ENABLED_DEFAULT 0
212
213static int enablecel;
214
215/*! \brief show_user_def is off by default */
216#define CEL_SHOW_USERDEF_DEFAULT 0
217
218#define MANAGER_BACKEND_NAME "Manager Event Logging"
219
220/*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
221static unsigned char cel_show_user_def;
222
223static void manager_log(struct ast_event *event)
224{
225 struct ast_tm timeresult;
226 char start_time[80] = "";
227 char user_defined_header[160];
228 const char *event_name;
229 struct ast_cel_event_record record = {
231 };
232 RAII_VAR(char *, tenant_id, NULL, ast_free);
233
234 if (!enablecel) {
235 return;
236 }
237
238 if (ast_cel_fill_record(event, &record)) {
239 return;
240 }
241
242 ast_localtime(&record.event_time, &timeresult, NULL);
243 ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
244
245 event_name = record.event_name;
246 user_defined_header[0] = '\0';
247 if (record.event_type == AST_CEL_USER_DEFINED) {
248 if (cel_show_user_def) {
249 snprintf(user_defined_header, sizeof(user_defined_header),
250 "UserDefType: %s\r\n", record.user_defined_name);
251 } else {
253 }
254 }
255
256 if (!ast_strlen_zero(record.tenant_id)) {
257 ast_asprintf(&tenant_id, "TenantID: %s\r\n", record.tenant_id);
258 }
259
261 "EventName: %s\r\n"
262 "AccountCode: %s\r\n"
263 "CallerIDnum: %s\r\n"
264 "CallerIDname: %s\r\n"
265 "CallerIDani: %s\r\n"
266 "CallerIDrdnis: %s\r\n"
267 "CallerIDdnid: %s\r\n"
268 "Exten: %s\r\n"
269 "Context: %s\r\n"
270 "Channel: %s\r\n"
271 "Application: %s\r\n"
272 "AppData: %s\r\n"
273 "EventTime: %s\r\n"
274 "AMAFlags: %s\r\n"
275 "UniqueID: %s\r\n"
276 "LinkedID: %s\r\n"
277 "%s"
278 "Userfield: %s\r\n"
279 "Peer: %s\r\n"
280 "PeerAccount: %s\r\n"
281 "%s"
282 "Extra: %s\r\n",
284 record.account_code,
285 record.caller_id_num,
286 record.caller_id_name,
287 record.caller_id_ani,
288 record.caller_id_rdnis,
289 record.caller_id_dnid,
290 record.extension,
291 record.context,
292 record.channel_name,
293 record.application_name,
294 record.application_data,
295 start_time,
297 record.unique_id,
298 record.linked_id,
300 record.user_field,
301 record.peer,
302 record.peer_account,
303 user_defined_header,
304 record.extra);
305}
306
307static int load_config(int reload)
308{
309 const char *cat = NULL;
310 struct ast_config *cfg;
311 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
312 struct ast_variable *v;
313 int newenablecel = CEL_AMI_ENABLED_DEFAULT;
314 int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
315
316 cfg = ast_config_load(CONF_FILE, config_flags);
317 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
318 return 0;
319 }
320
321 if (cfg == CONFIG_STATUS_FILEINVALID) {
322 ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
323 CONF_FILE);
324 enablecel = 0;
325 return -1;
326 } else if (!cfg) {
327 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
328 enablecel = 0;
329 return -1;
330 }
331
332 while ((cat = ast_category_browse(cfg, cat))) {
333 if (strcasecmp(cat, "manager")) {
334 continue;
335 }
336
337 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
338 if (!strcasecmp(v->name, "enabled")) {
339 newenablecel = ast_true(v->value) ? 1 : 0;
340 } else if (!strcasecmp(v->name, "show_user_defined")) {
341 new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
342 } else {
343 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
344 "for cel_manager.\n", v->name);
345 }
346 }
347 }
348
350
351 cel_show_user_def = new_cel_show_user_def;
352 if (enablecel && !newenablecel) {
354 } else if (!enablecel && newenablecel) {
356 ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
357 }
358 }
359 enablecel = newenablecel;
360
361 return 0;
362}
363
364static int unload_module(void)
365{
367 return 0;
368}
369
370static int load_module(void)
371{
372 if (load_config(0)) {
374 }
375
377}
378
379static int reload(void)
380{
381 return load_config(1);
382}
383
384AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
385 .support_level = AST_MODULE_SUPPORT_CORE,
386 .load = load_module,
387 .unload = unload_module,
388 .reload = reload,
389 .load_pri = AST_MODPRI_CDR_DRIVER,
390 .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:1771
@ 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:822
#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:1783
#define CEL_SHOW_USERDEF_DEFAULT
show_user_def is off by default
Definition: cel_manager.c:216
static const char DATE_FORMAT[]
Definition: cel_manager.c:206
static void manager_log(struct ast_event *event)
Definition: cel_manager.c:223
#define CEL_AMI_ENABLED_DEFAULT
AMI CEL is off by default.
Definition: cel_manager.c:211
static unsigned char cel_show_user_def
Definition: cel_manager.c:221
static int enablecel
Definition: cel_manager.c:213
static int load_module(void)
Definition: cel_manager.c:370
static int unload_module(void)
Definition: cel_manager.c:364
static int load_config(int reload)
Definition: cel_manager.c:307
static int reload(void)
Definition: cel_manager.c:379
#define MANAGER_BACKEND_NAME
Definition: cel_manager.c:218
static const char CONF_FILE[]
Definition: cel_manager.c:208
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:4392
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
#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
@ CONFIG_FLAG_FILEUNCHANGED
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:253
#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