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