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: 69392 $")
00031
00032 #include <sys/types.h>
00033 #include <strings.h>
00034 #include <unistd.h>
00035 #include <time.h>
00036
00037 #include "asterisk/channel.h"
00038 #include "asterisk/cdr.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/manager.h"
00043 #include "asterisk/config.h"
00044
00045 #define DATE_FORMAT "%Y-%m-%d %T"
00046 #define CONF_FILE "cdr_manager.conf"
00047
00048 static char *name = "cdr_manager";
00049
00050 static int enablecdr = 0;
00051
00052 static int loadconfigurationfile(void)
00053 {
00054 char *cat;
00055 struct ast_config *cfg;
00056 struct ast_variable *v;
00057
00058 cfg = ast_config_load(CONF_FILE);
00059 if (!cfg) {
00060
00061 enablecdr = 0;
00062 return 0;
00063 }
00064
00065 cat = ast_category_browse(cfg, NULL);
00066 while (cat) {
00067 if (!strcasecmp(cat, "general")) {
00068 v = ast_variable_browse(cfg, cat);
00069 while (v) {
00070 if (!strcasecmp(v->name, "enabled")) {
00071 enablecdr = ast_true(v->value);
00072 }
00073
00074 v = v->next;
00075 }
00076 }
00077
00078
00079 cat = ast_category_browse(cfg, cat);
00080 }
00081
00082 ast_config_destroy(cfg);
00083 return 1;
00084 }
00085
00086 static int manager_log(struct ast_cdr *cdr)
00087 {
00088 time_t t;
00089 struct tm timeresult;
00090 char strStartTime[80] = "";
00091 char strAnswerTime[80] = "";
00092 char strEndTime[80] = "";
00093
00094 if (!enablecdr)
00095 return 0;
00096
00097 t = cdr->start.tv_sec;
00098 ast_localtime(&t, &timeresult, NULL);
00099 strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00100
00101 if (cdr->answer.tv_sec) {
00102 t = cdr->answer.tv_sec;
00103 ast_localtime(&t, &timeresult, NULL);
00104 strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00105 }
00106
00107 t = cdr->end.tv_sec;
00108 ast_localtime(&t, &timeresult, NULL);
00109 strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00110
00111 manager_event(EVENT_FLAG_CALL, "Cdr",
00112 "AccountCode: %s\r\n"
00113 "Source: %s\r\n"
00114 "Destination: %s\r\n"
00115 "DestinationContext: %s\r\n"
00116 "CallerID: %s\r\n"
00117 "Channel: %s\r\n"
00118 "DestinationChannel: %s\r\n"
00119 "LastApplication: %s\r\n"
00120 "LastData: %s\r\n"
00121 "StartTime: %s\r\n"
00122 "AnswerTime: %s\r\n"
00123 "EndTime: %s\r\n"
00124 "Duration: %ld\r\n"
00125 "BillableSeconds: %ld\r\n"
00126 "Disposition: %s\r\n"
00127 "AMAFlags: %s\r\n"
00128 "UniqueID: %s\r\n"
00129 "UserField: %s\r\n",
00130 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00131 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00132 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00133 ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield);
00134
00135 return 0;
00136 }
00137
00138 static int unload_module(void)
00139 {
00140 ast_cdr_unregister(name);
00141 return 0;
00142 }
00143
00144 static int load_module(void)
00145 {
00146 int res;
00147
00148
00149 if (!loadconfigurationfile())
00150 return AST_MODULE_LOAD_DECLINE;
00151
00152 res = ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
00153 if (res) {
00154 ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
00155 }
00156
00157 return res;
00158 }
00159
00160 static int reload(void)
00161 {
00162 loadconfigurationfile();
00163 return 0;
00164 }
00165
00166 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
00167 .load = load_module,
00168 .unload = unload_module,
00169 .reload = reload,
00170 );