Wed Apr 6 11:29:54 2011

Asterisk developer's documentation


cdr_manager.c File Reference

Asterisk Call Manager CDR records. More...

#include "asterisk.h"
#include <time.h>
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"

Go to the source code of this file.

Defines

#define CONF_FILE   "cdr_manager.conf"
#define CUSTOM_FIELDS_BUF_SIZE   1024
#define DATE_FORMAT   "%Y-%m-%d %T"

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_config (int reload)
static int load_module (void)
static int manager_log (struct ast_cdr *cdr)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk Manager Interface CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_strcustomfields
static ast_rwlock_t customfields_lock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RWLOCK_INITIALIZER }
static int enablecdr = 0
static const char name [] = "cdr_manager"


Detailed Description

Asterisk Call Manager CDR records.

See also

Definition in file cdr_manager.c.


Define Documentation

#define CONF_FILE   "cdr_manager.conf"

Definition at line 43 of file cdr_manager.c.

Referenced by load_config().

#define CUSTOM_FIELDS_BUF_SIZE   1024

Definition at line 44 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

#define DATE_FORMAT   "%Y-%m-%d %T"

Definition at line 42 of file cdr_manager.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 226 of file cdr_manager.c.

static void __unreg_module ( void   )  [static]

Definition at line 226 of file cdr_manager.c.

static int load_config ( int  reload  )  [static]

Definition at line 55 of file cdr_manager.c.

References ast_category_browse(), ast_cdr_register(), ast_cdr_unregister(), ast_config_destroy(), ast_config_load, ast_free, ast_log(), ast_rwlock_unlock, ast_rwlock_wrlock, ast_str_append(), ast_str_create(), ast_str_size(), ast_str_strlen(), ast_strlen_zero(), ast_true(), ast_variable_browse(), CONF_FILE, CONFIG_FLAG_FILEUNCHANGED, config_flags, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, CUSTOM_FIELDS_BUF_SIZE, customfields, customfields_lock, LOG_ERROR, LOG_NOTICE, LOG_WARNING, manager_log(), ast_variable::name, ast_variable::next, and ast_variable::value.

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 == CONFIG_STATUS_FILEINVALID) {
00069       ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
00070       return -1;
00071    }
00072 
00073    if (!cfg) {
00074       /* Standard configuration */
00075       ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
00076       if (enablecdr)
00077          ast_cdr_unregister(name);
00078       enablecdr = 0;
00079       return -1;
00080    }
00081 
00082    if (reload) {
00083       ast_rwlock_wrlock(&customfields_lock);
00084    }
00085 
00086    if (reload && customfields) {
00087       ast_free(customfields);
00088       customfields = NULL;
00089    }
00090 
00091    while ( (cat = ast_category_browse(cfg, cat)) ) {
00092       if (!strcasecmp(cat, "general")) {
00093          v = ast_variable_browse(cfg, cat);
00094          while (v) {
00095             if (!strcasecmp(v->name, "enabled"))
00096                newenablecdr = ast_true(v->value);
00097 
00098             v = v->next;
00099          }
00100       } else if (!strcasecmp(cat, "mappings")) {
00101          customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
00102          v = ast_variable_browse(cfg, cat);
00103          while (v) {
00104             if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
00105                if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
00106                   ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
00107                   ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
00108                } else {
00109                   ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
00110                   break;
00111                }
00112 
00113             }
00114             v = v->next;
00115          }
00116       }
00117    }
00118 
00119    if (reload) {
00120       ast_rwlock_unlock(&customfields_lock);
00121    }
00122 
00123    ast_config_destroy(cfg);
00124 
00125    if (enablecdr && !newenablecdr)
00126       ast_cdr_unregister(name);
00127    else if (!enablecdr && newenablecdr)
00128       ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
00129    enablecdr = newenablecdr;
00130 
00131    return 0;
00132 }

static int load_module ( void   )  [static]

Definition at line 207 of file cdr_manager.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and load_config().

00208 {
00209    if (load_config(0)) {
00210       return AST_MODULE_LOAD_DECLINE;
00211    }
00212 
00213    return AST_MODULE_LOAD_SUCCESS;
00214 }

static int manager_log ( struct ast_cdr cdr  )  [static]

Definition at line 134 of file cdr_manager.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_cdr_disp2str(), ast_cdr_dup(), ast_cdr_flags2str(), ast_channel_release(), ast_dummy_channel_alloc(), ast_localtime(), ast_log(), ast_rwlock_rdlock, ast_rwlock_unlock, ast_str_buffer(), ast_str_strlen(), ast_strftime(), ast_cdr::billsec, ast_channel::cdr, ast_cdr::channel, ast_cdr::clid, CUSTOM_FIELDS_BUF_SIZE, customfields, customfields_lock, DATE_FORMAT, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, dummy(), ast_cdr::duration, ast_cdr::end, EVENT_FLAG_CDR, ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, manager_event, pbx_substitute_variables_helper(), ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_config().

00135 {
00136    struct ast_tm timeresult;
00137    char strStartTime[80] = "";
00138    char strAnswerTime[80] = "";
00139    char strEndTime[80] = "";
00140    char buf[CUSTOM_FIELDS_BUF_SIZE];
00141 
00142    if (!enablecdr)
00143       return 0;
00144 
00145    ast_localtime(&cdr->start, &timeresult, NULL);
00146    ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00147 
00148    if (cdr->answer.tv_sec) {
00149       ast_localtime(&cdr->answer, &timeresult, NULL);
00150       ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00151    }
00152 
00153    ast_localtime(&cdr->end, &timeresult, NULL);
00154    ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00155 
00156    buf[0] = '\0';
00157    ast_rwlock_rdlock(&customfields_lock);
00158    if (customfields && ast_str_strlen(customfields)) {
00159       struct ast_channel *dummy = ast_dummy_channel_alloc();
00160       if (!dummy) {
00161          ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
00162          return 0;
00163       }
00164       dummy->cdr = ast_cdr_dup(cdr);
00165       pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
00166       ast_channel_release(dummy);
00167    }
00168    ast_rwlock_unlock(&customfields_lock);
00169 
00170    manager_event(EVENT_FLAG_CDR, "Cdr",
00171        "AccountCode: %s\r\n"
00172        "Source: %s\r\n"
00173        "Destination: %s\r\n"
00174        "DestinationContext: %s\r\n"
00175        "CallerID: %s\r\n"
00176        "Channel: %s\r\n"
00177        "DestinationChannel: %s\r\n"
00178        "LastApplication: %s\r\n"
00179        "LastData: %s\r\n"
00180        "StartTime: %s\r\n"
00181        "AnswerTime: %s\r\n"
00182        "EndTime: %s\r\n"
00183        "Duration: %ld\r\n"
00184        "BillableSeconds: %ld\r\n"
00185        "Disposition: %s\r\n"
00186        "AMAFlags: %s\r\n"
00187        "UniqueID: %s\r\n"
00188        "UserField: %s\r\n"
00189        "%s",
00190        cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00191        cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00192        cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00193        ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
00194 
00195    return 0;
00196 }

static int reload ( void   )  [static]

Definition at line 216 of file cdr_manager.c.

References load_config().

00217 {
00218    return load_config(1);
00219 }

static int unload_module ( void   )  [static]

Definition at line 198 of file cdr_manager.c.

References ast_cdr_unregister(), ast_free, and customfields.

00199 {
00200    ast_cdr_unregister(name);
00201    if (customfields)
00202       ast_free(customfields);
00203 
00204    return 0;
00205 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk Manager Interface CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 226 of file cdr_manager.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 226 of file cdr_manager.c.

struct ast_str* customfields [static]

Definition at line 50 of file cdr_manager.c.

Referenced by load_config(), manager_log(), and unload_module().

ast_rwlock_t customfields_lock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RWLOCK_INITIALIZER } [static]

Definition at line 51 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

int enablecdr = 0 [static]

Definition at line 48 of file cdr_manager.c.

const char name[] = "cdr_manager" [static]

Definition at line 46 of file cdr_manager.c.


Generated on Wed Apr 6 11:29:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7