b5cc8e51a725b73645d4574515dad36362cb64b5
[asterisk/asterisk.git] / cdr / cdr_mysql.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * MySQL CDR logger 
5  * 
6  * James Sharp <jsharp@psychoses.org>
7  *
8  * Modified August 2003
9  * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License.
13  *
14  */
15
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"
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <time.h>
31
32 #include <mysql.h>
33 #include <errmsg.h>
34
35 #define DATE_FORMAT "%Y-%m-%d %T"
36
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;
44
45 static ast_mutex_t mysql_lock = AST_MUTEX_INITIALIZER;
46
47 static MYSQL mysql;
48
49 static int mysql_log(struct ast_cdr *cdr)
50 {
51         struct tm tm;
52         struct timeval tv;
53         char sqlcmd[2048], timestr[128];
54         time_t t;
55
56         ast_mutex_lock(&mysql_lock);
57
58         memset(sqlcmd,0,2048);
59
60         gettimeofday(&tv,NULL);
61         t = tv.tv_sec;
62         localtime_r(&t,&tm);
63         strftime(timestr,128,DATE_FORMAT,&tm);
64
65         if ((!connected) && (hostname || dbsock) && dbuser && password && dbname) {
66                 /* Attempt to connect */
67                 mysql_init(&mysql);
68                 if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
69                         connected = 1;
70                 } else {
71                         ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s.  Call will not be logged\n", hostname);
72                 }
73         } else {
74                 /* Long connection - ping the server */
75                 int error;
76                 if ((error = mysql_ping(&mysql))) {
77                         connected = 0;
78                         switch (error) {
79                                 case CR_SERVER_GONE_ERROR:
80                                         ast_log(LOG_ERROR, "cdr_mysql: Server has gone away\n");
81                                         break;
82                                 default:
83                                         ast_log(LOG_ERROR, "cdr_mysql: Unknown connection error\n");
84                         }
85                 }
86         }
87
88         if (connected) {
89                 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL, *uniqueid=NULL;
90
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));
107 #endif
108
109                 /* Check for all alloca failures above at once */
110 #ifdef MYSQL_LOGUNIQUEID
111                 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
112 #else
113                 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
114 #endif
115                         ast_log(LOG_ERROR, "cdr_mysql:  Out of memory error (insert fails)\n");
116                         ast_mutex_unlock(&mysql_lock);
117                         return -1;
118                 }
119
120                 ast_log(LOG_DEBUG,"cdr_mysql: inserting a CDR record.\n");
121
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);
124 #else
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);
126 #endif  
127                 ast_log(LOG_DEBUG,"cdr_mysql: SQL command as follows:  %s\n",sqlcmd);
128         
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);
132                         return -1;
133                 }
134         }
135         ast_mutex_unlock(&mysql_lock);
136         return 0;
137 }
138
139 char *description(void)
140 {
141         return desc;
142 }
143
144 int my_unload_module(void)
145
146         if (connected) {
147                 mysql_close(&mysql);
148                 connected = 0;
149         }
150         if (hostname && hostname_alloc) {
151                 free(hostname);
152                 hostname = NULL;
153                 hostname_alloc = 0;
154         }
155         if (dbname && dbname_alloc) {
156                 free(dbname);
157                 dbname = NULL;
158                 dbname_alloc = 0;
159         }
160         if (dbuser && dbuser_alloc) {
161                 free(dbuser);
162                 dbuser = NULL;
163                 dbuser_alloc = 0;
164         }
165         if (dbsock && dbsock_alloc) {
166                 free(dbsock);
167                 dbsock = NULL;
168                 dbsock_alloc = 0;
169         }
170         if (password && password_alloc) {
171                 free(password);
172                 password = NULL;
173                 password_alloc = 0;
174         }
175         dbport = 0;
176         ast_cdr_unregister(name);
177         return 0;
178 }
179
180 int my_load_module(void)
181 {
182         int res;
183         struct ast_config *cfg;
184         struct ast_variable *var;
185         char *tmp;
186
187         cfg = ast_load(config);
188         if (!cfg) {
189                 ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
190                 return 0;
191         }
192         
193         var = ast_variable_browse(cfg, "global");
194         if (!var) {
195                 /* nothing configured */
196                 return 0;
197         }
198
199         tmp = ast_variable_retrieve(cfg,"global","hostname");
200         if (tmp) {
201                 hostname = malloc(strlen(tmp) + 1);
202                 if (hostname != NULL) {
203                         hostname_alloc = 1;
204                         strcpy(hostname,tmp);
205                 } else {
206                         ast_log(LOG_ERROR,"Out of memory error.\n");
207                         return -1;
208                 }
209         } else {
210                 ast_log(LOG_WARNING,"MySQL server hostname not specified.  Assuming localhost\n");
211                 hostname = "localhost";
212         }
213
214         tmp = ast_variable_retrieve(cfg,"global","dbname");
215         if (tmp) {
216                 dbname = malloc(strlen(tmp) + 1);
217                 if (dbname != NULL) {
218                         dbname_alloc = 1;
219                         strcpy(dbname,tmp);
220                 } else {
221                         ast_log(LOG_ERROR,"Out of memory error.\n");
222                         return -1;
223                 }
224         } else {
225                 ast_log(LOG_WARNING,"MySQL database not specified.  Assuming asteriskcdrdb\n");
226                 dbname = "asteriskcdrdb";
227         }
228
229         tmp = ast_variable_retrieve(cfg,"global","user");
230         if (tmp) {
231                 dbuser = malloc(strlen(tmp) + 1);
232                 if (dbuser != NULL) {
233                         dbuser_alloc = 1;
234                         strcpy(dbuser,tmp);
235                 } else {
236                         ast_log(LOG_ERROR,"Out of memory error.\n");
237                         return -1;
238                 }
239         } else {
240                 ast_log(LOG_WARNING,"MySQL database user not specified.  Assuming root\n");
241                 dbuser = "root";
242         }
243
244         tmp = ast_variable_retrieve(cfg,"global","sock");
245         if (tmp) {
246                 dbsock = malloc(strlen(tmp) + 1);
247                 if (dbsock != NULL) {
248                         dbsock_alloc = 1;
249                         strcpy(dbsock,tmp);
250                 } else {
251                         ast_log(LOG_ERROR,"Out of memory error.\n");
252                         return -1;
253                 }
254         } else {
255                 ast_log(LOG_WARNING,"MySQL database sock file not specified.  Using default\n");
256                 dbsock = NULL;
257         }
258
259         tmp = ast_variable_retrieve(cfg,"global","password");
260         if (tmp) {
261                 password = malloc(strlen(tmp) + 1);
262                 if (password != NULL) {
263                         password_alloc = 1;
264                         strcpy(password,tmp);
265                 } else {
266                         ast_log(LOG_ERROR,"Out of memory error.\n");
267                         return -1;
268                 }
269         } else {
270                 ast_log(LOG_WARNING,"MySQL database password not specified.  Assuming blank\n");
271                 password = "";
272         }
273
274         tmp = ast_variable_retrieve(cfg,"global","port");
275         if (tmp) {
276                 if (sscanf(tmp,"%d",&dbport) < 1) {
277                         ast_log(LOG_WARNING,"Invalid MySQL port number.  Using default\n");
278                         dbport = 0;
279                 }
280         }
281
282         ast_destroy(cfg);
283
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);
286         if (dbsock)
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);
291
292         mysql_init(&mysql);
293
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);
296                 connected = 0;
297         } else {
298                 ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n");
299                 connected = 1;
300         }
301
302         res = ast_cdr_register(name, desc, mysql_log);
303         if (res) {
304                 ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
305         }
306         return res;
307 }
308
309 int load_module(void)
310 {
311         return my_load_module();
312 }
313
314 int unload_module(void)
315 {
316         return my_unload_module();
317 }
318
319 int reload(void)
320 {
321         my_unload_module();
322         return my_load_module();
323 }
324
325 int usecount(void)
326 {
327         return connected;
328 }
329
330 char *key()
331 {
332         return ASTERISK_GPL_KEY;
333 }