2 * Asterisk -- A telephony toolkit for Linux.
6 * James Sharp <jsharp@psychoses.org>
9 * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License.
16 #include <sys/types.h>
17 #include <asterisk/config.h>
18 #include <asterisk/options.h>
19 #include <asterisk/channel.h>
20 #include <asterisk/cdr.h>
21 #include <asterisk/module.h>
22 #include <asterisk/logger.h>
23 #include "../asterisk.h"
33 #include <mysql/errmsg.h>
35 #define DATE_FORMAT "%Y-%m-%d %T"
37 static char *desc = "MySQL CDR Backend";
38 static char *name = "mysql";
39 static char *config = "cdr_mysql.conf";
40 static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL;
41 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0;
42 static int dbport = 0;
43 static int connected = 0;
45 static ast_mutex_t mysql_lock = AST_MUTEX_INITIALIZER;
49 static int mysql_log(struct ast_cdr *cdr)
53 char sqlcmd[2048], timestr[128];
56 ast_mutex_lock(&mysql_lock);
58 memset(sqlcmd,0,2048);
60 gettimeofday(&tv,NULL);
63 strftime(timestr,128,DATE_FORMAT,&tm);
65 if ((!connected) && (hostname || dbsock) && dbuser && password && dbname) {
66 /* Attempt to connect */
68 if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
71 ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s. Call will not be logged\n", hostname);
74 /* Long connection - ping the server */
76 if ((error = mysql_ping(&mysql))) {
79 case CR_SERVER_GONE_ERROR:
80 ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n");
83 ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error\n");
89 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL, *uniqueid=NULL;
91 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
92 if (clid = alloca(strlen(cdr->clid) * 2 + 1))
93 mysql_real_escape_string(&mysql, clid, cdr->clid, strlen(cdr->clid));
94 if (dcontext = alloca(strlen(cdr->dcontext) * 2 + 1))
95 mysql_real_escape_string(&mysql, dcontext, cdr->dcontext, strlen(cdr->dcontext));
96 if (channel = alloca(strlen(cdr->channel) * 2 + 1))
97 mysql_real_escape_string(&mysql, channel, cdr->channel, strlen(cdr->channel));
98 if (dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1))
99 mysql_real_escape_string(&mysql, dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
100 if (lastapp = alloca(strlen(cdr->lastapp) * 2 + 1))
101 mysql_real_escape_string(&mysql, lastapp, cdr->lastapp, strlen(cdr->lastapp));
102 if (lastdata = alloca(strlen(cdr->lastdata) * 2 + 1))
103 mysql_real_escape_string(&mysql, lastdata, cdr->lastdata, strlen(cdr->lastdata));
104 #ifdef MYSQL_LOGUNIQUEID
105 if (uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1))
106 mysql_real_escape_string(&mysql, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
109 /* Check for all alloca failures above at once */
110 #ifdef MYSQL_LOGUNIQUEID
111 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
113 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
115 ast_log(LOG_ERROR, "cdr_mysql: Out of memory error (insert fails)\n");
116 ast_mutex_unlock(&mysql_lock);
120 ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n");
122 #ifdef MYSQL_LOGUNIQUEID
123 sprintf(sqlcmd,"INSERT INTO cdr (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')",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);
125 sprintf(sqlcmd,"INSERT INTO cdr (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')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode);
127 ast_log(LOG_DEBUG,"cdr_mysql: SQL command as follows: %s\n",sqlcmd);
129 if (mysql_real_query(&mysql,sqlcmd,strlen(sqlcmd))) {
130 ast_log(LOG_ERROR,"Failed to insert into database.");
131 ast_mutex_unlock(&mysql_lock);
135 ast_mutex_unlock(&mysql_lock);
139 char *description(void)
144 int unload_module(void)
150 if (hostname && hostname_alloc) {
155 if (dbname && dbname_alloc) {
160 if (dbuser && dbuser_alloc) {
165 if (dbsock && dbsock_alloc) {
170 if (password && password_alloc) {
176 ast_cdr_unregister(name);
180 int load_module(void)
183 struct ast_config *cfg;
184 struct ast_variable *var;
187 cfg = ast_load(config);
189 ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
193 var = ast_variable_browse(cfg, "global");
195 /* nothing configured */
199 tmp = ast_variable_retrieve(cfg,"global","hostname");
201 hostname = malloc(strlen(tmp) + 1);
202 if (hostname != NULL) {
204 strcpy(hostname,tmp);
206 ast_log(LOG_ERROR,"Out of memory error.\n");
210 ast_log(LOG_WARNING,"MySQL server hostname not specified. Assuming localhost\n");
211 hostname = "localhost";
214 tmp = ast_variable_retrieve(cfg,"global","dbname");
216 dbname = malloc(strlen(tmp) + 1);
217 if (dbname != NULL) {
221 ast_log(LOG_ERROR,"Out of memory error.\n");
225 ast_log(LOG_WARNING,"MySQL database not specified. Assuming asteriskcdrdb\n");
226 dbname = "asteriskcdrdb";
229 tmp = ast_variable_retrieve(cfg,"global","user");
231 dbuser = malloc(strlen(tmp) + 1);
232 if (dbuser != NULL) {
236 ast_log(LOG_ERROR,"Out of memory error.\n");
240 ast_log(LOG_WARNING,"MySQL database user not specified. Assuming root\n");
244 tmp = ast_variable_retrieve(cfg,"global","sock");
246 dbsock = malloc(strlen(tmp) + 1);
247 if (dbsock != NULL) {
251 ast_log(LOG_ERROR,"Out of memory error.\n");
255 ast_log(LOG_WARNING,"MySQL database sock file not specified. Using default\n");
259 tmp = ast_variable_retrieve(cfg,"global","password");
261 password = malloc(strlen(tmp) + 1);
262 if (password != NULL) {
264 strcpy(password,tmp);
266 ast_log(LOG_ERROR,"Out of memory error.\n");
270 ast_log(LOG_WARNING,"MySQL database password not specified. Assuming blank\n");
274 tmp = ast_variable_retrieve(cfg,"global","port");
276 if (sscanf(tmp,"%d",&dbport) < 1) {
277 ast_log(LOG_WARNING,"Invalid MySQL port number. Using default\n");
284 ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
285 ast_log(LOG_DEBUG,"cdr_mysql: got port of %d\n",dbport);
287 ast_log(LOG_DEBUG,"cdr_mysql: got sock file of %s\n",dbsock);
288 ast_log(LOG_DEBUG,"cdr_mysql: got user of %s\n",dbuser);
289 ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
290 ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);
294 if (!mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
295 ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", dbname, hostname);
298 ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n");
302 res = ast_cdr_register(name, desc, mysql_log);
304 ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
312 return load_module();
322 return ASTERISK_GPL_KEY;