Merged revisions 69392 via svnmerge from
[asterisk/asterisk.git] / cdr / cdr_sqlite.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005, Holger Schurig
5  *
6  *
7  * Ideas taken from other cdr_*.c files
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Store CDR records in a SQLite database.
23  * 
24  * \author Holger Schurig <hs4233@mail.mn-solutions.de>
25  * \extref SQLite http://www.sqlite.org/
26  *
27  * See also
28  * \arg \ref Config_cdr
29  * \arg http://www.sqlite.org/
30  * 
31  * Creates the database and table on-the-fly
32  * \ingroup cdr_drivers
33  *
34  * \note This module has been marked deprecated in favor for cdr_sqlite3_custom
35  */
36
37 /*** MODULEINFO
38         <depend>sqlite</depend>
39  ***/
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <sqlite.h>
51
52 #include "asterisk/channel.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/utils.h"
56
57 #define LOG_UNIQUEID    0
58 #define LOG_USERFIELD   0
59
60 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
61 #define DATE_FORMAT "%Y-%m-%d %T"
62
63 static char *name = "sqlite";
64 static sqlite* db = NULL;
65
66 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
67
68 /*! \brief SQL table format */
69 static char sql_create_table[] = "CREATE TABLE cdr ("
70 "       AcctId          INTEGER PRIMARY KEY,"
71 "       clid            VARCHAR(80),"
72 "       src             VARCHAR(80),"
73 "       dst             VARCHAR(80),"
74 "       dcontext        VARCHAR(80),"
75 "       channel         VARCHAR(80),"
76 "       dstchannel      VARCHAR(80),"
77 "       lastapp         VARCHAR(80),"
78 "       lastdata        VARCHAR(80),"
79 "       start           CHAR(19),"
80 "       answer          CHAR(19),"
81 "       end             CHAR(19),"
82 "       duration        INTEGER,"
83 "       billsec         INTEGER,"
84 "       disposition     INTEGER,"
85 "       amaflags        INTEGER,"
86 "       accountcode     VARCHAR(20)"
87 #if LOG_UNIQUEID
88 "       ,uniqueid       VARCHAR(32)"
89 #endif
90 #if LOG_USERFIELD
91 "       ,userfield      VARCHAR(255)"
92 #endif
93 ");";
94
95 static int sqlite_log(struct ast_cdr *cdr)
96 {
97         int res = 0;
98         char *zErr = 0;
99         struct tm tm;
100         time_t t;
101         char startstr[80], answerstr[80], endstr[80];
102         int count;
103
104         ast_mutex_lock(&sqlite_lock);
105
106         t = cdr->start.tv_sec;
107         ast_localtime(&t, &tm, NULL);
108         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
109
110         t = cdr->answer.tv_sec;
111         ast_localtime(&t, &tm, NULL);
112         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
113
114         t = cdr->end.tv_sec;
115         ast_localtime(&t, &tm, NULL);
116         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
117
118         for(count=0; count<5; count++) {
119                 res = sqlite_exec_printf(db,
120                         "INSERT INTO cdr ("
121                                 "clid,src,dst,dcontext,"
122                                 "channel,dstchannel,lastapp,lastdata, "
123                                 "start,answer,end,"
124                                 "duration,billsec,disposition,amaflags, "
125                                 "accountcode"
126 #                               if LOG_UNIQUEID
127                                 ",uniqueid"
128 #                               endif
129 #                               if LOG_USERFIELD
130                                 ",userfield"
131 #                               endif
132                         ") VALUES ("
133                                 "'%q', '%q', '%q', '%q', "
134                                 "'%q', '%q', '%q', '%q', "
135                                 "'%q', '%q', '%q', "
136                                 "%d, %d, %d, %d, "
137                                 "'%q'"
138 #                               if LOG_UNIQUEID
139                                 ",'%q'"
140 #                               endif
141 #                               if LOG_USERFIELD
142                                 ",'%q'"
143 #                               endif
144                         ")", NULL, NULL, &zErr,
145                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
146                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
147                                 startstr, answerstr, endstr,
148                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
149                                 cdr->accountcode
150 #                               if LOG_UNIQUEID
151                                 ,cdr->uniqueid
152 #                               endif
153 #                               if LOG_USERFIELD
154                                 ,cdr->userfield
155 #                               endif
156                         );
157                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
158                         break;
159                 usleep(200);
160         }
161         
162         if (zErr) {
163                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
164                 ast_free(zErr);
165         }
166
167         ast_mutex_unlock(&sqlite_lock);
168         return res;
169 }
170
171 static int unload_module(void)
172 {
173         if (db)
174                 sqlite_close(db);
175         ast_cdr_unregister(name);
176         return 0;
177 }
178
179 static int load_module(void)
180 {
181         char *zErr;
182         char fn[PATH_MAX];
183         int res;
184
185         ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
186                 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
187
188         /* is the database there? */
189         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
190         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
191         if (!db) {
192                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
193                 ast_free(zErr);
194                 return -1;
195         }
196
197         /* is the table there? */
198         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
199         if (res) {
200                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
201                 if (res) {
202                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
203                         ast_free(zErr);
204                         goto err;
205                 }
206
207                 /* TODO: here we should probably create an index */
208         }
209         
210         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
211         if (res) {
212                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
213                 return -1;
214         }
215         return 0;
216
217 err:
218         if (db)
219                 sqlite_close(db);
220         return -1;
221 }
222
223 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");