cel_manager.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008 - 2009, Digium, Inc.
00005  *
00006  * Steve Murphy <murf@digium.com>
00007  * who freely borrowed code from the cdr equivalents
00008  *     (see cdr/cdr_manager.c)
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief Asterisk Channel Event records.
00024  *
00025  * See also
00026  * \arg \ref AstCDR
00027  * \arg \ref AstAMI
00028  * \arg \ref Config_ami
00029  * \ingroup cel_drivers
00030  */
00031 
00032 /*** MODULEINFO
00033    <support_level>core</support_level>
00034  ***/
00035 
00036 /*** DOCUMENTATION
00037    <managerEvent language="en_US" name="CEL">
00038       <managerEventInstance class="EVENT_FLAG_CEL">
00039          <synopsis>Raised when a Channel Event Log is generated for a channel.</synopsis>
00040          <syntax>
00041             <parameter name="EventName">
00042                <para>
00043                   The name of the CEL event being raised. This can include
00044                   both the system defined CEL events, as well as user defined
00045                   events.
00046                </para>
00047                <note>
00048                   <para>All events listed here may not be raised, depending
00049                   on the configuration in <filename>cel.conf</filename>.</para>
00050                </note>
00051                <enumlist>
00052                   <enum name="CHAN_START">
00053                      <para>A channel was created.</para>
00054                   </enum>
00055                   <enum name="CHAN_END">
00056                      <para>A channel was terminated.</para>
00057                   </enum>
00058                   <enum name="ANSWER">
00059                      <para>A channel answered.</para>
00060                   </enum>
00061                   <enum name="HANGUP">
00062                      <para>A channel was hung up.</para>
00063                   </enum>
00064                   <enum name="BRIDGE_ENTER">
00065                      <para>A channel entered a bridge.</para>
00066                   </enum>
00067                   <enum name="BRIDGE_EXIT">
00068                      <para>A channel left a bridge.</para>
00069                   </enum>
00070                   <enum name="APP_START">
00071                      <para>A channel entered into a tracked application.</para>
00072                   </enum>
00073                   <enum name="APP_END">
00074                      <para>A channel left a tracked application.</para>
00075                   </enum>
00076                   <enum name="PARK_START">
00077                      <para>A channel was parked.</para>
00078                   </enum>
00079                   <enum name="PARK_END">
00080                      <para>A channel was unparked.</para>
00081                   </enum>
00082                   <enum name="BLINDTRANSFER">
00083                      <para>A channel initiated a blind transfer.</para>
00084                   </enum>
00085                   <enum name="ATTENDEDTRANSFER">
00086                      <para>A channel initiated an attended transfer.</para>
00087                   </enum>
00088                   <enum name="PICKUP">
00089                      <para>A channel initated a call pickup.</para>
00090                   </enum>
00091                   <enum name="FORWARD">
00092                      <para>A channel is being forwarded to another destination.</para>
00093                   </enum>
00094                   <enum name="LINKEDID_END">
00095                      <para>The linked ID associated with this channel is being retired.</para>
00096                   </enum>
00097                   <enum name="LOCAL_OPTIMIZE">
00098                      <para>A Local channel optimization has occurred.</para>
00099                   </enum>
00100                   <enum name="USER_DEFINED">
00101                      <para>A user defined type.</para>
00102                      <note>
00103                         <para>
00104                            This event is only present if <literal>show_user_defined</literal>
00105                            in <filename>cel.conf</filename> is <literal>True</literal>. Otherwise,
00106                            the user defined event will be placed directly in the
00107                            <replaceable>EventName</replaceable> field.
00108                         </para>
00109                      </note>
00110                   </enum>
00111                </enumlist>
00112             </parameter>
00113             <parameter name="AccountCode">
00114                <para>The channel's account code.</para>
00115             </parameter>
00116             <parameter name="CallerIDnum">
00117                <para>The Caller ID number.</para>
00118             </parameter>
00119             <parameter name="CallerIDname">
00120                <para>The Caller ID name.</para>
00121             </parameter>
00122             <parameter name="CallerIDani">
00123                <para>The Caller ID Automatic Number Identification.</para>
00124             </parameter>
00125             <parameter name="CallerIDrdnis">
00126                <para>The Caller ID Redirected Dialed Number Identification Service.</para>
00127             </parameter>
00128             <parameter name="CallerIDdnid">
00129                <para>The Caller ID Dialed Number Identifier.</para>
00130             </parameter>
00131             <parameter name="Exten">
00132                <para>The dialplan extension the channel is currently executing in.</para>
00133             </parameter>
00134             <parameter name="Context">
00135                <para>The dialplan context the channel is currently executing in.</para>
00136             </parameter>
00137             <parameter name="Application">
00138                <para>The dialplan application the channel is currently executing.</para>
00139             </parameter>
00140             <parameter name="AppData">
00141                <para>The arguments passed to the dialplan <replaceable>Application</replaceable>.</para>
00142             </parameter>
00143             <parameter name="EventTime">
00144                <para>The time the CEL event occurred.</para>
00145             </parameter>
00146             <parameter name="AMAFlags">
00147                <para>A flag that informs a billing system how to treat the CEL.</para>
00148                <enumlist>
00149                   <enum name="OMIT">
00150                      <para>This event should be ignored.</para>
00151                   </enum>
00152                   <enum name="BILLING">
00153                      <para>This event contains valid billing data.</para>
00154                   </enum>
00155                   <enum name="DOCUMENTATION">
00156                      <para>This event is for documentation purposes.</para>
00157                   </enum>
00158                </enumlist>
00159             </parameter>
00160             <parameter name="UniqueID">
00161                <para>The unique ID of the channel.</para>
00162             </parameter>
00163             <parameter name="LinkedID">
00164                <para>The linked ID of the channel, which ties this event to other related channel's events.</para>
00165             </parameter>
00166             <parameter name="UserField">
00167                <para>
00168                   A user defined field set on a channel, containing arbitrary
00169                   application specific data.
00170                </para>
00171             </parameter>
00172             <parameter name="Peer">
00173                <para>
00174                   If this channel is in a bridge, the channel that it is in
00175                   a bridge with.
00176                </para>
00177             </parameter>
00178             <parameter name="PeerAccount">
00179                <para>
00180                   If this channel is in a bridge, the accountcode of the
00181                   channel it is in a bridge with.
00182                </para>
00183             </parameter>
00184             <parameter name="Extra">
00185                <para>
00186                   Some events will have event specific data that accompanies the CEL record.
00187                   This extra data is JSON encoded, and is dependent on the event in
00188                   question.
00189                </para>
00190             </parameter>
00191          </syntax>
00192       </managerEventInstance>
00193    </managerEvent>
00194  ***/
00195 
00196 #include "asterisk.h"
00197 
00198 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 430863 $")
00199 
00200 #include "asterisk/channel.h"
00201 #include "asterisk/cel.h"
00202 #include "asterisk/module.h"
00203 #include "asterisk/logger.h"
00204 #include "asterisk/utils.h"
00205 #include "asterisk/manager.h"
00206 #include "asterisk/config.h"
00207 
00208 static const char DATE_FORMAT[] = "%Y-%m-%d %T";
00209 
00210 static const char CONF_FILE[] = "cel.conf";
00211 
00212 /*! \brief AMI CEL is off by default */
00213 #define CEL_AMI_ENABLED_DEFAULT     0
00214 
00215 static int enablecel;
00216 
00217 /*! \brief show_user_def is off by default */
00218 #define CEL_SHOW_USERDEF_DEFAULT 0
00219 
00220 #define MANAGER_BACKEND_NAME "Manager Event Logging"
00221 
00222 /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
00223 static unsigned char cel_show_user_def;
00224 
00225 static void manager_log(struct ast_event *event)
00226 {
00227    struct ast_tm timeresult;
00228    char start_time[80] = "";
00229    char user_defined_header[160];
00230    const char *event_name;
00231    struct ast_cel_event_record record = {
00232       .version = AST_CEL_EVENT_RECORD_VERSION,
00233    };
00234 
00235    if (!enablecel) {
00236       return;
00237    }
00238 
00239    if (ast_cel_fill_record(event, &record)) {
00240       return;
00241    }
00242 
00243    ast_localtime(&record.event_time, &timeresult, NULL);
00244    ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
00245 
00246    event_name = record.event_name;
00247    user_defined_header[0] = '\0';
00248    if (record.event_type == AST_CEL_USER_DEFINED) {
00249       if (cel_show_user_def) {
00250          snprintf(user_defined_header, sizeof(user_defined_header),
00251             "UserDefType: %s\r\n", record.user_defined_name);
00252       } else {
00253          event_name = record.user_defined_name;
00254       }
00255    }
00256 
00257    manager_event(EVENT_FLAG_CALL, "CEL",
00258       "EventName: %s\r\n"
00259       "AccountCode: %s\r\n"
00260       "CallerIDnum: %s\r\n"
00261       "CallerIDname: %s\r\n"
00262       "CallerIDani: %s\r\n"
00263       "CallerIDrdnis: %s\r\n"
00264       "CallerIDdnid: %s\r\n"
00265       "Exten: %s\r\n"
00266       "Context: %s\r\n"
00267       "Channel: %s\r\n"
00268       "Application: %s\r\n"
00269       "AppData: %s\r\n"
00270       "EventTime: %s\r\n"
00271       "AMAFlags: %s\r\n"
00272       "UniqueID: %s\r\n"
00273       "LinkedID: %s\r\n"
00274       "Userfield: %s\r\n"
00275       "Peer: %s\r\n"
00276       "PeerAccount: %s\r\n"
00277       "%s"
00278       "Extra: %s\r\n",
00279       event_name,
00280       record.account_code,
00281       record.caller_id_num,
00282       record.caller_id_name,
00283       record.caller_id_ani,
00284       record.caller_id_rdnis,
00285       record.caller_id_dnid,
00286       record.extension,
00287       record.context,
00288       record.channel_name,
00289       record.application_name,
00290       record.application_data,
00291       start_time,
00292       ast_channel_amaflags2string(record.amaflag),
00293       record.unique_id,
00294       record.linked_id,
00295       record.user_field,
00296       record.peer,
00297       record.peer_account,
00298       user_defined_header,
00299       record.extra);
00300 }
00301 
00302 static int load_config(int reload)
00303 {
00304    const char *cat = NULL;
00305    struct ast_config *cfg;
00306    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00307    struct ast_variable *v;
00308    int newenablecel = CEL_AMI_ENABLED_DEFAULT;
00309    int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
00310 
00311    cfg = ast_config_load(CONF_FILE, config_flags);
00312    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00313       return 0;
00314    }
00315 
00316    if (cfg == CONFIG_STATUS_FILEINVALID) {
00317       ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
00318          CONF_FILE);
00319       enablecel = 0;
00320       return -1;
00321    } else if (!cfg) {
00322       ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
00323       enablecel = 0;
00324       return -1;
00325    }
00326 
00327    while ((cat = ast_category_browse(cfg, cat))) {
00328       if (strcasecmp(cat, "manager")) {
00329          continue;
00330       }
00331 
00332       for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
00333          if (!strcasecmp(v->name, "enabled")) {
00334             newenablecel = ast_true(v->value) ? 1 : 0;
00335          } else if (!strcasecmp(v->name, "show_user_defined")) {
00336             new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
00337          } else {
00338             ast_log(LOG_NOTICE, "Unknown option '%s' specified "
00339                   "for cel_manager.\n", v->name);
00340          }
00341       }
00342    }
00343 
00344    ast_config_destroy(cfg);
00345 
00346    cel_show_user_def = new_cel_show_user_def;
00347    if (enablecel && !newenablecel) {
00348       ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
00349    } else if (!enablecel && newenablecel) {
00350       if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
00351          ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
00352       }
00353    }
00354    enablecel = newenablecel;
00355 
00356    return 0;
00357 }
00358 
00359 static int unload_module(void)
00360 {
00361    ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
00362    return 0;
00363 }
00364 
00365 static int load_module(void)
00366 {
00367    if (load_config(0)) {
00368       return AST_MODULE_LOAD_DECLINE;
00369    }
00370 
00371    return AST_MODULE_LOAD_SUCCESS;
00372 }
00373 
00374 static int reload(void)
00375 {
00376    return load_config(1);
00377 }
00378 
00379 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
00380    .support_level = AST_MODULE_SUPPORT_CORE,
00381    .load = load_module,
00382    .unload = unload_module,
00383    .reload = reload,
00384    .load_pri = AST_MODPRI_CDR_DRIVER,
00385 );

Generated on Thu Apr 16 06:27:18 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6