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"

Include dependency graph for cdr_manager.c:

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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .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 = { {0} , NULL, 1 }
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 182 of file cdr_manager.c.

Referenced by load_config().

#define CUSTOM_FIELDS_BUF_SIZE   1024

Definition at line 183 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

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

Definition at line 181 of file cdr_manager.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 376 of file cdr_manager.c.

static void __unreg_module ( void   )  [static]

Definition at line 376 of file cdr_manager.c.

static int load_config ( int  reload  )  [static]

Definition at line 194 of file cdr_manager.c.

References ast_category_browse(), ast_cdr_backend_suspend(), ast_cdr_backend_unsuspend(), 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_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, CUSTOM_FIELDS_BUF_SIZE, customfields_lock, LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, and ast_variable::value.

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 }

static int load_module ( void   )  [static]

Definition at line 351 of file cdr_manager.c.

References ast_cdr_register(), ast_cdr_unregister(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, load_config(), and manager_log().

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 }

static int manager_log ( struct ast_cdr cdr  )  [static]

Definition at line 275 of file cdr_manager.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_cdr_disp2str(), ast_cdr_dup(), ast_channel_amaflags2string(), ast_channel_cdr_set(), ast_channel_unref, 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, buf, ast_cdr::channel, ast_cdr::clid, CUSTOM_FIELDS_BUF_SIZE, 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, NULL, pbx_substitute_variables_helper(), ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_config(), and load_module().

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 }

static int reload ( void   )  [static]

Definition at line 365 of file cdr_manager.c.

References load_config().

00366 {
00367    return load_config(1);
00368 }

static int unload_module ( void   )  [static]

Definition at line 339 of file cdr_manager.c.

References ast_cdr_unregister(), and ast_free.

00340 {
00341    if (ast_cdr_unregister(name)) {
00342       return -1;
00343    }
00344 
00345    if (customfields)
00346       ast_free(customfields);
00347 
00348    return 0;
00349 }


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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 376 of file cdr_manager.c.

Definition at line 376 of file cdr_manager.c.

struct ast_str* customfields [static]

Definition at line 189 of file cdr_manager.c.

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

Definition at line 190 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

int enablecdr = 0 [static]

Definition at line 187 of file cdr_manager.c.

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

Definition at line 185 of file cdr_manager.c.


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