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
00029 #define MYSQL_LOGUNIQUEID 1
00030
00031 #include <asterisk.h>
00032
00033 #include <sys/types.h>
00034 #include <asterisk/config.h>
00035 #include <asterisk/options.h>
00036 #include <asterisk/channel.h>
00037 #include <asterisk/cdr.h>
00038 #include <asterisk/module.h>
00039 #include <asterisk/logger.h>
00040 #include <asterisk/cli.h>
00041
00042 #include <stdio.h>
00043 #include <string.h>
00044
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 #include <time.h>
00048
00049 #include <mysql/mysql.h>
00050 #include <mysql/errmsg.h>
00051
00052 #include <sys/stat.h>
00053 #include <sys/types.h>
00054 #include <errno.h>
00055
00056 #define DATE_FORMAT "%Y-%m-%d %T"
00057
00058 static char *desc = "MySQL CDR Backend";
00059 static char *name = "mysql";
00060 static char *config = "cdr_mysql.conf";
00061 static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
00062 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
00063 static int dbport = 0;
00064 static int connected = 0;
00065 static time_t connect_time = 0;
00066 static int records = 0;
00067 static int totalrecords = 0;
00068 static int userfield = 0;
00069 static unsigned int timeout = 0;
00070
00071 AST_MUTEX_DEFINE_STATIC(mysql_lock);
00072
00073 static MYSQL mysql;
00074
00075 static char cdr_mysql_status_help[] =
00076 "Usage: cdr mysql status\n"
00077 " Shows current connection status for cdr_mysql\n";
00078
00079 static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
00080 {
00081 if (connected) {
00082 char status[256], status2[100] = "";
00083 int ctime = time(NULL) - connect_time;
00084 if (dbport)
00085 snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
00086 else if (dbsock)
00087 snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
00088 else
00089 snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
00090
00091 if (dbuser && *dbuser)
00092 snprintf(status2, 99, " with username %s", dbuser);
00093 if (dbtable && *dbtable)
00094 snprintf(status2, 99, " using table %s", dbtable);
00095 if (ctime > 31536000) {
00096 ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
00097 } else if (ctime > 86400) {
00098 ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
00099 } else if (ctime > 3600) {
00100 ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
00101 } else if (ctime > 60) {
00102 ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
00103 } else {
00104 ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
00105 }
00106 if (records == totalrecords)
00107 ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
00108 else
00109 ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
00110 return RESULT_SUCCESS;
00111 } else {
00112 ast_cli(fd, "Not currently connected to a MySQL server.\n");
00113 return RESULT_FAILURE;
00114 }
00115 }
00116
00117 static struct ast_cli_entry cdr_mysql_status_cli =
00118 { { "cdr", "mysql", "status", NULL },
00119 handle_cdr_mysql_status, "Show connection status of cdr_mysql",
00120 cdr_mysql_status_help, NULL };
00121
00122 static int mysql_log(struct ast_cdr *cdr)
00123 {
00124 struct tm tm;
00125 struct timeval tv;
00126 struct ast_module_user *u;
00127 char *userfielddata = NULL;
00128 char sqlcmd[2048], timestr[128];
00129 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL, *src=NULL, *dst=NULL, *accountcode=NULL;
00130 int retries = 5;
00131 #ifdef MYSQL_LOGUNIQUEID
00132 char *uniqueid = NULL;
00133 #endif
00134 #if MYSQL_VERSION_ID >= 50013
00135 my_bool my_bool_true = 1;
00136 #endif
00137
00138 ast_mutex_lock(&mysql_lock);
00139
00140 memset(sqlcmd, 0, 2048);
00141
00142 ast_localtime(&cdr->start.tv_sec, &tm, NULL);
00143 strftime(timestr, 128, DATE_FORMAT, &tm);
00144
00145 db_reconnect:
00146 if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
00147
00148 mysql_init(&mysql);
00149
00150 if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
00151 ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
00152 }
00153 #if MYSQL_VERSION_ID >= 50013
00154
00155 if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
00156 ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
00157 }
00158 #endif
00159 if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
00160 connected = 1;
00161 connect_time = time(NULL);
00162 records = 0;
00163 } else {
00164 ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.\n", hostname);
00165 connected = 0;
00166 }
00167 } else {
00168
00169 int error;
00170 if ((error = mysql_ping(&mysql))) {
00171 connected = 0;
00172 records = 0;
00173 switch (mysql_errno(&mysql)) {
00174 case CR_SERVER_GONE_ERROR:
00175 case CR_SERVER_LOST:
00176 ast_log(LOG_ERROR, "cdr_mysql: Server has gone away. Attempting to reconnect.\n");
00177 break;
00178 default:
00179 ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
00180 }
00181 retries--;
00182 if (retries)
00183 goto db_reconnect;
00184 else
00185 ast_log(LOG_ERROR, "cdr_mysql: Retried to connect fives times, giving up.\n");
00186 }
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
00197 mysql_escape_string(clid, cdr->clid, strlen(cdr->clid));
00198 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
00199 mysql_escape_string(dcontext, cdr->dcontext, strlen(cdr->dcontext));
00200 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
00201 mysql_escape_string(channel, cdr->channel, strlen(cdr->channel));
00202 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
00203 mysql_escape_string(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
00204 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
00205 mysql_escape_string(lastapp, cdr->lastapp, strlen(cdr->lastapp));
00206 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
00207 mysql_escape_string(lastdata, cdr->lastdata, strlen(cdr->lastdata));
00208 if ((src = alloca(strlen(cdr->src) * 2 + 1)) != NULL)
00209 mysql_escape_string(src, cdr->src, strlen(cdr->src));
00210 if ((dst = alloca(strlen(cdr->dst) * 2 + 1)) != NULL)
00211 mysql_escape_string(dst, cdr->dst, strlen(cdr->dst));
00212 if ((accountcode = alloca(strlen(cdr->accountcode) * 2 + 1)) != NULL)
00213 mysql_escape_string(accountcode, cdr->accountcode, strlen(cdr->accountcode));
00214 #ifdef MYSQL_LOGUNIQUEID
00215 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
00216 mysql_escape_string(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
00217 #endif
00218 if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
00219 mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield));
00220
00221
00222 #ifdef MYSQL_LOGUNIQUEID
00223 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || !(src) || (!dst) || (!accountcode)) {
00224 #else
00225 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || !(src) || (!dst) || (!accountcode)) {
00226 #endif
00227 ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
00228 ast_mutex_unlock(&mysql_lock);
00229 return -1;
00230 }
00231
00232 if (option_debug)
00233 ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n");
00234
00235 if (userfield && userfielddata) {
00236 #ifdef MYSQL_LOGUNIQUEID
00237 sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid, userfielddata);
00238 #else
00239 sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, userfielddata);
00240 #endif
00241 } else {
00242 #ifdef MYSQL_LOGUNIQUEID
00243 sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode, uniqueid);
00244 #else
00245 sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, accountcode);
00246 #endif
00247 }
00248
00249 if (option_debug)
00250 ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd);
00251
00252 if (connected) {
00253 if (mysql_real_query(&mysql, sqlcmd, strlen(sqlcmd))) {
00254 ast_log(LOG_ERROR, "mysql_cdr: Failed to insert into database: (%d) %s", mysql_errno(&mysql), mysql_error(&mysql));
00255 mysql_close(&mysql);
00256 connected = 0;
00257 } else {
00258 records++;
00259 totalrecords++;
00260 }
00261 }
00262 ast_mutex_unlock(&mysql_lock);
00263 return 0;
00264 }
00265
00266 static int my_unload_module(void)
00267 {
00268 ast_cli_unregister(&cdr_mysql_status_cli);
00269 if (connected) {
00270 mysql_close(&mysql);
00271 connected = 0;
00272 records = 0;
00273 }
00274 if (hostname && hostname_alloc) {
00275 free(hostname);
00276 hostname = NULL;
00277 hostname_alloc = 0;
00278 }
00279 if (dbname && dbname_alloc) {
00280 free(dbname);
00281 dbname = NULL;
00282 dbname_alloc = 0;
00283 }
00284 if (dbuser && dbuser_alloc) {
00285 free(dbuser);
00286 dbuser = NULL;
00287 dbuser_alloc = 0;
00288 }
00289 if (dbsock && dbsock_alloc) {
00290 free(dbsock);
00291 dbsock = NULL;
00292 dbsock_alloc = 0;
00293 }
00294 if (dbtable && dbtable_alloc) {
00295 free(dbtable);
00296 dbtable = NULL;
00297 dbtable_alloc = 0;
00298 }
00299 if (password && password_alloc) {
00300 free(password);
00301 password = NULL;
00302 password_alloc = 0;
00303 }
00304 dbport = 0;
00305 ast_cdr_unregister(name);
00306 return 0;
00307 }
00308
00309 static int my_load_module(void)
00310 {
00311 int res;
00312 struct ast_config *cfg;
00313 struct ast_variable *var;
00314 const char *tmp;
00315 #if MYSQL_VERSION_ID >= 50013
00316 my_bool my_bool_true = 1;
00317 #endif
00318
00319 cfg = ast_config_load(config);
00320 if (!cfg) {
00321 ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
00322 return 0;
00323 }
00324
00325 var = ast_variable_browse(cfg, "global");
00326 if (!var) {
00327
00328 return 0;
00329 }
00330
00331 tmp = ast_variable_retrieve(cfg, "global", "hostname");
00332 if (tmp) {
00333 hostname = malloc(strlen(tmp) + 1);
00334 if (hostname != NULL) {
00335 hostname_alloc = 1;
00336 strcpy(hostname, tmp);
00337 } else {
00338 ast_log(LOG_ERROR, "Out of memory error.\n");
00339 return -1;
00340 }
00341 } else {
00342 ast_log(LOG_WARNING, "MySQL server hostname not specified. Assuming localhost\n");
00343 hostname = "localhost";
00344 }
00345
00346 tmp = ast_variable_retrieve(cfg, "global", "dbname");
00347 if (tmp) {
00348 dbname = malloc(strlen(tmp) + 1);
00349 if (dbname != NULL) {
00350 dbname_alloc = 1;
00351 strcpy(dbname, tmp);
00352 } else {
00353 ast_log(LOG_ERROR, "Out of memory error.\n");
00354 return -1;
00355 }
00356 } else {
00357 ast_log(LOG_WARNING, "MySQL database not specified. Assuming asteriskcdrdb\n");
00358 dbname = "asteriskcdrdb";
00359 }
00360
00361 tmp = ast_variable_retrieve(cfg, "global", "user");
00362 if (tmp) {
00363 dbuser = malloc(strlen(tmp) + 1);
00364 if (dbuser != NULL) {
00365 dbuser_alloc = 1;
00366 strcpy(dbuser, tmp);
00367 } else {
00368 ast_log(LOG_ERROR, "Out of memory error.\n");
00369 return -1;
00370 }
00371 } else {
00372 ast_log(LOG_WARNING, "MySQL database user not specified. Assuming root\n");
00373 dbuser = "root";
00374 }
00375
00376 tmp = ast_variable_retrieve(cfg, "global", "sock");
00377 if (tmp) {
00378 dbsock = malloc(strlen(tmp) + 1);
00379 if (dbsock != NULL) {
00380 dbsock_alloc = 1;
00381 strcpy(dbsock, tmp);
00382 } else {
00383 ast_log(LOG_ERROR, "Out of memory error.\n");
00384 return -1;
00385 }
00386 } else {
00387 ast_log(LOG_WARNING, "MySQL database sock file not specified. Using default\n");
00388 dbsock = NULL;
00389 }
00390
00391 tmp = ast_variable_retrieve(cfg, "global", "table");
00392 if (tmp) {
00393 dbtable = malloc(strlen(tmp) + 1);
00394 if (dbtable != NULL) {
00395 dbtable_alloc = 1;
00396 strcpy(dbtable, tmp);
00397 } else {
00398 ast_log(LOG_ERROR, "Out of memory error.\n");
00399 return -1;
00400 }
00401 } else {
00402 ast_log(LOG_NOTICE, "MySQL database table not specified. Assuming \"cdr\"\n");
00403 dbtable = "cdr";
00404 }
00405
00406 tmp = ast_variable_retrieve(cfg, "global", "password");
00407 if (tmp) {
00408 password = malloc(strlen(tmp) + 1);
00409 if (password != NULL) {
00410 password_alloc = 1;
00411 strcpy(password, tmp);
00412 } else {
00413 ast_log(LOG_ERROR, "Out of memory error.\n");
00414 return -1;
00415 }
00416 } else {
00417 ast_log(LOG_WARNING, "MySQL database password not specified. Assuming blank\n");
00418 password = "";
00419 }
00420
00421 tmp = ast_variable_retrieve(cfg, "global", "port");
00422 if (tmp) {
00423 if (sscanf(tmp, "%d", &dbport) < 1) {
00424 ast_log(LOG_WARNING, "Invalid MySQL port number. Using default\n");
00425 dbport = 0;
00426 }
00427 }
00428
00429 tmp = ast_variable_retrieve(cfg, "global", "timeout");
00430 if (tmp) {
00431 if (sscanf(tmp,"%d", &timeout) < 1) {
00432 ast_log(LOG_WARNING, "Invalid MySQL timeout number. Using default\n");
00433 timeout = 0;
00434 }
00435 }
00436
00437 tmp = ast_variable_retrieve(cfg, "global", "userfield");
00438 if (tmp) {
00439 if (sscanf(tmp, "%d", &userfield) < 1) {
00440 ast_log(LOG_WARNING, "Invalid MySQL configuration file\n");
00441 userfield = 0;
00442 }
00443 }
00444
00445 ast_config_destroy(cfg);
00446
00447 if (option_debug) {
00448 ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);
00449 ast_log(LOG_DEBUG, "cdr_mysql: got port of %d\n", dbport);
00450 ast_log(LOG_DEBUG, "cdr_mysql: got a timeout of %d\n", timeout);
00451 if (dbsock)
00452 ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
00453 ast_log(LOG_DEBUG, "cdr_mysql: got user of %s\n", dbuser);
00454 ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname);
00455 ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password);
00456 }
00457
00458 mysql_init(&mysql);
00459
00460 if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
00461 ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
00462 }
00463
00464 #if MYSQL_VERSION_ID >= 50013
00465
00466 if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
00467 ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
00468 }
00469 #endif
00470
00471 if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
00472 ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
00473 connected = 0;
00474 records = 0;
00475 } else {
00476 if (option_debug)
00477 ast_log(LOG_DEBUG, "Successfully connected to MySQL database.\n");
00478 connected = 1;
00479 records = 0;
00480 connect_time = time(NULL);
00481 }
00482
00483 res = ast_cdr_register(name, desc, mysql_log);
00484 if (res) {
00485 ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
00486 } else {
00487 res = ast_cli_register(&cdr_mysql_status_cli);
00488 }
00489
00490 return res;
00491 }
00492
00493 static int load_module(void)
00494 {
00495 return my_load_module();
00496 }
00497
00498 static int unload_module(void)
00499 {
00500 return my_unload_module();
00501 }
00502
00503 static int reload(void)
00504 {
00505 int ret;
00506
00507 ast_mutex_lock(&mysql_lock);
00508 my_unload_module();
00509 ret = my_load_module();
00510 ast_mutex_unlock(&mysql_lock);
00511
00512 return ret;
00513 }
00514
00515 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MySQL CDR Backend");
00516