| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#include "asterisk.h" |
| 171 |
|
| 172 |
#include <time.h> |
| 173 |
|
| 174 |
#include "asterisk/channel.h" |
| 175 |
#include "asterisk/cdr.h" |
| 176 |
#include "asterisk/module.h" |
| 177 |
#include "asterisk/utils.h" |
| 178 |
#include "asterisk/manager.h" |
| 179 |
#include "asterisk/config.h" |
| 180 |
#include "asterisk/pbx.h" |
| 181 |
|
| 182 |
#define DATE_FORMAT "%Y-%m-%d %T" |
| 183 |
#define CONF_FILE "cdr_manager.conf" |
| 184 |
#define CUSTOM_FIELDS_BUF_SIZE 1024 |
| 185 |
|
| 186 |
static const char name[] = "cdr_manager"; |
| 187 |
|
| 188 |
static int enablecdr = 0; |
| 189 |
|
| 190 |
static struct ast_str *customfields; |
| 191 |
AST_RWLOCK_DEFINE_STATIC(customfields_lock); |
| 192 |
|
| 193 |
static int manager_log(struct ast_cdr *cdr); |
| 194 |
|
| 195 |
static int load_config(int reload) |
| 196 |
{ |
| 197 |
char *cat = NULL; |
| 198 |
struct ast_config *cfg; |
| 199 |
struct ast_variable *v; |
| 200 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 201 |
int newenablecdr = 0; |
| 202 |
|
| 203 |
cfg = ast_config_load(CONF_FILE, config_flags); |
| 204 |
if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 209 |
ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
if (!cfg) { |
| 214 |
|
| 215 |
ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n"); |
| 216 |
if (enablecdr) { |
| 217 |
ast_cdr_backend_suspend(name); |
| 218 |
} |
| 219 |
enablecdr = 0; |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
if (reload) { |
| 224 |
ast_rwlock_wrlock(&customfields_lock); |
| 225 |
} |
| 226 |
|
| 227 |
if (reload && customfields) { |
| 228 |
ast_free(customfields); |
| 229 |
customfields = NULL; |
| 230 |
} |
| 231 |
|
| 232 |
while ( (cat = ast_category_browse(cfg, cat)) ) { |
| 233 |
if (!strcasecmp(cat, "general")) { |
| 234 |
v = ast_variable_browse(cfg, cat); |
| 235 |
while (v) { |
| 236 |
if (!strcasecmp(v->name, "enabled")) |
| 237 |
newenablecdr = ast_true(v->value); |
| 238 |
|
| 239 |
v = v->next; |
| 240 |
} |
| 241 |
} else if (!strcasecmp(cat, "mappings")) { |
| 242 |
customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE); |
| 243 |
v = ast_variable_browse(cfg, cat); |
| 244 |
while (v) { |
| 245 |
if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) { |
| 246 |
if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) { |
| 247 |
ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name); |
| 248 |
ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name); |
| 249 |
} else { |
| 250 |
ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n"); |
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
v = v->next; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (reload) { |
| 261 |
ast_rwlock_unlock(&customfields_lock); |
| 262 |
} |
| 263 |
|
| 264 |
ast_config_destroy(cfg); |
| 265 |
|
| 266 |
if (!newenablecdr) { |
| 267 |
ast_cdr_backend_suspend(name); |
| 268 |
} else if (newenablecdr) { |
| 269 |
ast_cdr_backend_unsuspend(name); |
| 270 |
} |
| 271 |
enablecdr = newenablecdr; |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
static int manager_log(struct ast_cdr *cdr) |
| 277 |
{ |
| 278 |
struct ast_tm timeresult; |
| 279 |
char strStartTime[80] = ""; |
| 280 |
char strAnswerTime[80] = ""; |
| 281 |
char strEndTime[80] = ""; |
| 282 |
char buf[CUSTOM_FIELDS_BUF_SIZE]; |
| 283 |
|
| 284 |
if (!enablecdr) |
| 285 |
return 0; |
| 286 |
|
| 287 |
ast_localtime(&cdr->start, &timeresult, NULL); |
| 288 |
ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult); |
| 289 |
|
| 290 |
if (cdr->answer.tv_sec) { |
| 291 |
ast_localtime(&cdr->answer, &timeresult, NULL); |
| 292 |
ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult); |
| 293 |
} |
| 294 |
|
| 295 |
ast_localtime(&cdr->end, &timeresult, NULL); |
| 296 |
ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult); |
| 297 |
|
| 298 |
buf[0] = '\0'; |
| 299 |
ast_rwlock_rdlock(&customfields_lock); |
| 300 |
if (customfields && ast_str_strlen(customfields)) { |
| 301 |
struct ast_channel *dummy = ast_dummy_channel_alloc(); |
| 302 |
if (!dummy) { |
| 303 |
ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n"); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
ast_channel_cdr_set(dummy, ast_cdr_dup(cdr)); |
| 307 |
pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1); |
| 308 |
ast_channel_unref(dummy); |
| 309 |
} |
| 310 |
ast_rwlock_unlock(&customfields_lock); |
| 311 |
|
| 312 |
manager_event(EVENT_FLAG_CDR, "Cdr", |
| 313 |
"AccountCode: %s\r\n" |
| 314 |
"Source: %s\r\n" |
| 315 |
"Destination: %s\r\n" |
| 316 |
"DestinationContext: %s\r\n" |
| 317 |
"CallerID: %s\r\n" |
| 318 |
"Channel: %s\r\n" |
| 319 |
"DestinationChannel: %s\r\n" |
| 320 |
"LastApplication: %s\r\n" |
| 321 |
"LastData: %s\r\n" |
| 322 |
"StartTime: %s\r\n" |
| 323 |
"AnswerTime: %s\r\n" |
| 324 |
"EndTime: %s\r\n" |
| 325 |
"Duration: %ld\r\n" |
| 326 |
"BillableSeconds: %ld\r\n" |
| 327 |
"Disposition: %s\r\n" |
| 328 |
"AMAFlags: %s\r\n" |
| 329 |
"UniqueID: %s\r\n" |
| 330 |
"UserField: %s\r\n" |
| 331 |
"%s", |
| 332 |
cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel, |
| 333 |
cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime, |
| 334 |
cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), |
| 335 |
ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf); |
| 336 |
|
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
static int unload_module(void) |
| 341 |
{ |
| 342 |
if (ast_cdr_unregister(name)) { |
| 343 |
return -1; |
| 344 |
} |
| 345 |
|
| 346 |
if (customfields) |
| 347 |
ast_free(customfields); |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
static int load_module(void) |
| 353 |
{ |
| 354 |
if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) { |
| 355 |
return AST_MODULE_LOAD_DECLINE; |
| 356 |
} |
| 357 |
|
| 358 |
if (load_config(0)) { |
| 359 |
ast_cdr_unregister(name); |
| 360 |
return AST_MODULE_LOAD_DECLINE; |
| 361 |
} |
| 362 |
|
| 363 |
return AST_MODULE_LOAD_SUCCESS; |
| 364 |
} |
| 365 |
|
| 366 |
static int reload(void) |
| 367 |
{ |
| 368 |
return load_config(1); |
| 369 |
} |
| 370 |
|
| 371 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend", |
| 372 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 373 |
.load = load_module, |
| 374 |
.unload = unload_module, |
| 375 |
.reload = reload, |
| 376 |
.load_pri = AST_MODPRI_CDR_DRIVER, |
| 377 |
.requires = "cdr", |
| 378 |
); |