Fri Jul 3 20:37:00 2009

Asterisk developer's documentation


cdr_addon_mysql.c File Reference

#include <asterisk.h>
#include <sys/types.h>
#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/cdr.h>
#include <asterisk/module.h>
#include <asterisk/logger.h>
#include <asterisk/cli.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
#include <sys/stat.h>
#include <errno.h>

Include dependency graph for cdr_addon_mysql.c:

Go to the source code of this file.

Defines

#define DATE_FORMAT   "%Y-%m-%d %T"
#define MYSQL_LOGUNIQUEID   1

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int handle_cdr_mysql_status (int fd, int argc, char *argv[])
static int load_module (void)
static int my_load_module (void)
static int my_unload_module (void)
static int mysql_log (struct ast_cdr *cdr)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "MySQL CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static const struct
ast_module_info * 
ast_module_info = &__mod_info
static struct ast_cli_entry cdr_mysql_status_cli
static char cdr_mysql_status_help []
static char * config = "cdr_mysql.conf"
static time_t connect_time = 0
static int connected = 0
static char * dbname = NULL
static int dbname_alloc = 0
static int dbport = 0
static char * dbsock = NULL
static int dbsock_alloc = 0
static char * dbtable = NULL
static int dbtable_alloc = 0
static char * dbuser = NULL
static int dbuser_alloc = 0
static char * desc = "MySQL CDR Backend"
static char * hostname = NULL
static int hostname_alloc = 0
static MYSQL mysql
static ast_mutex_t mysql_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static char * name = "mysql"
static char * password = NULL
static int password_alloc = 0
static int records = 0
static unsigned int timeout = 0
static int totalrecords = 0
static int userfield = 0


Define Documentation

#define DATE_FORMAT   "%Y-%m-%d %T"

#define MYSQL_LOGUNIQUEID   1

Definition at line 29 of file cdr_addon_mysql.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 515 of file cdr_addon_mysql.c.

static void __unreg_module ( void   )  [static]

Definition at line 515 of file cdr_addon_mysql.c.

static int handle_cdr_mysql_status ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 79 of file cdr_addon_mysql.c.

References ast_cli(), connect_time, connected, dbname, dbport, dbsock, dbtable, dbuser, hostname, records, RESULT_FAILURE, RESULT_SUCCESS, and totalrecords.

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 }

static int load_module ( void   )  [static]

Definition at line 493 of file cdr_addon_mysql.c.

References my_load_module().

00494 {
00495    return my_load_module();
00496 }

static int my_load_module ( void   )  [static]

Definition at line 309 of file cdr_addon_mysql.c.

References ast_cdr_register(), ast_cli_register(), ast_config_destroy(), ast_config_load(), ast_log(), ast_variable_browse(), ast_variable_retrieve(), config, connect_time, connected, dbname, dbname_alloc, dbport, dbsock, dbsock_alloc, dbtable, dbtable_alloc, dbuser, dbuser_alloc, desc, hostname, hostname_alloc, LOG_DEBUG, LOG_ERROR, LOG_NOTICE, LOG_WARNING, malloc, mysql, mysql_log(), name, option_debug, password, password_alloc, records, timeout, userfield, and var.

Referenced by load_module(), and reload().

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       /* nothing configured */
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    /* Add option for automatic reconnection */
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 }

static int my_unload_module ( void   )  [static]

Definition at line 266 of file cdr_addon_mysql.c.

References ast_cdr_unregister(), ast_cli_unregister(), connected, dbname, dbname_alloc, dbport, dbsock, dbsock_alloc, dbtable, dbtable_alloc, dbuser, dbuser_alloc, free, hostname, hostname_alloc, mysql, name, password, password_alloc, and records.

Referenced by reload(), and unload_module().

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 }

static int mysql_log ( struct ast_cdr *  cdr  )  [static]

Definition at line 122 of file cdr_addon_mysql.c.

References ast_cdr::accountcode, accountcode, ast_cdr::amaflags, ast_cdr_disp2str(), ast_localtime(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, connect_time, connected, DATE_FORMAT, dbname, dbport, dbsock, dbtable, dbuser, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, hostname, ast_cdr::lastapp, ast_cdr::lastdata, LOG_DEBUG, LOG_ERROR, mysql, mysql_lock, option_debug, password, records, ast_cdr::src, ast_cdr::start, timeout, totalrecords, ast_cdr::uniqueid, ast_cdr::userfield, and userfield.

Referenced by my_load_module().

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       /* Attempt to connect */
00148       mysql_init(&mysql);
00149       /* Add option to quickly timeout the connection */
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       /* Add option for automatic reconnection */
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       /* Long connection - ping the server */
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    /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
00190    /* WARNING: This code previously used mysql_real_escape_string, but the use of said function
00191       requires an active connection to a database.  If we are not connected, then this function
00192        cannot be used.  This is a problem since we need to store off the SQL statement into our
00193       spool file for later restoration.
00194       So the question is, what's the best way to handle this?  This works for now.
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    /* Check for all alloca failures above at once */
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 }

static int reload ( void   )  [static]

Definition at line 503 of file cdr_addon_mysql.c.

References ast_mutex_lock(), ast_mutex_unlock(), my_load_module(), my_unload_module(), and mysql_lock.

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 }

static int unload_module ( void   )  [static]

Definition at line 498 of file cdr_addon_mysql.c.

References my_unload_module().

00499 {
00500    return my_unload_module();
00501 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "MySQL CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 515 of file cdr_addon_mysql.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 515 of file cdr_addon_mysql.c.

Initial value:

   { { "cdr", "mysql", "status", NULL },
   handle_cdr_mysql_status, "Show connection status of cdr_mysql",
   cdr_mysql_status_help, NULL }

Definition at line 117 of file cdr_addon_mysql.c.

char cdr_mysql_status_help[] [static]

Initial value:

"Usage: cdr mysql status\n"
"       Shows current connection status for cdr_mysql\n"

Definition at line 75 of file cdr_addon_mysql.c.

char* config = "cdr_mysql.conf" [static]

Definition at line 60 of file cdr_addon_mysql.c.

Referenced by my_load_module().

time_t connect_time = 0 [static]

Definition at line 65 of file cdr_addon_mysql.c.

Referenced by handle_cdr_mysql_status(), my_load_module(), and mysql_log().

int connected = 0 [static]

char * dbname = NULL [static]

int dbname_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

int dbport = 0 [static]

char * dbsock = NULL [static]

int dbsock_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

char * dbtable = NULL [static]

int dbtable_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

char * dbuser = NULL [static]

int dbuser_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

char* desc = "MySQL CDR Backend" [static]

Definition at line 58 of file cdr_addon_mysql.c.

Referenced by my_load_module().

char* hostname = NULL [static]

int hostname_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

MYSQL mysql [static]

Definition at line 73 of file cdr_addon_mysql.c.

Referenced by my_load_module(), my_unload_module(), and mysql_log().

ast_mutex_t mysql_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 71 of file cdr_addon_mysql.c.

Referenced by mysql_log(), and reload().

char* name = "mysql" [static]

Definition at line 59 of file cdr_addon_mysql.c.

char * password = NULL [static]

int password_alloc = 0 [static]

Definition at line 62 of file cdr_addon_mysql.c.

Referenced by my_load_module(), and my_unload_module().

int records = 0 [static]

unsigned int timeout = 0 [static]

int totalrecords = 0 [static]

Definition at line 67 of file cdr_addon_mysql.c.

Referenced by handle_cdr_mysql_status(), and mysql_log().

int userfield = 0 [static]


Generated on Fri Jul 3 20:37:00 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9