88557b6ab093243de191c1c58c92672bc18d7bac
[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 /*!
21  * \file
22  * \brief Store CDR records in a SQLite database.
23  *
24  * \author Holger Schurig <hs4233@mail.mn-solutions.de>
25  * 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         <defaultenabled>no</defaultenabled>
40         <support_level>deprecated</support_level>
41         <replacement>sqlite3_custom</replacement>
42  ***/
43
44 #include "asterisk.h"
45
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47
48 #include <sqlite.h>
49
50 #include "asterisk/channel.h"
51 #include "asterisk/module.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/paths.h"
54
55 #define LOG_UNIQUEID    0
56 #define LOG_USERFIELD   0
57 #define LOG_HRTIME      0
58
59 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
60 #define DATE_FORMAT "%Y-%m-%d %T"
61
62 static const char name[] = "sqlite";
63 static sqlite* db = NULL;
64
65 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
66
67 /*! \brief SQL table format */
68 static const char sql_create_table[] = "CREATE TABLE cdr ("
69 "       AcctId          INTEGER PRIMARY KEY,"
70 "       clid            VARCHAR(80),"
71 "       src             VARCHAR(80),"
72 "       dst             VARCHAR(80),"
73 "       dcontext        VARCHAR(80),"
74 "       channel         VARCHAR(80),"
75 "       dstchannel      VARCHAR(80),"
76 "       lastapp         VARCHAR(80),"
77 "       lastdata        VARCHAR(80),"
78 "       start           CHAR(19),"
79 "       answer          CHAR(19),"
80 "       end             CHAR(19),"
81 #if LOG_HRTIME
82 "       duration        FLOAT,"
83 "       billsec         FLOAT,"
84 #else
85 "       duration        INTEGER,"
86 "       billsec         INTEGER,"
87 #endif
88 "       disposition     INTEGER,"
89 "       amaflags        INTEGER,"
90 "       accountcode     VARCHAR(20)"
91 #if LOG_UNIQUEID
92 "       ,uniqueid       VARCHAR(32)"
93 #endif
94 #if LOG_USERFIELD
95 "       ,userfield      VARCHAR(255)"
96 #endif
97 ");";
98
99 static void format_date(char *buffer, size_t length, struct timeval *when)
100 {
101         struct ast_tm tm;
102
103         ast_localtime(when, &tm, NULL);
104         ast_strftime(buffer, length, DATE_FORMAT, &tm);
105 }
106
107 static int sqlite_log(struct ast_cdr *cdr)
108 {
109         int res = 0;
110         char *zErr = 0;
111         char startstr[80], answerstr[80], endstr[80];
112         int count;
113 #if LOG_HRTIME
114         double hrbillsec = 0.0;
115         double hrduration;
116 #endif
117
118         ast_mutex_lock(&sqlite_lock);
119
120         format_date(startstr, sizeof(startstr), &cdr->start);
121         format_date(answerstr, sizeof(answerstr), &cdr->answer);
122         format_date(endstr, sizeof(endstr), &cdr->end);
123
124 #if LOG_HRTIME
125         if (!ast_tvzero(cdr->answer)) {
126                 hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
127         }
128         hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
129 #endif
130
131         for(count=0; count<5; count++) {
132                 res = sqlite_exec_printf(db,
133                         "INSERT INTO cdr ("
134                                 "clid,src,dst,dcontext,"
135                                 "channel,dstchannel,lastapp,lastdata, "
136                                 "start,answer,end,"
137                                 "duration,billsec,disposition,amaflags, "
138                                 "accountcode"
139 #                               if LOG_UNIQUEID
140                                 ",uniqueid"
141 #                               endif
142 #                               if LOG_USERFIELD
143                                 ",userfield"
144 #                               endif
145                         ") VALUES ("
146                                 "'%q', '%q', '%q', '%q', "
147                                 "'%q', '%q', '%q', '%q', "
148                                 "'%q', '%q', '%q', "
149 #if LOG_HRTIME
150                                 "%f, %f, %d, %d, "
151 #else
152                                 "%d, %d, %d, %d, "
153 #endif
154                                 "'%q'"
155 #                               if LOG_UNIQUEID
156                                 ",'%q'"
157 #                               endif
158 #                               if LOG_USERFIELD
159                                 ",'%q'"
160 #                               endif
161                         ")", NULL, NULL, &zErr,
162                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
163                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
164                                 startstr, answerstr, endstr,
165 #if LOG_HRTIME
166                                 hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
167 #else
168                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
169 #endif
170                                 cdr->accountcode
171 #                               if LOG_UNIQUEID
172                                 ,cdr->uniqueid
173 #                               endif
174 #                               if LOG_USERFIELD
175                                 ,cdr->userfield
176 #                               endif
177                         );
178                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
179                         break;
180                 usleep(200);
181         }
182
183         if (zErr) {
184                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
185                 ast_free(zErr);
186         }
187
188         ast_mutex_unlock(&sqlite_lock);
189         return res;
190 }
191
192 static int unload_module(void)
193 {
194         if (ast_cdr_unregister(name)) {
195                 return -1;
196         }
197
198         if (db) {
199                 sqlite_close(db);
200         }
201         return 0;
202 }
203
204 static int load_module(void)
205 {
206         char *zErr;
207         char fn[PATH_MAX];
208         int res;
209
210         ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
211                 "using cdr_sqlite3_custom.\n");
212
213         /* is the database there? */
214         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
215         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
216         if (!db) {
217                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
218                 ast_free(zErr);
219                 return AST_MODULE_LOAD_DECLINE;
220         }
221
222         /* is the table there? */
223         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
224         if (res) {
225                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
226                 if (res) {
227                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
228                         ast_free(zErr);
229                         goto err;
230                 }
231
232                 /* TODO: here we should probably create an index */
233         }
234
235         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
236         if (res) {
237                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
238                 return AST_MODULE_LOAD_DECLINE;
239         }
240         return AST_MODULE_LOAD_SUCCESS;
241
242 err:
243         if (db)
244                 sqlite_close(db);
245         return AST_MODULE_LOAD_DECLINE;
246 }
247
248 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite CDR Backend",
249         .support_level = AST_MODULE_SUPPORT_DEPRECATED,
250         .load = load_module,
251         .unload = unload_module,
252         .load_pri = AST_MODPRI_CDR_DRIVER,
253 );