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