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: 202265 $")
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 == 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 struct ast_channel dummy;
00142
00143 if (!enablecdr)
00144 return 0;
00145
00146 ast_localtime(&cdr->start, &timeresult, NULL);
00147 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00148
00149 if (cdr->answer.tv_sec) {
00150 ast_localtime(&cdr->answer, &timeresult, NULL);
00151 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00152 }
00153
00154 ast_localtime(&cdr->end, &timeresult, NULL);
00155 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00156
00157 buf[0] = '\0';
00158 ast_rwlock_rdlock(&customfields_lock);
00159 if (customfields && ast_str_strlen(customfields)) {
00160 memset(&dummy, 0, sizeof(dummy));
00161 dummy.cdr = cdr;
00162 pbx_substitute_variables_helper(&dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
00163 }
00164 ast_rwlock_unlock(&customfields_lock);
00165
00166 manager_event(EVENT_FLAG_CDR, "Cdr",
00167 "AccountCode: %s\r\n"
00168 "Source: %s\r\n"
00169 "Destination: %s\r\n"
00170 "DestinationContext: %s\r\n"
00171 "CallerID: %s\r\n"
00172 "Channel: %s\r\n"
00173 "DestinationChannel: %s\r\n"
00174 "LastApplication: %s\r\n"
00175 "LastData: %s\r\n"
00176 "StartTime: %s\r\n"
00177 "AnswerTime: %s\r\n"
00178 "EndTime: %s\r\n"
00179 "Duration: %ld\r\n"
00180 "BillableSeconds: %ld\r\n"
00181 "Disposition: %s\r\n"
00182 "AMAFlags: %s\r\n"
00183 "UniqueID: %s\r\n"
00184 "UserField: %s\r\n"
00185 "%s",
00186 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00187 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00188 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00189 ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
00190
00191 return 0;
00192 }
00193
00194 static int unload_module(void)
00195 {
00196 ast_cdr_unregister(name);
00197 if (customfields)
00198 ast_free(customfields);
00199
00200 return 0;
00201 }
00202
00203 static int load_module(void)
00204 {
00205 if (load_config(0)) {
00206 return AST_MODULE_LOAD_DECLINE;
00207 }
00208
00209 return AST_MODULE_LOAD_SUCCESS;
00210 }
00211
00212 static int reload(void)
00213 {
00214 return load_config(1);
00215 }
00216
00217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
00218 .load = load_module,
00219 .unload = unload_module,
00220 .reload = reload,
00221 );