1 diff -Nru asterisk-1.2.14.org/cdr/cdr_mysql.c asterisk-1.2.14/cdr/cdr_mysql.c
2 --- asterisk-1.2.14.org/cdr/cdr_mysql.c 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.14/cdr/cdr_mysql.c 2006-12-27 09:02:18.000000000 +0100
6 + * Asterisk -- A telephony toolkit for Linux.
10 + * James Sharp <jsharp@psychoses.org>
12 + * Modified August 2003
13 + * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
15 + * Modified August 6, 2005
16 + * Joseph Benden <joe@thrallingpenguin.com>
17 + * Added mysql connection timeout parameter
18 + * Added an automatic reconnect as to not lose a cdr record
19 + * Cleaned up the original code to match the coding guidelines
21 + * This program is free software, distributed under the terms of
22 + * the GNU General Public License.
26 +#include <sys/types.h>
38 +#include <sys/stat.h>
39 +#include <sys/types.h>
42 +#include <asterisk/config.h>
43 +#include <asterisk/options.h>
44 +#include <asterisk/channel.h>
45 +#include <asterisk/cdr.h>
46 +#include <asterisk/module.h>
47 +#include <asterisk/logger.h>
48 +#include <asterisk/cli.h>
50 +#define DATE_FORMAT "%Y-%m-%d %T"
52 +static char *desc = "MySQL CDR Backend";
53 +static char *name = "mysql";
54 +static char *config = "cdr_mysql.conf";
55 +static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
56 +static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
57 +static int dbport = 0;
58 +static int connected = 0;
59 +static time_t connect_time = 0;
60 +static int records = 0;
61 +static int totalrecords = 0;
62 +static int userfield = 0;
63 +static unsigned int timeout = 0;
65 +AST_MUTEX_DEFINE_STATIC(mysql_lock);
69 +static char cdr_mysql_status_help[] =
70 +"Usage: cdr mysql status\n"
71 +" Shows current connection status for cdr_mysql\n";
73 +static int handle_cdr_mysql_status(int fd, int argc, char *argv[])
76 + char status[256], status2[100] = "";
77 + int ctime = time(NULL) - connect_time;
79 + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, hostname, dbport);
81 + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
83 + snprintf(status, 255, "Connected to %s@%s", dbname, hostname);
85 + if (dbuser && *dbuser)
86 + snprintf(status2, 99, " with username %s", dbuser);
87 + if (dbtable && *dbtable)
88 + snprintf(status2, 99, " using table %s", dbtable);
89 + if (ctime > 31536000) {
90 + 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);
91 + } else if (ctime > 86400) {
92 + 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);
93 + } else if (ctime > 3600) {
94 + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
95 + } else if (ctime > 60) {
96 + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
98 + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
100 + if (records == totalrecords)
101 + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords);
103 + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
104 + return RESULT_SUCCESS;
106 + ast_cli(fd, "Not currently connected to a MySQL server.\n");
107 + return RESULT_FAILURE;
111 +static struct ast_cli_entry cdr_mysql_status_cli =
112 + { { "cdr", "mysql", "status", NULL },
113 + handle_cdr_mysql_status, "Show connection status of cdr_mysql",
114 + cdr_mysql_status_help, NULL };
116 +static int mysql_log(struct ast_cdr *cdr)
120 + struct localuser *u;
121 + char *userfielddata = NULL;
122 + char sqlcmd[2048], timestr[128];
123 + char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
125 +#ifdef MYSQL_LOGUNIQUEID
126 + char *uniqueid = NULL;
129 + ast_mutex_lock(&mysql_lock);
131 + memset(sqlcmd, 0, 2048);
133 + localtime_r(&cdr->start.tv_sec, &tm);
134 + strftime(timestr, 128, DATE_FORMAT, &tm);
137 + if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
138 + /* Attempt to connect */
139 + mysql_init(&mysql);
140 + /* Add option to quickly timeout the connection */
141 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
142 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
144 + if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
146 + connect_time = time(NULL);
149 + ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.\n", hostname);
153 + /* Long connection - ping the server */
155 + if ((error = mysql_ping(&mysql))) {
159 + case CR_SERVER_GONE_ERROR:
160 + case CR_SERVER_LOST:
161 + ast_log(LOG_ERROR, "cdr_mysql: Server has gone away. Attempting to reconnect.\n");
164 + ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
170 + ast_log(LOG_ERROR, "cdr_mysql: Retried to connect fives times, giving up.\n");
174 + /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
175 + /* WARNING: This code previously used mysql_real_escape_string, but the use of said function
176 + requires an active connection to a database. If we are not connected, then this function
177 + cannot be used. This is a problem since we need to store off the SQL statement into our
178 + spool file for later restoration.
179 + So the question is, what's the best way to handle this? This works for now.
181 + if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
182 + mysql_escape_string(clid, cdr->clid, strlen(cdr->clid));
183 + if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
184 + mysql_escape_string(dcontext, cdr->dcontext, strlen(cdr->dcontext));
185 + if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
186 + mysql_escape_string(channel, cdr->channel, strlen(cdr->channel));
187 + if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
188 + mysql_escape_string(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
189 + if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
190 + mysql_escape_string(lastapp, cdr->lastapp, strlen(cdr->lastapp));
191 + if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
192 + mysql_escape_string(lastdata, cdr->lastdata, strlen(cdr->lastdata));
193 +#ifdef MYSQL_LOGUNIQUEID
194 + if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
195 + mysql_escape_string(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
197 + if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
198 + mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield));
200 + /* Check for all alloca failures above at once */
201 +#ifdef MYSQL_LOGUNIQUEID
202 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
204 + if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
206 + ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
207 + ast_mutex_unlock(&mysql_lock);
211 + ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n");
213 + if (userfield && userfielddata) {
214 +#ifdef MYSQL_LOGUNIQUEID
215 + 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);
217 + 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);
220 +#ifdef MYSQL_LOGUNIQUEID
221 + 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);
223 + 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);
227 + ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd);
230 + if (mysql_real_query(&mysql, sqlcmd, strlen(sqlcmd))) {
231 + ast_log(LOG_ERROR, "mysql_cdr: Failed to insert into database: (%d) %s", mysql_errno(&mysql), mysql_error(&mysql));
238 + ast_mutex_unlock(&mysql_lock);
242 +char *description(void)
247 +static int my_unload_module(void)
249 + ast_cli_unregister(&cdr_mysql_status_cli);
251 + mysql_close(&mysql);
255 + if (hostname && hostname_alloc) {
258 + hostname_alloc = 0;
260 + if (dbname && dbname_alloc) {
265 + if (dbuser && dbuser_alloc) {
270 + if (dbsock && dbsock_alloc) {
275 + if (dbtable && dbtable_alloc) {
280 + if (password && password_alloc) {
283 + password_alloc = 0;
286 + ast_cdr_unregister(name);
290 +static int my_load_module(void)
293 + struct ast_config *cfg;
294 + struct ast_variable *var;
297 + cfg = ast_config_load(config);
299 + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
303 + var = ast_variable_browse(cfg, "global");
305 + /* nothing configured */
309 + tmp = ast_variable_retrieve(cfg, "global", "hostname");
311 + hostname = malloc(strlen(tmp) + 1);
312 + if (hostname != NULL) {
313 + hostname_alloc = 1;
314 + strcpy(hostname, tmp);
316 + ast_log(LOG_ERROR, "Out of memory error.\n");
320 + ast_log(LOG_WARNING, "MySQL server hostname not specified. Assuming localhost\n");
321 + hostname = "localhost";
324 + tmp = ast_variable_retrieve(cfg, "global", "dbname");
326 + dbname = malloc(strlen(tmp) + 1);
327 + if (dbname != NULL) {
329 + strcpy(dbname, tmp);
331 + ast_log(LOG_ERROR, "Out of memory error.\n");
335 + ast_log(LOG_WARNING, "MySQL database not specified. Assuming asteriskcdrdb\n");
336 + dbname = "asteriskcdrdb";
339 + tmp = ast_variable_retrieve(cfg, "global", "user");
341 + dbuser = malloc(strlen(tmp) + 1);
342 + if (dbuser != NULL) {
344 + strcpy(dbuser, tmp);
346 + ast_log(LOG_ERROR, "Out of memory error.\n");
350 + ast_log(LOG_WARNING, "MySQL database user not specified. Assuming root\n");
354 + tmp = ast_variable_retrieve(cfg, "global", "sock");
356 + dbsock = malloc(strlen(tmp) + 1);
357 + if (dbsock != NULL) {
359 + strcpy(dbsock, tmp);
361 + ast_log(LOG_ERROR, "Out of memory error.\n");
365 + ast_log(LOG_WARNING, "MySQL database sock file not specified. Using default\n");
369 + tmp = ast_variable_retrieve(cfg, "global", "table");
371 + dbtable = malloc(strlen(tmp) + 1);
372 + if (dbtable != NULL) {
374 + strcpy(dbtable, tmp);
376 + ast_log(LOG_ERROR, "Out of memory error.\n");
380 + ast_log(LOG_NOTICE, "MySQL database table not specified. Assuming \"cdr\"\n");
384 + tmp = ast_variable_retrieve(cfg, "global", "password");
386 + password = malloc(strlen(tmp) + 1);
387 + if (password != NULL) {
388 + password_alloc = 1;
389 + strcpy(password, tmp);
391 + ast_log(LOG_ERROR, "Out of memory error.\n");
395 + ast_log(LOG_WARNING, "MySQL database password not specified. Assuming blank\n");
399 + tmp = ast_variable_retrieve(cfg, "global", "port");
401 + if (sscanf(tmp, "%d", &dbport) < 1) {
402 + ast_log(LOG_WARNING, "Invalid MySQL port number. Using default\n");
407 + tmp = ast_variable_retrieve(cfg, "global", "timeout");
409 + if (sscanf(tmp,"%d", &timeout) < 1) {
410 + ast_log(LOG_WARNING, "Invalid MySQL timeout number. Using default\n");
415 + tmp = ast_variable_retrieve(cfg, "global", "userfield");
417 + if (sscanf(tmp, "%d", &userfield) < 1) {
418 + ast_log(LOG_WARNING, "Invalid MySQL configurtation file\n");
423 + ast_config_destroy(cfg);
425 + ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);
426 + ast_log(LOG_DEBUG, "cdr_mysql: got port of %d\n", dbport);
427 + ast_log(LOG_DEBUG, "cdr_mysql: got a timeout of %d\n", timeout);
429 + ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
430 + ast_log(LOG_DEBUG, "cdr_mysql: got user of %s\n", dbuser);
431 + ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname);
432 + ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password);
434 + mysql_init(&mysql);
436 + if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {
437 + ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
440 + if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
441 + ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
445 + ast_log(LOG_DEBUG, "Successfully connected to MySQL database.\n");
448 + connect_time = time(NULL);
451 + res = ast_cdr_register(name, desc, mysql_log);
453 + ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
455 + res = ast_cli_register(&cdr_mysql_status_cli);
461 +int load_module(void)
463 + return my_load_module();
466 +int unload_module(void)
468 + return my_unload_module();
475 + ast_mutex_lock(&mysql_lock);
476 + my_unload_module();
477 + ret = my_load_module();
478 + ast_mutex_unlock(&mysql_lock);
485 + /* Simplistic use count */
486 + if (ast_mutex_trylock(&mysql_lock)) {
489 + ast_mutex_unlock(&mysql_lock);
496 + return ASTERISK_GPL_KEY;
498 diff -Nru asterisk-1.2.14.org/configs/cdr_mysql.conf.sample asterisk-1.2.14/configs/cdr_mysql.conf.sample
499 --- asterisk-1.2.14.org/configs/cdr_mysql.conf.sample 1970-01-01 01:00:00.000000000 +0100
500 +++ asterisk-1.2.14/configs/cdr_mysql.conf.sample 2006-12-27 09:02:18.000000000 +0100
503 +; Note - if the database server is hosted on the same machine as the
504 +; asterisk server, you can achieve a local Unix socket connection by
505 +; setting hostname=localhost
507 +; port and sock are both optional parameters. If hostname is specified
508 +; and is not "localhost", then cdr_mysql will attempt to connect to the
509 +; port specified or use the default port. If hostname is not specified
510 +; or if hostname is "localhost", then cdr_mysql will attempt to connect
511 +; to the socket file specified by sock or otherwise use the default socket
515 +;hostname=database.host.name
516 +;dbname=asteriskcdrdb
519 +;user=asteriskcdruser
521 +;sock=/tmp/mysql.sock