| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (C) 2008 - 2009, Digium, Inc. |
| 5 | * |
| 6 | * Steve Murphy <[email protected]> |
| 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_CALL"> |
| 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_BEGIN"> |
| 101 | <para>A Local channel optimization has begun.</para> |
| 102 | </enum> |
| 103 | <enum name="LOCAL_OPTIMIZE"> |
| 104 | <para>A Local channel optimization has occurred.</para> |
| 105 | </enum> |
| 106 | <enum name="STREAM_BEGIN"> |
| 107 | <para>A stream started playing: it can be a standalone sound |
| 108 | file playing back, or a music-on-hold class started.</para> |
| 109 | </enum> |
| 110 | <enum name="STREAM_END"> |
| 111 | <para>A playing stream ended.</para> |
| 112 | </enum> |
| 113 | <enum name="DTMF"> |
| 114 | <para>A DTMF digit was processed: these events are dispatched |
| 115 | at the end, when the button is released and the duration is |
| 116 | present in the extra field</para> |
| 117 | </enum> |
| 118 | <enum name="USER_DEFINED"> |
| 119 | <para>A user defined type.</para> |
| 120 | <note> |
| 121 | <para> |
| 122 | This event is only present if <literal>show_user_defined</literal> |
| 123 | in <filename>cel.conf</filename> is <literal>True</literal>. Otherwise, |
| 124 | the user defined event will be placed directly in the |
| 125 | <replaceable>EventName</replaceable> field. |
| 126 | </para> |
| 127 | </note> |
| 128 | </enum> |
| 129 | </enumlist> |
| 130 | </parameter> |
| 131 | <parameter name="AccountCode"> |
| 132 | <para>The channel's account code.</para> |
| 133 | </parameter> |
| 134 | <parameter name="CallerIDnum"> |
| 135 | <para>The Caller ID number.</para> |
| 136 | </parameter> |
| 137 | <parameter name="CallerIDname"> |
| 138 | <para>The Caller ID name.</para> |
| 139 | </parameter> |
| 140 | <parameter name="CallerIDani"> |
| 141 | <para>The Caller ID Automatic Number Identification.</para> |
| 142 | </parameter> |
| 143 | <parameter name="CallerIDrdnis"> |
| 144 | <para>The Caller ID Redirected Dialed Number Identification Service.</para> |
| 145 | </parameter> |
| 146 | <parameter name="CallerIDdnid"> |
| 147 | <para>The Caller ID Dialed Number Identifier.</para> |
| 148 | </parameter> |
| 149 | <parameter name="Exten"> |
| 150 | <para>The dialplan extension the channel is currently executing in.</para> |
| 151 | </parameter> |
| 152 | <parameter name="Context"> |
| 153 | <para>The dialplan context the channel is currently executing in.</para> |
| 154 | </parameter> |
| 155 | <parameter name="Application"> |
| 156 | <para>The dialplan application the channel is currently executing.</para> |
| 157 | </parameter> |
| 158 | <parameter name="AppData"> |
| 159 | <para>The arguments passed to the dialplan <replaceable>Application</replaceable>.</para> |
| 160 | </parameter> |
| 161 | <parameter name="EventTime"> |
| 162 | <para>The time the CEL event occurred.</para> |
| 163 | </parameter> |
| 164 | <parameter name="AMAFlags"> |
| 165 | <para>A flag that informs a billing system how to treat the CEL.</para> |
| 166 | <enumlist> |
| 167 | <enum name="OMIT"> |
| 168 | <para>This event should be ignored.</para> |
| 169 | </enum> |
| 170 | <enum name="BILLING"> |
| 171 | <para>This event contains valid billing data.</para> |
| 172 | </enum> |
| 173 | <enum name="DOCUMENTATION"> |
| 174 | <para>This event is for documentation purposes.</para> |
| 175 | </enum> |
| 176 | </enumlist> |
| 177 | </parameter> |
| 178 | <parameter name="UniqueID"> |
| 179 | <para>The unique ID of the channel.</para> |
| 180 | </parameter> |
| 181 | <parameter name="LinkedID"> |
| 182 | <para>The linked ID of the channel, which ties this event to other related channel's events.</para> |
| 183 | </parameter> |
| 184 | <parameter name="UserField"> |
| 185 | <para> |
| 186 | A user defined field set on a channel, containing arbitrary |
| 187 | application specific data. |
| 188 | </para> |
| 189 | </parameter> |
| 190 | <parameter name="Peer"> |
| 191 | <para> |
| 192 | If this channel is in a bridge, the channel that it is in |
| 193 | a bridge with. |
| 194 | </para> |
| 195 | </parameter> |
| 196 | <parameter name="PeerAccount"> |
| 197 | <para> |
| 198 | If this channel is in a bridge, the accountcode of the |
| 199 | channel it is in a bridge with. |
| 200 | </para> |
| 201 | </parameter> |
| 202 | <parameter name="Extra"> |
| 203 | <para> |
| 204 | Some events will have event specific data that accompanies the CEL record. |
| 205 | This extra data is JSON encoded, and is dependent on the event in |
| 206 | question. |
| 207 | </para> |
| 208 | </parameter> |
| 209 | </syntax> |
| 210 | </managerEventInstance> |
| 211 | </managerEvent> |
| 212 | ***/ |
| 213 | |
| 214 | #include "asterisk.h" |
| 215 | |
| 216 | #include "asterisk/channel.h" |
| 217 | #include "asterisk/cel.h" |
| 218 | #include "asterisk/module.h" |
| 219 | #include "asterisk/logger.h" |
| 220 | #include "asterisk/utils.h" |
| 221 | #include "asterisk/manager.h" |
| 222 | #include "asterisk/config.h" |
| 223 | |
| 224 | static const char DATE_FORMAT[] = "%Y-%m-%d %T"; |
| 225 | |
| 226 | static const char CONF_FILE[] = "cel.conf"; |
| 227 | |
| 228 | /*! \brief AMI CEL is off by default */ |
| 229 | #define CEL_AMI_ENABLED_DEFAULT 0 |
| 230 | |
| 231 | static int enablecel; |
| 232 | |
| 233 | /*! \brief show_user_def is off by default */ |
| 234 | #define CEL_SHOW_USERDEF_DEFAULT 0 |
| 235 | |
| 236 | #define MANAGER_BACKEND_NAME "Manager Event Logging" |
| 237 | |
| 238 | /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */ |
| 239 | static unsigned char cel_show_user_def; |
| 240 | |
| 241 | static void manager_log(struct ast_event *event) |
| 242 | { |
| 243 | struct ast_tm timeresult; |
| 244 | char start_time[80] = ""; |
| 245 | char user_defined_header[160]; |
| 246 | const char *event_name; |
| 247 | struct ast_cel_event_record record = { |
| 248 | .version = AST_CEL_EVENT_RECORD_VERSION, |
| 249 | }; |
| 250 | RAII_VAR(char *, tenant_id, NULL, ast_free); |
| 251 | |
| 252 | if (!enablecel) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if (ast_cel_fill_record(event, &record)) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | ast_localtime(&record.event_time, &timeresult, NULL); |
| 261 | ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult); |
| 262 | |
| 263 | event_name = record.event_name; |
| 264 | user_defined_header[0] = '\0'; |
| 265 | if (record.event_type == AST_CEL_USER_DEFINED) { |
| 266 | if (cel_show_user_def) { |
| 267 | snprintf(user_defined_header, sizeof(user_defined_header), |
| 268 | "UserDefType: %s\r\n", record.user_defined_name); |
| 269 | } else { |
| 270 | event_name = record.user_defined_name; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (!ast_strlen_zero(record.tenant_id)) { |
| 275 | ast_asprintf(&tenant_id, "TenantID: %s\r\n", record.tenant_id); |
| 276 | } |
| 277 | |
| 278 | manager_event(EVENT_FLAG_CALL, "CEL", |
| 279 | "EventName: %s\r\n" |
| 280 | "AccountCode: %s\r\n" |
| 281 | "CallerIDnum: %s\r\n" |
| 282 | "CallerIDname: %s\r\n" |
| 283 | "CallerIDani: %s\r\n" |
| 284 | "CallerIDrdnis: %s\r\n" |
| 285 | "CallerIDdnid: %s\r\n" |
| 286 | "Exten: %s\r\n" |
| 287 | "Context: %s\r\n" |
| 288 | "Channel: %s\r\n" |
| 289 | "Application: %s\r\n" |
| 290 | "AppData: %s\r\n" |
| 291 | "EventTime: %s\r\n" |
| 292 | "AMAFlags: %s\r\n" |
| 293 | "UniqueID: %s\r\n" |
| 294 | "LinkedID: %s\r\n" |
| 295 | "%s" |
| 296 | "Userfield: %s\r\n" |
| 297 | "Peer: %s\r\n" |
| 298 | "PeerAccount: %s\r\n" |
| 299 | "%s" |
| 300 | "Extra: %s\r\n", |
| 301 | event_name, |
| 302 | record.account_code, |
| 303 | record.caller_id_num, |
| 304 | record.caller_id_name, |
| 305 | record.caller_id_ani, |
| 306 | record.caller_id_rdnis, |
| 307 | record.caller_id_dnid, |
| 308 | record.extension, |
| 309 | record.context, |
| 310 | record.channel_name, |
| 311 | record.application_name, |
| 312 | record.application_data, |
| 313 | start_time, |
| 314 | ast_channel_amaflags2string(record.amaflag), |
| 315 | record.unique_id, |
| 316 | record.linked_id, |
| 317 | !ast_strlen_zero(tenant_id) ? tenant_id : "", |
| 318 | record.user_field, |
| 319 | record.peer, |
| 320 | record.peer_account, |
| 321 | user_defined_header, |
| 322 | record.extra); |
| 323 | } |
| 324 | |
| 325 | static int load_config(int reload) |
| 326 | { |
| 327 | const char *cat = NULL; |
| 328 | struct ast_config *cfg; |
| 329 | struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 330 | struct ast_variable *v; |
| 331 | int newenablecel = CEL_AMI_ENABLED_DEFAULT; |
| 332 | int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT; |
| 333 | |
| 334 | cfg = ast_config_load(CONF_FILE, config_flags); |
| 335 | if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 340 | ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n", |
| 341 | CONF_FILE); |
| 342 | enablecel = 0; |
| 343 | return -1; |
| 344 | } else if (!cfg) { |
| 345 | ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n"); |
| 346 | enablecel = 0; |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | while ((cat = ast_category_browse(cfg, cat))) { |
| 351 | if (strcasecmp(cat, "manager")) { |
| 352 | continue; |
| 353 | } |
| 354 | |
| 355 | for (v = ast_variable_browse(cfg, cat); v; v = v->next) { |
| 356 | if (!strcasecmp(v->name, "enabled")) { |
| 357 | newenablecel = ast_true(v->value) ? 1 : 0; |
| 358 | } else if (!strcasecmp(v->name, "show_user_defined")) { |
| 359 | new_cel_show_user_def = ast_true(v->value) ? 1 : 0; |
| 360 | } else { |
| 361 | ast_log(LOG_NOTICE, "Unknown option '%s' specified " |
| 362 | "for cel_manager.\n", v->name); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | ast_config_destroy(cfg); |
| 368 | |
| 369 | cel_show_user_def = new_cel_show_user_def; |
| 370 | if (enablecel && !newenablecel) { |
| 371 | ast_cel_backend_unregister(MANAGER_BACKEND_NAME); |
| 372 | } else if (!enablecel && newenablecel) { |
| 373 | if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) { |
| 374 | ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n"); |
| 375 | } |
| 376 | } |
| 377 | enablecel = newenablecel; |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int unload_module(void) |
| 383 | { |
| 384 | ast_cel_backend_unregister(MANAGER_BACKEND_NAME); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int load_module(void) |
| 389 | { |
| 390 | if (load_config(0)) { |
| 391 | return AST_MODULE_LOAD_DECLINE; |
| 392 | } |
| 393 | |
| 394 | return AST_MODULE_LOAD_SUCCESS; |
| 395 | } |
| 396 | |
| 397 | static int reload(void) |
| 398 | { |
| 399 | return load_config(1); |
| 400 | } |
| 401 | |
| 402 | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend", |
| 403 | .support_level = AST_MODULE_SUPPORT_CORE, |
| 404 | .load = load_module, |
| 405 | .unload = unload_module, |
| 406 | .reload = reload, |
| 407 | .load_pri = AST_MODPRI_CDR_DRIVER, |
| 408 | .requires = "cel", |
| 409 | ); |