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