Rename cdr_unixodbc to cdr_odbc
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * PostgreSQL CDR logger 
5  *
6  * Matthew D. Hardeman <mhardemn@papersoft.com> 
7  * Adapted from the MySQL CDR logger originally by James Sharp 
8  *
9  * Modified September 2003
10  * Matthew D. Hardeman <mhardemn@papersoft.com>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License.
14  *
15  */
16
17 #include <sys/types.h>
18 #include <asterisk/config.h>
19 #include <asterisk/options.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/cdr.h>
22 #include <asterisk/module.h>
23 #include <asterisk/logger.h>
24 #include "../asterisk.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <time.h>
32
33 #include <libpq-fe.h>
34
35 #define DATE_FORMAT "%Y-%m-%d %T"
36
37 static char *desc = "PostgreSQL CDR Backend";
38 static char *name = "pgsql";
39 static char *config = "cdr_pgsql.conf";
40 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL;
41 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0;
42 static int connected = 0;
43
44 static ast_mutex_t pgsql_lock = AST_MUTEX_INITIALIZER;
45
46 PGconn          *conn;
47 PGresult        *result;
48
49 static int pgsql_log(struct ast_cdr *cdr)
50 {
51         struct tm tm;
52         struct timeval tv;
53         char sqlcmd[2048], timestr[128];
54         time_t t;
55         char *pgerror;
56
57         ast_mutex_lock(&pgsql_lock);
58
59         memset(sqlcmd,0,2048);
60
61         gettimeofday(&tv,NULL);
62         t = tv.tv_sec;
63         localtime_r(&t,&tm);
64         strftime(timestr,128,DATE_FORMAT,&tm);
65
66         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
67                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
68                 if (PQstatus(conn) != CONNECTION_BAD) {
69                         connected = 1;
70                 } else {
71                         pgerror = PQerrorMessage(conn);
72                         ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
73                         ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
74                 }
75         }
76
77         if (connected) {
78                 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
79                 char *uniqueid=NULL;
80
81                 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
82                 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
83                         PQescapeString(clid, cdr->clid, strlen(cdr->clid));
84                 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
85                         PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
86                 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
87                         PQescapeString(channel, cdr->channel, strlen(cdr->channel));
88                 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
89                         PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
90                 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
91                         PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
92                 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
93                         PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
94                 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
95                         PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
96
97                 /* Check for all alloca failures above at once */
98                 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
99                         ast_log(LOG_ERROR, "cdr_pgsql:  Out of memory error (insert fails)\n");
100                         ast_mutex_unlock(&pgsql_lock);
101                         return -1;
102                 }
103
104                 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
105
106                 sprintf(sqlcmd,"INSERT INTO cdr (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid);
107                 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed:  %s\n",sqlcmd);
108         
109                 /* Test to be sure we're still connected... */
110                 /* If we're connected, and connection is working, good. */
111                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
112                 if (PQstatus(conn) == CONNECTION_OK) {
113                         connected = 1;
114                 } else {
115                         ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
116                         PQreset(conn);
117                         if (PQstatus(conn) == CONNECTION_OK) {
118                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
119                                 connected = 1;
120                         } else {
121                                 pgerror = PQerrorMessage(conn);
122                                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
123                                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
124                                 connected = 0;
125                                 return -1;
126                         }
127                 }
128                 result = PQexec(conn, sqlcmd);
129                 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
130                         pgerror = PQresultErrorMessage(result);
131                         ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
132                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
133                         ast_mutex_unlock(&pgsql_lock);
134                         return -1;
135                 }
136         }
137         ast_mutex_unlock(&pgsql_lock);
138         return 0;
139 }
140
141 char *description(void)
142 {
143         return desc;
144 }
145
146 static int my_unload_module(void)
147
148         PQfinish(conn);
149         connected = 0;
150         if (pghostname && hostname_alloc) {
151                 free(pghostname);
152                 pghostname = NULL;
153                 hostname_alloc = 0;
154         }
155         if (pgdbname && dbname_alloc) {
156                 free(pgdbname);
157                 pgdbname = NULL;
158                 dbname_alloc = 0;
159         }
160         if (pgdbuser && dbuser_alloc) {
161                 free(pgdbuser);
162                 pgdbuser = NULL;
163                 dbuser_alloc = 0;
164         }
165         if (pgdbsock && dbsock_alloc) {
166                 free(pgdbsock);
167                 pgdbsock = NULL;
168                 dbsock_alloc = 0;
169         }
170         if (pgpassword && password_alloc) {
171                 free(pgpassword);
172                 pgpassword = NULL;
173                 password_alloc = 0;
174         }
175         if (pgdbport && dbport_alloc) {
176                 free(pgdbport);
177                 pgdbport = NULL;
178                 dbport_alloc = 0;
179         }
180         ast_cdr_unregister(name);
181         return 0;
182 }
183
184 static int my_load_module(void)
185 {
186         int res;
187         struct ast_config *cfg;
188         struct ast_variable *var;
189         char *pgerror;
190         char *tmp;
191
192         cfg = ast_load(config);
193         if (!cfg) {
194                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
195                 return 0;
196         }
197         
198         var = ast_variable_browse(cfg, "global");
199         if (!var) {
200                 /* nothing configured */
201                 return 0;
202         }
203
204         tmp = ast_variable_retrieve(cfg,"global","hostname");
205         if (tmp) {
206                 pghostname = malloc(strlen(tmp) + 1);
207                 if (pghostname != NULL) {
208                         hostname_alloc = 1;
209                         strcpy(pghostname,tmp);
210                 } else {
211                         ast_log(LOG_ERROR,"Out of memory error.\n");
212                         return -1;
213                 }
214         } else {
215                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming localhost\n");
216                 pghostname = "localhost";
217         }
218
219         tmp = ast_variable_retrieve(cfg,"global","dbname");
220         if (tmp) {
221                 pgdbname = malloc(strlen(tmp) + 1);
222                 if (pgdbname != NULL) {
223                         dbname_alloc = 1;
224                         strcpy(pgdbname,tmp);
225                 } else {
226                         ast_log(LOG_ERROR,"Out of memory error.\n");
227                         return -1;
228                 }
229         } else {
230                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
231                 pgdbname = "asteriskcdrdb";
232         }
233
234         tmp = ast_variable_retrieve(cfg,"global","user");
235         if (tmp) {
236                 pgdbuser = malloc(strlen(tmp) + 1);
237                 if (pgdbuser != NULL) {
238                         dbuser_alloc = 1;
239                         strcpy(pgdbuser,tmp);
240                 } else {
241                         ast_log(LOG_ERROR,"Out of memory error.\n");
242                         return -1;
243                 }
244         } else {
245                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming root\n");
246                 pgdbuser = "root";
247         }
248
249         tmp = ast_variable_retrieve(cfg,"global","password");
250         if (tmp) {
251                 pgpassword = malloc(strlen(tmp) + 1);
252                 if (pgpassword != NULL) {
253                         password_alloc = 1;
254                         strcpy(pgpassword,tmp);
255                 } else {
256                         ast_log(LOG_ERROR,"Out of memory error.\n");
257                         return -1;
258                 }
259         } else {
260                 ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
261                 pgpassword = "";
262         }
263
264         tmp = ast_variable_retrieve(cfg,"global","port");
265         if (tmp) {
266                 pgdbport = malloc(strlen(tmp) + 1);
267                 if (pgdbport != NULL) {
268                         dbport_alloc = 1;
269                         strcpy(pgdbport,tmp);
270                 } else {
271                         ast_log(LOG_ERROR,"Out of memory error.\n");
272                         return -1;
273                 }
274         } else {
275                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
276                 pgdbport = "5432";
277         }
278
279         ast_destroy(cfg);
280
281         ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
282         ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
283         if (pgdbsock)
284                 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
285         ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
286         ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
287         ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
288
289         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
290         if (PQstatus(conn) != CONNECTION_BAD) {
291                 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
292                 connected = 1;
293         } else {
294                 pgerror = PQerrorMessage(conn);
295                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
296                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
297                 connected = 0;
298         }
299
300         res = ast_cdr_register(name, desc, pgsql_log);
301         if (res) {
302                 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
303         }
304         return res;
305 }
306
307 int load_module(void)
308 {
309         return my_load_module();
310 }
311
312 int unload_module(void)
313 {
314         return my_unload_module();
315 }
316
317 int reload(void)
318 {
319         my_unload_module();
320         return my_load_module();
321 }
322
323 int usecount(void)
324 {
325         return connected;
326 }
327
328 char *key()
329 {
330         return ASTERISK_GPL_KEY;
331 }