| 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 |
#include "asterisk.h" |
| 44 |
|
| 45 |
#include "asterisk/config.h" |
| 46 |
#include "asterisk/channel.h" |
| 47 |
#include "asterisk/cdr.h" |
| 48 |
#include "asterisk/module.h" |
| 49 |
#include "asterisk/res_odbc.h" |
| 50 |
|
| 51 |
#define DATE_FORMAT "%Y-%m-%d %T" |
| 52 |
|
| 53 |
static const char name[] = "ODBC"; |
| 54 |
static const char config_file[] = "cdr_odbc.conf"; |
| 55 |
static char *dsn = NULL, *table = NULL; |
| 56 |
|
| 57 |
enum { |
| 58 |
CONFIG_LOGUNIQUEID = 1 << 0, |
| 59 |
CONFIG_USEGMTIME = 1 << 1, |
| 60 |
CONFIG_DISPOSITIONSTRING = 1 << 2, |
| 61 |
CONFIG_HRTIME = 1 << 3, |
| 62 |
CONFIG_REGISTERED = 1 << 4, |
| 63 |
CONFIG_NEWCDRCOLUMNS = 1 << 5, |
| 64 |
}; |
| 65 |
|
| 66 |
static struct ast_flags config = { 0 }; |
| 67 |
|
| 68 |
static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data) |
| 69 |
{ |
| 70 |
struct ast_cdr *cdr = data; |
| 71 |
SQLRETURN ODBC_res; |
| 72 |
char sqlcmd[2048] = "", timestr[128], new_columns[120] = "", new_values[7] = ""; |
| 73 |
struct ast_tm tm; |
| 74 |
SQLHSTMT stmt; |
| 75 |
int i = 0; |
| 76 |
|
| 77 |
ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL); |
| 78 |
ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); |
| 79 |
|
| 80 |
if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) { |
| 81 |
snprintf(new_columns, sizeof(new_columns), "%s", ",peeraccount,linkedid,sequence"); |
| 82 |
snprintf(new_values, sizeof(new_values), "%s", ",?,?,?"); |
| 83 |
} |
| 84 |
|
| 85 |
if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) { |
| 86 |
snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " |
| 87 |
"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp," |
| 88 |
"lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield%s) " |
| 89 |
"VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values); |
| 90 |
} else { |
| 91 |
snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " |
| 92 |
"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata," |
| 93 |
"duration,billsec,disposition,amaflags,accountcode%s) " |
| 94 |
"VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values); |
| 95 |
} |
| 96 |
|
| 97 |
ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 98 |
|
| 99 |
if (!SQL_SUCCEEDED(ODBC_res)) { |
| 100 |
ast_log(LOG_WARNING, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res); |
| 101 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL); |
| 106 |
SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL); |
| 107 |
SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL); |
| 108 |
SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL); |
| 109 |
SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL); |
| 110 |
SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL); |
| 111 |
SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL); |
| 112 |
SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL); |
| 113 |
|
| 114 |
if (ast_test_flag(&config, CONFIG_HRTIME)) { |
| 115 |
double hrbillsec = 0.0; |
| 116 |
double hrduration; |
| 117 |
|
| 118 |
if (!ast_tvzero(cdr->answer)) { |
| 119 |
hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0; |
| 120 |
} |
| 121 |
hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0; |
| 122 |
|
| 123 |
SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrduration, 0, NULL); |
| 124 |
SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrbillsec, 0, NULL); |
| 125 |
} else { |
| 126 |
SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL); |
| 127 |
SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL); |
| 128 |
} |
| 129 |
|
| 130 |
if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING)) { |
| 131 |
char *disposition; |
| 132 |
disposition = ast_strdupa(ast_cdr_disp2str(cdr->disposition)); |
| 133 |
SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(disposition) + 1, 0, disposition, 0, NULL); |
| 134 |
} else { |
| 135 |
SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL); |
| 136 |
} |
| 137 |
SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL); |
| 138 |
SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL); |
| 139 |
|
| 140 |
i = 14; |
| 141 |
if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) { |
| 142 |
SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL); |
| 143 |
SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL); |
| 144 |
i = 16; |
| 145 |
} |
| 146 |
|
| 147 |
if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) { |
| 148 |
SQLBindParameter(stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->peeraccount), 0, cdr->peeraccount, 0, NULL); |
| 149 |
SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->linkedid), 0, cdr->linkedid, 0, NULL); |
| 150 |
SQLBindParameter(stmt, i + 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->sequence, 0, NULL); |
| 151 |
} |
| 152 |
|
| 153 |
ODBC_res = ast_odbc_execute_sql(obj, stmt, sqlcmd); |
| 154 |
|
| 155 |
if (!SQL_SUCCEEDED(ODBC_res)) { |
| 156 |
ast_log(LOG_WARNING, "cdr_odbc: Error in ExecDirect: %d, query is: %s\n", ODBC_res, sqlcmd); |
| 157 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
return stmt; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static int odbc_log(struct ast_cdr *cdr) |
| 166 |
{ |
| 167 |
struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0); |
| 168 |
SQLHSTMT stmt; |
| 169 |
|
| 170 |
if (!obj) { |
| 171 |
ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n"); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
stmt = ast_odbc_direct_execute(obj, execute_cb, cdr); |
| 176 |
if (stmt) { |
| 177 |
SQLLEN rows = 0; |
| 178 |
|
| 179 |
SQLRowCount(stmt, &rows); |
| 180 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 181 |
|
| 182 |
if (rows == 0) |
| 183 |
ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n"); |
| 184 |
} else |
| 185 |
ast_log(LOG_ERROR, "CDR direct execute failed\n"); |
| 186 |
ast_odbc_release_obj(obj); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int odbc_load_module(int reload) |
| 191 |
{ |
| 192 |
int res = 0; |
| 193 |
struct ast_config *cfg; |
| 194 |
struct ast_variable *var; |
| 195 |
const char *tmp; |
| 196 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 197 |
|
| 198 |
do { |
| 199 |
cfg = ast_config_load(config_file, config_flags); |
| 200 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 201 |
ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file); |
| 202 |
res = AST_MODULE_LOAD_DECLINE; |
| 203 |
break; |
| 204 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) |
| 205 |
break; |
| 206 |
|
| 207 |
var = ast_variable_browse(cfg, "global"); |
| 208 |
if (!var) { |
| 209 |
|
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) { |
| 214 |
ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n"); |
| 215 |
tmp = "asteriskdb"; |
| 216 |
} |
| 217 |
if (dsn) |
| 218 |
ast_free(dsn); |
| 219 |
dsn = ast_strdup(tmp); |
| 220 |
if (dsn == NULL) { |
| 221 |
res = -1; |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp)) |
| 226 |
ast_set_flag(&config, CONFIG_DISPOSITIONSTRING); |
| 227 |
else |
| 228 |
ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING); |
| 229 |
|
| 230 |
if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) { |
| 231 |
ast_set_flag(&config, CONFIG_LOGUNIQUEID); |
| 232 |
ast_debug(1, "cdr_odbc: Logging uniqueid\n"); |
| 233 |
} else { |
| 234 |
ast_clear_flag(&config, CONFIG_LOGUNIQUEID); |
| 235 |
ast_debug(1, "cdr_odbc: Not logging uniqueid\n"); |
| 236 |
} |
| 237 |
|
| 238 |
if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) { |
| 239 |
ast_set_flag(&config, CONFIG_USEGMTIME); |
| 240 |
ast_debug(1, "cdr_odbc: Logging in GMT\n"); |
| 241 |
} else { |
| 242 |
ast_clear_flag(&config, CONFIG_USEGMTIME); |
| 243 |
ast_debug(1, "cdr_odbc: Logging in local time\n"); |
| 244 |
} |
| 245 |
|
| 246 |
if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) { |
| 247 |
ast_set_flag(&config, CONFIG_HRTIME); |
| 248 |
ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n"); |
| 249 |
} else { |
| 250 |
ast_clear_flag(&config, CONFIG_HRTIME); |
| 251 |
ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n"); |
| 252 |
} |
| 253 |
|
| 254 |
if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) { |
| 255 |
ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n"); |
| 256 |
tmp = "cdr"; |
| 257 |
} |
| 258 |
if (table) |
| 259 |
ast_free(table); |
| 260 |
table = ast_strdup(tmp); |
| 261 |
if (table == NULL) { |
| 262 |
res = -1; |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
if (!ast_test_flag(&config, CONFIG_REGISTERED)) { |
| 267 |
res = ast_cdr_register(name, ast_module_info->description, odbc_log); |
| 268 |
if (res) { |
| 269 |
ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n"); |
| 270 |
} else { |
| 271 |
ast_set_flag(&config, CONFIG_REGISTERED); |
| 272 |
} |
| 273 |
} |
| 274 |
if (((tmp = ast_variable_retrieve(cfg, "global", "newcdrcolumns"))) && ast_true(tmp)) { |
| 275 |
ast_set_flag(&config, CONFIG_NEWCDRCOLUMNS); |
| 276 |
ast_debug(1, "cdr_odbc: Add new cdr columns\n"); |
| 277 |
} else { |
| 278 |
ast_clear_flag(&config, CONFIG_NEWCDRCOLUMNS); |
| 279 |
ast_debug(1, "cdr_odbc: Not add new cdr columns\n"); |
| 280 |
} |
| 281 |
} while (0); |
| 282 |
|
| 283 |
if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) { |
| 284 |
ast_cdr_backend_suspend(name); |
| 285 |
ast_clear_flag(&config, CONFIG_REGISTERED); |
| 286 |
} else { |
| 287 |
ast_cdr_backend_unsuspend(name); |
| 288 |
} |
| 289 |
|
| 290 |
if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) { |
| 291 |
ast_config_destroy(cfg); |
| 292 |
} |
| 293 |
return res; |
| 294 |
} |
| 295 |
|
| 296 |
static int load_module(void) |
| 297 |
{ |
| 298 |
return odbc_load_module(0); |
| 299 |
} |
| 300 |
|
| 301 |
static int unload_module(void) |
| 302 |
{ |
| 303 |
if (ast_cdr_unregister(name)) { |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
if (dsn) { |
| 308 |
ast_free(dsn); |
| 309 |
} |
| 310 |
if (table) { |
| 311 |
ast_free(table); |
| 312 |
} |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
static int reload(void) |
| 318 |
{ |
| 319 |
return odbc_load_module(1); |
| 320 |
} |
| 321 |
|
| 322 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CDR Backend", |
| 323 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 324 |
.load = load_module, |
| 325 |
.unload = unload_module, |
| 326 |
.reload = reload, |
| 327 |
.load_pri = AST_MODPRI_CDR_DRIVER, |
| 328 |
.requires = "cdr,res_odbc", |
| 329 |
); |