cdr_manager.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2004 - 2005
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*!
00018  * \file
00019  * \brief Asterisk Call Manager CDR records.
00020  *
00021  * See also
00022  * \arg \ref AstCDR
00023  * \arg \ref AstAMI
00024  * \arg \ref Config_ami
00025  * \ingroup cdr_drivers
00026  */
00027 
00028 /*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
00029  * \addtogroup configuration_file Configuration Files
00030  */
00031 
00032 /*!
00033  * \page cdr_manager.conf cdr_manager.conf
00034  * \verbinclude cdr_manager.conf.sample
00035  */
00036 
00037 /*** MODULEINFO
00038    <support_level>core</support_level>
00039  ***/
00040 
00041 /*** DOCUMENTATION
00042    <managerEvent language="en_US" name="Cdr">
00043       <managerEventInstance class="EVENT_FLAG_CDR">
00044          <synopsis>Raised when a CDR is generated.</synopsis>
00045          <syntax>
00046             <parameter name="AccountCode">
00047                <para>The account code of the Party A channel.</para>
00048             </parameter>
00049             <parameter name="Source">
00050                <para>The Caller ID number associated with the Party A in the CDR.</para>
00051             </parameter>
00052             <parameter name="Destination">
00053                <para>The dialplan extension the Party A was executing.</para>
00054             </parameter>
00055             <parameter name="DestinationContext">
00056                <para>The dialplan context the Party A was executing.</para>
00057             </parameter>
00058             <parameter name="CallerID">
00059                <para>The Caller ID name associated with the Party A in the CDR.</para>
00060             </parameter>
00061             <parameter name="Channel">
00062                <para>The channel name of the Party A.</para>
00063             </parameter>
00064             <parameter name="DestinationChannel">
00065                <para>The channel name of the Party B.</para>
00066             </parameter>
00067             <parameter name="LastApplication">
00068                <para>The last dialplan application the Party A executed.</para>
00069             </parameter>
00070             <parameter name="LastData">
00071                <para>
00072                   The parameters passed to the last dialplan application the
00073                   Party A executed.
00074                </para>
00075             </parameter>
00076             <parameter name="StartTime">
00077                <para>The time the CDR was created.</para>
00078             </parameter>
00079             <parameter name="AnswerTime">
00080                <para>
00081                   The earliest of either the time when Party A answered, or
00082                   the start time of this CDR.
00083                </para>
00084             </parameter>
00085             <parameter name="EndTime">
00086                <para>
00087                   The time when the CDR was finished. This occurs when the
00088                   Party A hangs up or when the bridge between Party A and
00089                   Party B is broken.
00090                </para>
00091             </parameter>
00092             <parameter name="Duration">
00093                <para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
00094             </parameter>
00095             <parameter name="BillableSeconds">
00096                <para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
00097             </parameter>
00098             <parameter name="Disposition">
00099                <para>The final known disposition of the CDR.</para>
00100                <enumlist>
00101                   <enum name="NO ANSWER">
00102                      <para>The channel was not answered. This is the default disposition.</para>
00103                   </enum>
00104                   <enum name="FAILED">
00105                      <para>The channel attempted to dial but the call failed.</para>
00106                      <note>
00107                         <para>The congestion setting in <filename>cdr.conf</filename> can result
00108                         in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
00109                         <literal>CONGESTION</literal> dial status to map to this disposition.
00110                         </para>
00111                      </note>
00112                   </enum>
00113                   <enum name="BUSY">
00114                      <para>The channel attempted to dial but the remote party was busy.</para>
00115                   </enum>
00116                   <enum name="ANSWERED">
00117                      <para>The channel was answered. The hang up cause will no longer
00118                      impact the disposition of the CDR.</para>
00119                   </enum>
00120                   <enum name="CONGESTION">
00121                      <para>The channel attempted to dial but the remote party was congested.</para>
00122                   </enum>
00123                </enumlist>
00124             </parameter>
00125             <parameter name="AMAFlags">
00126                <para>A flag that informs a billing system how to treat the CDR.</para>
00127                <enumlist>
00128                   <enum name="OMIT">
00129                      <para>This CDR should be ignored.</para>
00130                   </enum>
00131                   <enum name="BILLING">
00132                      <para>This CDR contains valid billing data.</para>
00133                   </enum>
00134                   <enum name="DOCUMENTATION">
00135                      <para>This CDR is for documentation purposes.</para>
00136                   </enum>
00137                </enumlist>
00138             </parameter>
00139             <parameter name="UniqueID">
00140                <para>A unique identifier for the Party A channel.</para>
00141             </parameter>
00142             <parameter name="UserField">
00143                <para>
00144                   A user defined field set on the channels. If set on both the Party A
00145                   and Party B channel, the userfields of both are concatenated and
00146                   separated by a <literal>;</literal>.
00147                </para>
00148             </parameter>
00149          </syntax>
00150          <description>
00151             <para>
00152                The <replaceable>Cdr</replaceable> event is only raised when the
00153                <filename>cdr_manager</filename> backend is loaded and registered with
00154                the CDR engine.
00155             </para>
00156             <note>
00157                <para>
00158                   This event can contain additional fields depending on the configuration
00159                   provided by <filename>cdr_manager.conf</filename>.
00160                </para>
00161             </note>
00162          </description>
00163       </managerEventInstance>
00164    </managerEvent>
00165  ***/
00166 
00167 #include "asterisk.h"
00168 
00169 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 430863 $")
00170 
00171 #include <time.h>
00172 
00173 #include "asterisk/channel.h"
00174 #include "asterisk/cdr.h"
00175 #include "asterisk/module.h"
00176 #include "asterisk/utils.h"
00177 #include "asterisk/manager.h"
00178 #include "asterisk/config.h"
00179 #include "asterisk/pbx.h"
00180 
00181 #define DATE_FORMAT  "%Y-%m-%d %T"
00182 #define CONF_FILE "cdr_manager.conf"
00183 #define CUSTOM_FIELDS_BUF_SIZE 1024
00184 
00185 static const char name[] = "cdr_manager";
00186 
00187 static int enablecdr = 0;
00188 
00189 static struct ast_str *customfields;
00190 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
00191 
00192 static int manager_log(struct ast_cdr *cdr);
00193 
00194 static int load_config(int reload)
00195 {
00196    char *cat = NULL;
00197    struct ast_config *cfg;
00198    struct ast_variable *v;
00199    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00200    int newenablecdr = 0;
00201 
00202    cfg = ast_config_load(CONF_FILE, config_flags);
00203    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00204       return 0;
00205    }
00206 
00207    if (cfg == CONFIG_STATUS_FILEINVALID) {
00208       ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
00209       return -1;
00210    }
00211 
00212    if (!cfg) {
00213       /* Standard configuration */
00214       ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
00215       if (enablecdr) {
00216          ast_cdr_backend_suspend(name);
00217       }
00218       enablecdr = 0;
00219       return -1;
00220    }
00221 
00222    if (reload) {
00223       ast_rwlock_wrlock(&customfields_lock);
00224    }
00225 
00226    if (reload && customfields) {
00227       ast_free(customfields);
00228       customfields = NULL;
00229    }
00230 
00231    while ( (cat = ast_category_browse(cfg, cat)) ) {
00232       if (!strcasecmp(cat, "general")) {
00233          v = ast_variable_browse(cfg, cat);
00234          while (v) {
00235             if (!strcasecmp(v->name, "enabled"))
00236                newenablecdr = ast_true(v->value);
00237 
00238             v = v->next;
00239          }
00240       } else if (!strcasecmp(cat, "mappings")) {
00241          customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
00242          v = ast_variable_browse(cfg, cat);
00243          while (v) {
00244             if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
00245                if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
00246                   ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
00247                   ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
00248                } else {
00249                   ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
00250                   break;
00251                }
00252 
00253             }
00254             v = v->next;
00255          }
00256       }
00257    }
00258 
00259    if (reload) {
00260       ast_rwlock_unlock(&customfields_lock);
00261    }
00262 
00263    ast_config_destroy(cfg);
00264 
00265    if (!newenablecdr) {
00266       ast_cdr_backend_suspend(name);
00267    } else if (newenablecdr) {
00268       ast_cdr_backend_unsuspend(name);
00269    }
00270    enablecdr = newenablecdr;
00271 
00272    return 0;
00273 }
00274 
00275 static int manager_log(struct ast_cdr *cdr)
00276 {
00277    struct ast_tm timeresult;
00278    char strStartTime[80] = "";
00279    char strAnswerTime[80] = "";
00280    char strEndTime[80] = "";
00281    char buf[CUSTOM_FIELDS_BUF_SIZE];
00282 
00283    if (!enablecdr)
00284       return 0;
00285 
00286    ast_localtime(&cdr->start, &timeresult, NULL);
00287    ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00288 
00289    if (cdr->answer.tv_sec) {
00290       ast_localtime(&cdr->answer, &timeresult, NULL);
00291       ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00292    }
00293 
00294    ast_localtime(&cdr->end, &timeresult, NULL);
00295    ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00296 
00297    buf[0] = '\0';
00298    ast_rwlock_rdlock(&customfields_lock);
00299    if (customfields && ast_str_strlen(customfields)) {
00300       struct ast_channel *dummy = ast_dummy_channel_alloc();
00301       if (!dummy) {
00302          ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
00303          return 0;
00304       }
00305       ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
00306       pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
00307       ast_channel_unref(dummy);
00308    }
00309    ast_rwlock_unlock(&customfields_lock);
00310 
00311    manager_event(EVENT_FLAG_CDR, "Cdr",
00312        "AccountCode: %s\r\n"
00313        "Source: %s\r\n"
00314        "Destination: %s\r\n"
00315        "DestinationContext: %s\r\n"
00316        "CallerID: %s\r\n"
00317        "Channel: %s\r\n"
00318        "DestinationChannel: %s\r\n"
00319        "LastApplication: %s\r\n"
00320        "LastData: %s\r\n"
00321        "StartTime: %s\r\n"
00322        "AnswerTime: %s\r\n"
00323        "EndTime: %s\r\n"
00324        "Duration: %ld\r\n"
00325        "BillableSeconds: %ld\r\n"
00326        "Disposition: %s\r\n"
00327        "AMAFlags: %s\r\n"
00328        "UniqueID: %s\r\n"
00329        "UserField: %s\r\n"
00330        "%s",
00331        cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00332        cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00333        cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00334        ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
00335 
00336    return 0;
00337 }
00338 
00339 static int unload_module(void)
00340 {
00341    if (ast_cdr_unregister(name)) {
00342       return -1;
00343    }
00344 
00345    if (customfields)
00346       ast_free(customfields);
00347 
00348    return 0;
00349 }
00350 
00351 static int load_module(void)
00352 {
00353    if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
00354       return AST_MODULE_LOAD_DECLINE;
00355    }
00356 
00357    if (load_config(0)) {
00358       ast_cdr_unregister(name);
00359       return AST_MODULE_LOAD_DECLINE;
00360    }
00361 
00362    return AST_MODULE_LOAD_SUCCESS;
00363 }
00364 
00365 static int reload(void)
00366 {
00367    return load_config(1);
00368 }
00369 
00370 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
00371       .support_level = AST_MODULE_SUPPORT_CORE,
00372       .load = load_module,
00373       .unload = unload_module,
00374       .reload = reload,
00375       .load_pri = AST_MODPRI_CDR_DRIVER,
00376           );

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