33410229fbf6c1f1d4088e77c42042d67825d5c4
[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  *
26  * See also
27  * \arg \ref Config_cdr
28  * \arg http://www.sqlite.org/
29  * 
30  * Creates the database and table on-the-fly
31  * \ingroup cdr_drivers
32  */
33
34 #include <sys/types.h>
35
36 #include <unistd.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <sqlite.h>
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include "asterisk/channel.h"
46 #include "asterisk/module.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/utils.h"
49
50 #define LOG_UNIQUEID    0
51 #define LOG_USERFIELD   0
52
53 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
54 #define DATE_FORMAT "%Y-%m-%d %T"
55
56 static char *desc = "SQLite CDR Backend";
57 static char *name = "sqlite";
58 static sqlite* db = NULL;
59
60 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
61
62 /*! \brief SQL table format */
63 static char sql_create_table[] = "CREATE TABLE cdr ("
64 "       AcctId          INTEGER PRIMARY KEY,"
65 "       clid            VARCHAR(80),"
66 "       src             VARCHAR(80),"
67 "       dst             VARCHAR(80),"
68 "       dcontext        VARCHAR(80),"
69 "       channel         VARCHAR(80),"
70 "       dstchannel      VARCHAR(80),"
71 "       lastapp         VARCHAR(80),"
72 "       lastdata        VARCHAR(80),"
73 "       start           CHAR(19),"
74 "       answer          CHAR(19),"
75 "       end             CHAR(19),"
76 "       duration        INTEGER,"
77 "       billsec         INTEGER,"
78 "       disposition     INTEGER,"
79 "       amaflags        INTEGER,"
80 "       accountcode     VARCHAR(20)"
81 #if LOG_UNIQUEID
82 "       ,uniqueid       VARCHAR(32)"
83 #endif
84 #if LOG_USERFIELD
85 "       ,userfield      VARCHAR(255)"
86 #endif
87 ");";
88
89 static int sqlite_log(struct ast_cdr *cdr)
90 {
91         int res = 0;
92         char *zErr = 0;
93         struct tm tm;
94         time_t t;
95         char startstr[80], answerstr[80], endstr[80];
96         int count;
97
98         ast_mutex_lock(&sqlite_lock);
99
100         t = cdr->start.tv_sec;
101         localtime_r(&t, &tm);
102         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
103
104         t = cdr->answer.tv_sec;
105         localtime_r(&t, &tm);
106         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
107
108         t = cdr->end.tv_sec;
109         localtime_r(&t, &tm);
110         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
111
112         for(count=0; count<5; count++) {
113                 res = sqlite_exec_printf(db,
114                         "INSERT INTO cdr ("
115                                 "clid,src,dst,dcontext,"
116                                 "channel,dstchannel,lastapp,lastdata, "
117                                 "start,answer,end,"
118                                 "duration,billsec,disposition,amaflags, "
119                                 "accountcode"
120 #                               if LOG_UNIQUEID
121                                 ",uniqueid"
122 #                               endif
123 #                               if LOG_USERFIELD
124                                 ",userfield"
125 #                               endif
126                         ") VALUES ("
127                                 "'%q', '%q', '%q', '%q', "
128                                 "'%q', '%q', '%q', '%q', "
129                                 "'%q', '%q', '%q', "
130                                 "%d, %d, %d, %d, "
131                                 "'%q'"
132 #                               if LOG_UNIQUEID
133                                 ",'%q'"
134 #                               endif
135 #                               if LOG_USERFIELD
136                                 ",'%q'"
137 #                               endif
138                         ")", NULL, NULL, &zErr,
139                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
140                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
141                                 startstr, answerstr, endstr,
142                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
143                                 cdr->accountcode
144 #                               if LOG_UNIQUEID
145                                 ,cdr->uniqueid
146 #                               endif
147 #                               if LOG_USERFIELD
148                                 ,cdr->userfield
149 #                               endif
150                         );
151                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
152                         break;
153                 usleep(200);
154         }
155         
156         if (zErr) {
157                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
158                 free(zErr);
159         }
160
161         ast_mutex_unlock(&sqlite_lock);
162         return res;
163 }
164
165
166 char *description(void)
167 {
168         return desc;
169 }
170
171 int unload_module(void)
172 {
173         if (db)
174                 sqlite_close(db);
175         ast_cdr_unregister(name);
176         return 0;
177 }
178
179 int load_module(void)
180 {
181         char *zErr;
182         char fn[PATH_MAX];
183         int res;
184
185         /* is the database there? */
186         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
187         db = sqlite_open(fn, 0660, &zErr);
188         if (!db) {
189                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
190                 free(zErr);
191                 return -1;
192         }
193
194         /* is the table there? */
195         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
196         if (res) {
197                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
198                 if (res) {
199                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
200                         free(zErr);
201                         goto err;
202                 }
203
204                 /* TODO: here we should probably create an index */
205         }
206         
207         res = ast_cdr_register(name, desc, sqlite_log);
208         if (res) {
209                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
210                 return -1;
211         }
212         return 0;
213
214 err:
215         if (db)
216                 sqlite_close(db);
217         return -1;
218 }
219
220 int reload(void)
221 {
222         return 0;
223 }
224
225 int usecount(void)
226 {
227         return 0;
228 }
229
230 char *key()
231 {
232         return ASTERISK_GPL_KEY;
233 }