Index: cdr_addon_mysql.c =================================================================== RCS file: /usr/cvsroot/asterisk-addons/cdr_addon_mysql.c,v retrieving revision 1.2 diff -u -r1.2 cdr_addon_mysql.c --- cdr_addon_mysql.c 22 Jan 2004 21:37:33 -0000 1.2 +++ cdr_addon_mysql.c 29 Jan 2004 17:16:54 -0000 @@ -20,6 +20,7 @@ #include #include #include +#include #include "asterisk.h" #include @@ -37,20 +38,70 @@ static char *desc = "MySQL CDR Backend"; static char *name = "mysql"; static char *config = "cdr_mysql.conf"; -static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *userfielddata = NULL; +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL; static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0; static int dbport = 0; static int connected = 0; +static time_t connect_time = 0; +static int records = 0; +static int totalrecords = 0; static int userfield = 0; static ast_mutex_t mysql_lock = AST_MUTEX_INITIALIZER; static MYSQL mysql; +static char cdr_mysql_status_help[] = +"Usage: cdr mysql status\n" +" Shows current connection status for cdr_mysql\n"; + +static int handle_cdr_mysql_status(int fd, int argc, char *argv[]) +{ + if (connected) { + char status[256], status2[100] = ""; + int ctime = time(NULL) - connect_time; + if (dbport) + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport); + else if (dbsock) + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock); + else + snprintf(status, 255, "Connected to %s@%s", dbname, hostname); + + if (dbuser && *dbuser) + snprintf(status2, 99, " with username %s", dbuser); + if (ctime > 31536000) { + 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); + } else if (ctime > 86400) { + 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); + } else if (ctime > 3600) { + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60); + } else if (ctime > 60) { + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60); + } else { + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime); + } + if (records == totalrecords) + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords); + else + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records); + return RESULT_SUCCESS; + } else { + ast_cli(fd, "Not currently connected to a MySQL server.\n"); + return RESULT_FAILURE; + } +} + +static struct ast_cli_entry cdr_mysql_status_cli = + { { "cdr", "mysql", "status", NULL }, + handle_cdr_mysql_status, "Show connection status of cdr_mysql", + cdr_mysql_status_help, NULL }; + static int mysql_log(struct ast_cdr *cdr) { struct tm tm; struct timeval tv; + struct localuser *u; + char *userfielddata = NULL; char sqlcmd[2048], timestr[128]; time_t t; @@ -68,6 +119,8 @@ mysql_init(&mysql); if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) { connected = 1; + connect_time = time(NULL); + records = 0; } else { ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s. Call will not be logged\n", hostname); } @@ -76,6 +129,7 @@ int error; if ((error = mysql_ping(&mysql))) { connected = 0; + records = 0; switch (error) { case CR_SERVER_GONE_ERROR: ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n"); @@ -126,7 +180,7 @@ ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n"); - if (userfield) + if (userfield && userfielddata) { #ifdef MYSQL_LOGUNIQUEID sprintf(sqlcmd,"INSERT INTO cdr (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')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfielddata); @@ -149,6 +203,9 @@ ast_log(LOG_ERROR,"Failed to insert into database."); ast_mutex_unlock(&mysql_lock); return -1; + } else { + records++; + totalrecords++; } } ast_mutex_unlock(&mysql_lock); @@ -162,9 +219,11 @@ static int my_unload_module(void) { + ast_cli_unregister(&cdr_mysql_status_cli); if (connected) { mysql_close(&mysql); connected = 0; + records = 0; } if (hostname && hostname_alloc) { free(hostname); @@ -321,15 +380,21 @@ if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) { ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname); connected = 0; + records = 0; } else { ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n"); connected = 1; + records = 0; + connect_time = time(NULL); } res = ast_cdr_register(name, desc, mysql_log); if (res) { ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n"); + } else { + res = ast_cli_register(&cdr_mysql_status_cli); } + return res; } @@ -351,7 +416,13 @@ int usecount(void) { - return connected; + /* Simplistic use count */ + if (ast_mutex_trylock(&mysql_lock)) { + return 1; + } else { + ast_mutex_unlock(&mysql_lock); + return 0; + } } char *key()