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