1 diff -ruN asterisk-1.2.0-old/configs/cdr_mysql.conf.sample asterisk-1.2.0-new/configs/cdr_mysql.conf.sample
2 --- asterisk-1.2.0-old/configs/cdr_mysql.conf.sample 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.0-new/configs/cdr_mysql.conf.sample 2005-01-21 02:43:20.000000000 +0100
6 +; Note - if the database server is hosted on the same machine as the
7 +; asterisk server, you can achieve a local Unix socket connection by
8 +; setting hostname=localhost
10 +; port and sock are both optional parameters. If hostname is specified
11 +; and is not "localhost", then cdr_mysql will attempt to connect to the
12 +; port specified or use the default port. If hostname is not specified
13 +; or if hostname is "localhost", then cdr_mysql will attempt to connect
14 +; to the socket file specified by sock or otherwise use the default socket
18 +;hostname=database.host.name
19 +;dbname=asteriskcdrdb
22 +;user=asteriskcdruser
24 +;sock=/tmp/mysql.sock
26 diff -ruN asterisk-1.2.0-old/cdr/cdr_mysql.c asterisk-1.2.0-new/cdr/cdr_mysql.c
27 --- asterisk-1.2.0-old/cdr/cdr_mysql.c 1970-01-01 01:00:00.000000000 +0100
28 +++ asterisk-1.2.0-new/cdr/cdr_mysql.c 2005-12-04 20:10:59.000000000 +0100
31 + * Asterisk -- A telephony toolkit for Linux.
35 + * James Sharp <jsharp@psychoses.org>
37 + * Modified August 2003
38 + * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
40 + * Modified August 6, 2005
41 + * Joseph Benden <joe@thrallingpenguin.com>
42 + * Added mysql connection timeout parameter
43 + * Added an automatic reconnect as to not lose a cdr record
44 + * Cleaned up the original code to match the coding guidelines
46 + * This program is free software, distributed under the terms of
47 + * the GNU General Public License.
51 +#include <sys/types.h>
63 +#include <sys/stat.h>
64 +#include <sys/types.h>
67 +#include <asterisk/config.h>
68 +#include <asterisk/options.h>
69 +#include <asterisk/channel.h>
70 +#include <asterisk/cdr.h>
71 +#include <asterisk/module.h>
72 +#include <asterisk/logger.h>
73 +#include <asterisk/cli.h>
75 +#define DATE_FORMAT "%Y-%m-%d %T"
77 +static char *desc = "MySQL CDR Backend";
78 +static char *name = "mysql";
79 +static char *config = "cdr_mysql.conf";
80 +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
81 +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
82 +static int dbport = 0;
83 +static int connected = 0;
84 +static time_t connect_time = 0;
85 +static int records = 0;
86 +static int totalrecords = 0;
87 +static int userfield = 0;
88 +static unsigned int timeout = 0;
90 +AST_MUTEX_DEFINE_STATIC(mysql_lock);
94 +static char cdr_mysql_status_help[] =
95 +"Usage: cdr mysql status\n"
96 +" Shows current connection status for cdr_mysql\n";
98 +static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
101 + char status[256], status2[100] = "";
102 + int ctime = time(NULL) - connect_time;
104 + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
106 + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
108 + snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
110 + if (dbuser && *dbuser)
111 + snprintf(status2, 99, " with username %s", dbuser);
112 + if (dbtable && *dbtable)
113 + snprintf(status2, 99, " using table %s", dbtable);
114 + if (ctime > 31536000) {
115 + 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);
116 + } else if (ctime > 86400) {
117 + 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);
118 + } else if (ctime > 3600) {
119 + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
120 + } else if (ctime > 60) {
121 + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
123 + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
125 + if (records == totalrecords)
126 + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
128 + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
129 + return RESULT_SUCCESS;
131 + ast_cli(fd, "Not currently connected to a MySQL server.\n");
132 + return RESULT_FAILURE;
136 +static struct ast_cli_entry cdr_mysql_status_cli =
137 + { { "cdr", "mysql", "status", NULL },
138 + handle_cdr_mysql_status, "Show connection status of cdr_mysql",
139 + cdr_mysql_status_help, NULL };
141 +static int mysql_log(struct ast_cdr *cdr)
145 + struct localuser *u;
146 + char *userfielddata = NULL;
147 + char sqlcmd[2048], timestr[128];
148 + char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
150 +#ifdef MYSQL_LOGUNIQUEID
151 + char *uniqueid = NULL;
154 + ast_mutex_lock(&mysql_lock);
156 + memset(sqlcmd, 0, 2048);
158 + localtime_r(&cdr->start.tv_sec, &tm);
159 + strftime(timestr, 128, DATE_FORMAT, &tm);
162 + if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
163 + /* Attempt to connect */
164 + mysql_init(&mysql);
165 + /* Add option to quickly timeout the connection */
166 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
167 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
169 + if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
171 + connect_time = time(NULL);
174 + ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.\n", hostname);
178 + /* Long connection - ping the server */
180 + if ((error = mysql_ping(&mysql))) {
184 + case CR_SERVER_GONE_ERROR:
185 + case CR_SERVER_LOST:
186 + ast_log(LOG_ERROR, "cdr_mysql: Server has gone away. Attempting to reconnect.\n");
189 + ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
195 + ast_log(LOG_ERROR, "cdr_mysql: Retried to connect fives times, giving up.\n");
199 + /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
200 + /* WARNING: This code previously used mysql_real_escape_string, but the use of said function
201 + requires an active connection to a database. If we are not connected, then this function
202 + cannot be used. This is a problem since we need to store off the SQL statement into our
203 + spool file for later restoration.
204 + So the question is, what's the best way to handle this? This works for now.
206 + if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
207 + mysql_escape_string(clid, cdr->clid, strlen(cdr->clid));
208 + if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
209 + mysql_escape_string(dcontext, cdr->dcontext, strlen(cdr->dcontext));
210 + if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
211 + mysql_escape_string(channel, cdr->channel, strlen(cdr->channel));
212 + if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
213 + mysql_escape_string(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
214 + if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
215 + mysql_escape_string(lastapp, cdr->lastapp, strlen(cdr->lastapp));
216 + if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
217 + mysql_escape_string(lastdata, cdr->lastdata, strlen(cdr->lastdata));
218 +#ifdef MYSQL_LOGUNIQUEID
219 + if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
220 + mysql_escape_string(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
222 + if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
223 + mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield));
225 + /* Check for all alloca failures above at once */
226 +#ifdef MYSQL_LOGUNIQUEID
227 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
229 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
231 + ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
232 + ast_mutex_unlock(&mysql_lock);
236 + ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n");
238 + if (userfield && userfielddata) {
239 +#ifdef MYSQL_LOGUNIQUEID
240 + 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, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata);
242 + 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, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata);
245 +#ifdef MYSQL_LOGUNIQUEID
246 + 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, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid);
248 + 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, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode);
252 + ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd);
255 + if (mysql_real_query(&mysql, sqlcmd, strlen(sqlcmd))) {
256 + ast_log(LOG_ERROR, "mysql_cdr: Failed to insert into database: (%d) %s", mysql_errno(&mysql), mysql_error(&mysql));
263 + ast_mutex_unlock(&mysql_lock);
267 +char *description(void)
272 +static int my_unload_module(void)
274 + ast_cli_unregister(&cdr_mysql_status_cli);
276 + mysql_close(&mysql);
280 + if (hostname && hostname_alloc) {
283 + hostname_alloc = 0;
285 + if (dbname && dbname_alloc) {
290 + if (dbuser && dbuser_alloc) {
295 + if (dbsock && dbsock_alloc) {
300 + if (dbtable && dbtable_alloc) {
305 + if (password && password_alloc) {
308 + password_alloc = 0;
311 + ast_cdr_unregister(name);
315 +static int my_load_module(void)
318 + struct ast_config *cfg;
319 + struct ast_variable *var;
322 + cfg = ast_config_load(config);
324 + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
328 + var = ast_variable_browse(cfg, "global");
330 + /* nothing configured */
334 + tmp = ast_variable_retrieve(cfg, "global", "hostname");
336 + hostname = malloc(strlen(tmp) + 1);
337 + if (hostname != NULL) {
338 + hostname_alloc = 1;
339 + strcpy(hostname, tmp);
341 + ast_log(LOG_ERROR, "Out of memory error.\n");
345 + ast_log(LOG_WARNING, "MySQL server hostname not specified. Assuming localhost\n");
346 + hostname = "localhost";
349 + tmp = ast_variable_retrieve(cfg, "global", "dbname");
351 + dbname = malloc(strlen(tmp) + 1);
352 + if (dbname != NULL) {
354 + strcpy(dbname, tmp);
356 + ast_log(LOG_ERROR, "Out of memory error.\n");
360 + ast_log(LOG_WARNING, "MySQL database not specified. Assuming asteriskcdrdb\n");
361 + dbname = "asteriskcdrdb";
364 + tmp = ast_variable_retrieve(cfg, "global", "user");
366 + dbuser = malloc(strlen(tmp) + 1);
367 + if (dbuser != NULL) {
369 + strcpy(dbuser, tmp);
371 + ast_log(LOG_ERROR, "Out of memory error.\n");
375 + ast_log(LOG_WARNING, "MySQL database user not specified. Assuming root\n");
379 + tmp = ast_variable_retrieve(cfg, "global", "sock");
381 + dbsock = malloc(strlen(tmp) + 1);
382 + if (dbsock != NULL) {
384 + strcpy(dbsock, tmp);
386 + ast_log(LOG_ERROR, "Out of memory error.\n");
390 + ast_log(LOG_WARNING, "MySQL database sock file not specified. Using default\n");
394 + tmp = ast_variable_retrieve(cfg, "global", "table");
396 + dbtable = malloc(strlen(tmp) + 1);
397 + if (dbtable != NULL) {
399 + strcpy(dbtable, tmp);
401 + ast_log(LOG_ERROR, "Out of memory error.\n");
405 + ast_log(LOG_NOTICE, "MySQL database table not specified. Assuming \"cdr\"\n");
409 + tmp = ast_variable_retrieve(cfg, "global", "password");
411 + password = malloc(strlen(tmp) + 1);
412 + if (password != NULL) {
413 + password_alloc = 1;
414 + strcpy(password, tmp);
416 + ast_log(LOG_ERROR, "Out of memory error.\n");
420 + ast_log(LOG_WARNING, "MySQL database password not specified. Assuming blank\n");
424 + tmp = ast_variable_retrieve(cfg, "global", "port");
426 + if (sscanf(tmp, "%d", &dbport) < 1) {
427 + ast_log(LOG_WARNING, "Invalid MySQL port number. Using default\n");
432 + tmp = ast_variable_retrieve(cfg, "global", "timeout");
434 + if (sscanf(tmp,"%d", &timeout) < 1) {
435 + ast_log(LOG_WARNING, "Invalid MySQL timeout number. Using default\n");
440 + tmp = ast_variable_retrieve(cfg, "global", "userfield");
442 + if (sscanf(tmp, "%d", &userfield) < 1) {
443 + ast_log(LOG_WARNING, "Invalid MySQL configurtation file\n");
448 + ast_config_destroy(cfg);
450 + ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);
451 + ast_log(LOG_DEBUG, "cdr_mysql: got port of %d\n", dbport);
452 + ast_log(LOG_DEBUG, "cdr_mysql: got a timeout of %d\n", timeout);
454 + ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
455 + ast_log(LOG_DEBUG, "cdr_mysql: got user of %s\n", dbuser);
456 + ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname);
457 + ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password);
459 + mysql_init(&mysql);
461 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
462 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
465 + if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
466 + ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
470 + ast_log(LOG_DEBUG, "Successfully connected to MySQL database.\n");
473 + connect_time = time(NULL);
476 + res = ast_cdr_register(name, desc, mysql_log);
478 + ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
480 + res = ast_cli_register(&cdr_mysql_status_cli);
486 +int load_module(void)
488 + return my_load_module();
491 +int unload_module(void)
493 + return my_unload_module();
500 + ast_mutex_lock(&mysql_lock);
501 + my_unload_module();
502 + ret = my_load_module();
503 + ast_mutex_unlock(&mysql_lock);
510 + /* Simplistic use count */
511 + if (ast_mutex_trylock(&mysql_lock)) {
514 + ast_mutex_unlock(&mysql_lock);
521 + return ASTERISK_GPL_KEY;