Skip to content
Snippets Groups Projects
cdr_manager.c 5.62 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2004 - 2005
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
    
     *
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     * \brief Asterisk Call Manager CDR records.
    
     * See also
     * \arg \ref AstCDR
     * \arg \ref AstAMI
     * \arg \ref Config_ami
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup cdr_drivers
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/channel.h"
    #include "asterisk/cdr.h"
    #include "asterisk/module.h"
    #include "asterisk/utils.h"
    #include "asterisk/manager.h"
    #include "asterisk/config.h"
    
    
    #define DATE_FORMAT 	"%Y-%m-%d %T"
    #define CONF_FILE	"cdr_manager.conf"
    
    static const char name[] = "cdr_manager";
    
    static struct ast_str *customfields;
    
    AST_RWLOCK_DEFINE_STATIC(customfields_lock);
    
    static int manager_log(struct ast_cdr *cdr);
    
    
    static int load_config(int reload)
    
    	struct ast_config *cfg;
    	struct ast_variable *v;
    
    	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
    
    
    	cfg = ast_config_load(CONF_FILE, config_flags);
    
    	if (cfg == CONFIG_STATUS_FILEINVALID) {
    		ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
    
    	if (!cfg) {
    		/* Standard configuration */
    
    		ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
    
    		if (enablecdr)
    			ast_cdr_unregister(name);
    
    	if (reload) {
    		ast_rwlock_wrlock(&customfields_lock);
    	}
    
    	if (reload && customfields) {
    		ast_free(customfields);
    		customfields = NULL;
    	}
    
    
    	while ( (cat = ast_category_browse(cfg, cat)) ) {
    
    		if (!strcasecmp(cat, "general")) {
    			v = ast_variable_browse(cfg, cat);
    			while (v) {
    
    				if (!strcasecmp(v->name, "enabled"))
    					newenablecdr = ast_true(v->value);
    
    				v = v->next;
    			}
    		} else if (!strcasecmp(cat, "mappings")) {
    			customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
    			v = ast_variable_browse(cfg, cat);
    			while (v) {
    				if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
    
    					if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
    
    						ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
    						ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
    					} else {
    						ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
    						break;
    					}
    
    	if (reload) {
    		ast_rwlock_unlock(&customfields_lock);
    	}
    
    
    	ast_config_destroy(cfg);
    
    
    	if (enablecdr && !newenablecdr)
    		ast_cdr_unregister(name);
    	else if (!enablecdr && newenablecdr)
    		ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
    
    }
    
    static int manager_log(struct ast_cdr *cdr)
    {
    
    	char strStartTime[80] = "";
    	char strAnswerTime[80] = "";
    	char strEndTime[80] = "";
    
    	ast_localtime(&cdr->start, &timeresult, NULL);
    	ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
    
    		ast_localtime(&cdr->answer, &timeresult, NULL);
    		ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
    
    	ast_localtime(&cdr->end, &timeresult, NULL);
    	ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
    
    	buf[0] = '\0';
    	ast_rwlock_rdlock(&customfields_lock);
    	if (customfields && ast_str_strlen(customfields)) {
    
    		struct ast_channel *dummy = ast_dummy_channel_alloc();
    
    		if (!dummy) {
    			ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
    			return 0;
    		}
    		dummy->cdr = ast_cdr_dup(cdr);
    		pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
    		ast_channel_release(dummy);
    
    	ast_rwlock_unlock(&customfields_lock);
    
    	    "AccountCode: %s\r\n"
    	    "Source: %s\r\n"
    	    "Destination: %s\r\n"
    	    "DestinationContext: %s\r\n"
    	    "CallerID: %s\r\n"
    	    "Channel: %s\r\n"
    	    "DestinationChannel: %s\r\n"
    	    "LastApplication: %s\r\n"
    	    "LastData: %s\r\n"
    	    "StartTime: %s\r\n"
    	    "AnswerTime: %s\r\n"
    	    "EndTime: %s\r\n"
    
    	    "Duration: %ld\r\n"
    	    "BillableSeconds: %ld\r\n"
    
    	    "Disposition: %s\r\n"
    	    "AMAFlags: %s\r\n"
    	    "UniqueID: %s\r\n"
    
    	    cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
    	    cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
    
    	    cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
    	    ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
    
    
    static int unload_module(void)
    
    static int load_module(void)
    
    		return AST_MODULE_LOAD_DECLINE;
    
    AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
    		.load = load_module,
    		.unload = unload_module,
    		.reload = reload,
    	       );