Thu Jul 9 13:40:23 2009

Asterisk developer's documentation


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 /*! \file
00018  *
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 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 202263 $")
00031 
00032 #include <time.h>
00033 
00034 #include "asterisk/channel.h"
00035 #include "asterisk/cdr.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/manager.h"
00039 #include "asterisk/config.h"
00040 #include "asterisk/pbx.h"
00041 
00042 #define DATE_FORMAT  "%Y-%m-%d %T"
00043 #define CONF_FILE "cdr_manager.conf"
00044 #define CUSTOM_FIELDS_BUF_SIZE 1024
00045 
00046 static char *name = "cdr_manager";
00047 
00048 static int enablecdr = 0;
00049 
00050 static struct ast_str *customfields;
00051 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
00052 
00053 static int manager_log(struct ast_cdr *cdr);
00054 
00055 static int load_config(int reload)
00056 {
00057    char *cat = NULL;
00058    struct ast_config *cfg;
00059    struct ast_variable *v;
00060    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00061    int newenablecdr = 0;
00062 
00063    cfg = ast_config_load(CONF_FILE, config_flags);
00064    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00065       return 0;
00066    }
00067 
00068    if (!cfg) {
00069       /* Standard configuration */
00070       ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
00071       if (enablecdr)
00072          ast_cdr_unregister(name);
00073       enablecdr = 0;
00074       return -1;
00075    }
00076    
00077    if (reload) {
00078       ast_rwlock_wrlock(&customfields_lock);
00079    }
00080 
00081    if (reload && customfields) {
00082       ast_free(customfields);
00083       customfields = NULL;
00084    }
00085 
00086    while ( (cat = ast_category_browse(cfg, cat)) ) {
00087       if (!strcasecmp(cat, "general")) {
00088          v = ast_variable_browse(cfg, cat);
00089          while (v) {
00090             if (!strcasecmp(v->name, "enabled"))
00091                newenablecdr = ast_true(v->value);
00092             
00093             v = v->next;
00094          }
00095       } else if (!strcasecmp(cat, "mappings")) {
00096          customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
00097          v = ast_variable_browse(cfg, cat);
00098          while (v) {
00099             if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
00100                if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
00101                   ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
00102                   ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
00103                } else {
00104                   ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
00105                   break;
00106                }
00107                
00108             }
00109             v = v->next;
00110          }
00111       }
00112    }
00113    
00114    if (reload) {
00115       ast_rwlock_unlock(&customfields_lock);
00116    }
00117 
00118    ast_config_destroy(cfg);
00119 
00120    if (enablecdr && !newenablecdr)
00121       ast_cdr_unregister(name);
00122    else if (!enablecdr && newenablecdr)
00123       ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
00124    enablecdr = newenablecdr;
00125 
00126    return 0;
00127 }
00128 
00129 static int manager_log(struct ast_cdr *cdr)
00130 {
00131    struct ast_tm timeresult;
00132    char strStartTime[80] = "";
00133    char strAnswerTime[80] = "";
00134    char strEndTime[80] = "";
00135    char buf[CUSTOM_FIELDS_BUF_SIZE];
00136    struct ast_channel dummy;
00137 
00138    if (!enablecdr)
00139       return 0;
00140 
00141    ast_localtime(&cdr->start, &timeresult, NULL);
00142    ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00143    
00144    if (cdr->answer.tv_sec) {
00145       ast_localtime(&cdr->answer, &timeresult, NULL);
00146       ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00147    }
00148 
00149    ast_localtime(&cdr->end, &timeresult, NULL);
00150    ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00151 
00152    buf[0] = '\0';
00153    ast_rwlock_rdlock(&customfields_lock);
00154    if (customfields && ast_str_strlen(customfields)) {
00155       memset(&dummy, 0, sizeof(dummy));
00156       dummy.cdr = cdr;
00157       pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
00158    }
00159    ast_rwlock_unlock(&customfields_lock);
00160 
00161    manager_event(EVENT_FLAG_CDR, "Cdr",
00162        "AccountCode: %s\r\n"
00163        "Source: %s\r\n"
00164        "Destination: %s\r\n"
00165        "DestinationContext: %s\r\n"
00166        "CallerID: %s\r\n"
00167        "Channel: %s\r\n"
00168        "DestinationChannel: %s\r\n"
00169        "LastApplication: %s\r\n"
00170        "LastData: %s\r\n"
00171        "StartTime: %s\r\n"
00172        "AnswerTime: %s\r\n"
00173        "EndTime: %s\r\n"
00174        "Duration: %ld\r\n"
00175        "BillableSeconds: %ld\r\n"
00176        "Disposition: %s\r\n"
00177        "AMAFlags: %s\r\n"
00178        "UniqueID: %s\r\n"
00179        "UserField: %s\r\n"
00180        "%s",
00181        cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00182        cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00183        cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00184        ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
00185 
00186    return 0;
00187 }
00188 
00189 static int unload_module(void)
00190 {
00191    ast_cdr_unregister(name);
00192    if (customfields)
00193       ast_free(customfields);
00194 
00195    return 0;
00196 }
00197 
00198 static int load_module(void)
00199 {
00200    if (load_config(0)) {
00201       return AST_MODULE_LOAD_DECLINE;
00202    }
00203 
00204    return AST_MODULE_LOAD_SUCCESS;
00205 }
00206 
00207 static int reload(void)
00208 {
00209    return load_config(1);
00210 }
00211 
00212 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
00213       .load = load_module,
00214       .unload = unload_module,
00215       .reload = reload,
00216           );

Generated on Thu Jul 9 13:40:23 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7