00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 278132 $")
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 const 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 == 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
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 }
00133
00134 static int manager_log(struct ast_cdr *cdr)
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 }
00197
00198 static int unload_module(void)
00199 {
00200 ast_cdr_unregister(name);
00201 if (customfields)
00202 ast_free(customfields);
00203
00204 return 0;
00205 }
00206
00207 static int load_module(void)
00208 {
00209 if (load_config(0)) {
00210 return AST_MODULE_LOAD_DECLINE;
00211 }
00212
00213 return AST_MODULE_LOAD_SUCCESS;
00214 }
00215
00216 static int reload(void)
00217 {
00218 return load_config(1);
00219 }
00220
00221 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
00222 .load = load_module,
00223 .unload = unload_module,
00224 .reload = reload,
00225 .load_pri = AST_MODPRI_CDR_DRIVER,
00226 );