f7676f88ead52d29b1b61f00857db310c4836716
[asterisk/asterisk.git] / cel / cel_pgsql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008
5  *
6  * Steve Murphy - adapted to CEL, from:
7  * Matthew D. Hardeman <mhardemn@papersoft.com> 
8  * Adapted from the MySQL CDR logger originally by James Sharp 
9  *
10  * Modified April, 2007; Dec, 2008
11  * Steve Murphy <murf@digium.com>
12
13  * Modified September 2003
14  * Matthew D. Hardeman <mhardemn@papersoft.com>
15  *
16  * See http://www.asterisk.org for more information about
17  * the Asterisk project. Please do not directly contact
18  * any of the maintainers of this project for assistance;
19  * the project provides a web site, mailing lists and IRC
20  * channels for your use.
21  *
22  * This program is free software, distributed under the terms of
23  * the GNU General Public License Version 2. See the LICENSE file
24  * at the top of the source tree.
25  */
26
27 /*! \file
28  *
29  * \brief PostgreSQL CEL logger 
30  * 
31  * \author Steve Murphy <murf@digium.com>
32  * \extref PostgreSQL http://www.postgresql.org/
33  *
34  * See also
35  * \arg \ref Config_cel
36  * \extref PostgreSQL http://www.postgresql.org/
37  * \ingroup cel_drivers
38  */
39
40 /*** MODULEINFO
41         <depend>pgsql</depend>
42         <support_level>extended</support_level>
43  ***/
44
45 #include "asterisk.h"
46
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48
49 #include <libpq-fe.h>
50
51 #include "asterisk/config.h"
52 #include "asterisk/options.h"
53 #include "asterisk/channel.h"
54 #include "asterisk/cel.h"
55 #include "asterisk/module.h"
56 #include "asterisk/logger.h"
57 #include "asterisk.h"
58
59 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
60
61 static char *config = "cel_pgsql.conf";
62 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
63 static int connected = 0;
64 static int maxsize = 512, maxsize2 = 512;
65
66 /*! \brief show_user_def is off by default */
67 #define CEL_SHOW_USERDEF_DEFAULT        0
68
69 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
70 static unsigned char cel_show_user_def;
71
72 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
73
74 static PGconn   *conn = NULL;
75 static PGresult *result = NULL;
76 static struct ast_event_sub *event_sub = NULL;
77
78 struct columns {
79         char *name;
80         char *type;
81         int len;
82         unsigned int notnull:1;
83         unsigned int hasdefault:1;
84         AST_RWLIST_ENTRY(columns) list;
85 };
86
87 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
88
89 #define LENGTHEN_BUF1(size) \
90         do { \
91                 /* Lengthen buffer, if necessary */ \
92                 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
93                         if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
94                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
95                                 ast_free(sql); \
96                                 ast_free(sql2); \
97                                 AST_RWLIST_UNLOCK(&psql_columns); \
98                                 return; \
99                         } \
100                 } \
101         } while (0)
102
103 #define LENGTHEN_BUF2(size) \
104         do { \
105                 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
106                         if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
107                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
108                                 ast_free(sql); \
109                                 ast_free(sql2); \
110                                 AST_RWLIST_UNLOCK(&psql_columns); \
111                                 return; \
112                         } \
113                 } \
114         } while (0)
115
116 static void pgsql_log(const struct ast_event *event, void *userdata)
117 {
118         struct ast_tm tm;
119         char timestr[128];
120         char *pgerror;
121         struct ast_cel_event_record record = {
122                 .version = AST_CEL_EVENT_RECORD_VERSION,
123         };
124
125         if (ast_cel_fill_record(event, &record)) {
126                 return;
127         }
128
129         ast_mutex_lock(&pgsql_lock);
130
131         ast_localtime(&record.event_time, &tm, NULL);
132         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
133
134         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
135                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
136                 if (PQstatus(conn) != CONNECTION_BAD) {
137                         connected = 1;
138                 } else {
139                         pgerror = PQerrorMessage(conn);
140                         ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
141                         ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
142                         PQfinish(conn);
143                         conn = NULL;
144                 }
145         }
146         if (connected) {
147                 struct columns *cur;
148                 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
149                 char buf[257], escapebuf[513];
150                 const char *value;
151                 int first = 1;
152
153                 if (!sql || !sql2) {
154                         goto ast_log_cleanup;
155                 }
156
157                 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
158                 ast_str_set(&sql2, 0, " VALUES (");
159
160 #define SEP (first ? "" : ",")
161
162                 AST_RWLIST_RDLOCK(&psql_columns);
163                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
164                         LENGTHEN_BUF1(strlen(cur->name) + 2);
165                         ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
166
167                         if (strcmp(cur->name, "eventtime") == 0) {
168                                 if (strncmp(cur->type, "int", 3) == 0) {
169                                         LENGTHEN_BUF2(13);
170                                         ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
171                                 } else if (strncmp(cur->type, "float", 5) == 0) {
172                                         LENGTHEN_BUF2(31);
173                                         ast_str_append(&sql2, 0, "%s%f",
174                                                 SEP,
175                                                 (double) record.event_time.tv_sec +
176                                                 (double) record.event_time.tv_usec / 1000000.0);
177                                 } else {
178                                         /* char, hopefully */
179                                         LENGTHEN_BUF2(31);
180                                         ast_localtime(&record.event_time, &tm, NULL);
181                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
182                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
183                                 }
184                         } else if (strcmp(cur->name, "eventtype") == 0) {
185                                 if (cur->type[0] == 'i') {
186                                         /* Get integer, no need to escape anything */
187                                         LENGTHEN_BUF2(5);
188                                         ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
189                                 } else if (strncmp(cur->type, "float", 5) == 0) {
190                                         LENGTHEN_BUF2(31);
191                                         ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
192                                 } else {
193                                         /* Char field, probably */
194                                         const char *event_name;
195
196                                         event_name = (!cel_show_user_def
197                                                 && record.event_type == AST_CEL_USER_DEFINED)
198                                                 ? record.user_defined_name : record.event_name;
199                                         LENGTHEN_BUF2(strlen(event_name) + 1);
200                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
201                                 }
202                         } else if (strcmp(cur->name, "amaflags") == 0) {
203                                 if (strncmp(cur->type, "int", 3) == 0) {
204                                         /* Integer, no need to escape anything */
205                                         LENGTHEN_BUF2(13);
206                                         ast_str_append(&sql2, 0, "%s%d", SEP, record.amaflag);
207                                 } else {
208                                         /* Although this is a char field, there are no special characters in the values for these fields */
209                                         LENGTHEN_BUF2(31);
210                                         ast_str_append(&sql2, 0, "%s'%d'", SEP, record.amaflag);
211                                 }
212                         } else {
213                                 /* Arbitrary field, could be anything */
214                                 if (strcmp(cur->name, "userdeftype") == 0) {
215                                         value = record.user_defined_name;
216                                 } else if (strcmp(cur->name, "cid_name") == 0) {
217                                         value = record.caller_id_name;
218                                 } else if (strcmp(cur->name, "cid_num") == 0) {
219                                         value = record.caller_id_num;
220                                 } else if (strcmp(cur->name, "cid_ani") == 0) {
221                                         value = record.caller_id_ani;
222                                 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
223                                         value = record.caller_id_rdnis;
224                                 } else if (strcmp(cur->name, "cid_dnid") == 0) {
225                                         value = record.caller_id_dnid;
226                                 } else if (strcmp(cur->name, "exten") == 0) {
227                                         value = record.extension;
228                                 } else if (strcmp(cur->name, "context") == 0) {
229                                         value = record.context;
230                                 } else if (strcmp(cur->name, "channame") == 0) {
231                                         value = record.channel_name;
232                                 } else if (strcmp(cur->name, "appname") == 0) {
233                                         value = record.application_name;
234                                 } else if (strcmp(cur->name, "appdata") == 0) {
235                                         value = record.application_data;
236                                 } else if (strcmp(cur->name, "accountcode") == 0) {
237                                         value = record.account_code;
238                                 } else if (strcmp(cur->name, "peeraccount") == 0) {
239                                         value = record.peer_account;
240                                 } else if (strcmp(cur->name, "uniqueid") == 0) {
241                                         value = record.unique_id;
242                                 } else if (strcmp(cur->name, "linkedid") == 0) {
243                                         value = record.linked_id;
244                                 } else if (strcmp(cur->name, "userfield") == 0) {
245                                         value = record.user_field;
246                                 } else if (strcmp(cur->name, "peer") == 0) {
247                                         value = record.peer;
248                                 } else if (strcmp(cur->name, "extra") == 0) {
249                                         value = record.extra;
250                                 } else {
251                                         value = NULL;
252                                 }
253
254                                 if (value == NULL) {
255                                         ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
256                                 } else if (strncmp(cur->type, "int", 3) == 0) {
257                                         long long whatever;
258                                         if (value && sscanf(value, "%30lld", &whatever) == 1) {
259                                                 LENGTHEN_BUF2(26);
260                                                 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
261                                         } else {
262                                                 LENGTHEN_BUF2(2);
263                                                 ast_str_append(&sql2, 0, "%s0", SEP);
264                                         }
265                                 } else if (strncmp(cur->type, "float", 5) == 0) {
266                                         long double whatever;
267                                         if (value && sscanf(value, "%30Lf", &whatever) == 1) {
268                                                 LENGTHEN_BUF2(51);
269                                                 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
270                                         } else {
271                                                 LENGTHEN_BUF2(2);
272                                                 ast_str_append(&sql2, 0, "%s0", SEP);
273                                         }
274                                         /* XXX Might want to handle dates, times, and other misc fields here XXX */
275                                 } else {
276                                         if (value) {
277                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
278                                         } else {
279                                                 escapebuf[0] = '\0';
280                                         }
281                                         LENGTHEN_BUF2(strlen(escapebuf) + 3);
282                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
283                                 }
284                         }
285                         first = 0;
286                 }
287                 AST_RWLIST_UNLOCK(&psql_columns);
288                 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
289                 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
290                 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
291
292                 ast_debug(2, "inserting a CEL record.\n");
293                 /* Test to be sure we're still connected... */
294                 /* If we're connected, and connection is working, good. */
295                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
296                 if (PQstatus(conn) == CONNECTION_OK) {
297                         connected = 1;
298                 } else {
299                         ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
300                         PQreset(conn);
301                         if (PQstatus(conn) == CONNECTION_OK) {
302                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
303                                 connected = 1;
304                         } else {
305                                 pgerror = PQerrorMessage(conn);
306                                 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
307                                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
308                                 PQfinish(conn);
309                                 conn = NULL;
310                                 connected = 0;
311                                 goto ast_log_cleanup;
312                         }
313                 }
314                 result = PQexec(conn, ast_str_buffer(sql));
315                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
316                         pgerror = PQresultErrorMessage(result);
317                         ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
318                         ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
319                         ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
320                         PQreset(conn);
321                         if (PQstatus(conn) == CONNECTION_OK) {
322                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
323                                 connected = 1;
324                                 PQclear(result);
325                                 result = PQexec(conn, ast_str_buffer(sql));
326                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
327                                         pgerror = PQresultErrorMessage(result);
328                                         ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
329                                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
330                                 }
331                         }
332                         PQclear(result);
333                         goto ast_log_cleanup;
334                 }
335
336 ast_log_cleanup:
337                 ast_free(sql);
338                 ast_free(sql2);
339         }
340
341         ast_mutex_unlock(&pgsql_lock);
342 }
343
344 static int my_unload_module(void)
345 {
346         struct columns *current;
347         AST_RWLIST_WRLOCK(&psql_columns);
348         if (event_sub) {
349                 event_sub = ast_event_unsubscribe(event_sub);
350                 event_sub = NULL;
351         }
352         if (conn) {
353                 PQfinish(conn);
354                 conn = NULL;
355         }
356         if (pghostname) {
357                 ast_free(pghostname);
358                 pghostname = NULL;
359         }
360         if (pgdbname) {
361                 ast_free(pgdbname);
362                 pgdbname = NULL;
363         }
364         if (pgdbuser) {
365                 ast_free(pgdbuser);
366                 pgdbuser = NULL;
367         }
368         if (pgpassword) {
369                 ast_free(pgpassword);
370                 pgpassword = NULL;
371         }
372         if (pgdbport) {
373                 ast_free(pgdbport);
374                 pgdbport = NULL;
375         }
376         if (table) {
377                 ast_free(table);
378                 table = NULL;
379         }
380         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
381                 ast_free(current);
382         }
383         AST_RWLIST_UNLOCK(&psql_columns);
384         return 0;
385 }
386
387 static int unload_module(void)
388 {
389         return my_unload_module();
390 }
391
392 static int process_my_load_module(struct ast_config *cfg)
393 {
394         struct ast_variable *var;
395         char *pgerror;
396         const char *tmp;
397         PGresult *result;
398         struct columns *cur;
399
400         if (!(var = ast_variable_browse(cfg, "global"))) {
401                 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
402                 return AST_MODULE_LOAD_DECLINE;
403         }
404         if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
405                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
406                 tmp = "";       /* connect via UNIX-socket by default */
407         }
408         if (pghostname)
409                 ast_free(pghostname);
410         if (!(pghostname = ast_strdup(tmp))) {
411                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
412                 return AST_MODULE_LOAD_DECLINE;
413         }
414         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
415                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
416                 tmp = "asteriskceldb";
417         }
418         if (pgdbname)
419                 ast_free(pgdbname);
420         if (!(pgdbname = ast_strdup(tmp))) {
421                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
422                 return AST_MODULE_LOAD_DECLINE;
423         }
424         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
425                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
426                 tmp = "asterisk";
427         }
428         if (pgdbuser)
429                 ast_free(pgdbuser);
430         if (!(pgdbuser = ast_strdup(tmp))) {
431                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
432                 return AST_MODULE_LOAD_DECLINE;
433         }
434         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
435                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
436                 tmp = "";
437         }
438         if (pgpassword)
439                 ast_free(pgpassword);
440         if (!(pgpassword = ast_strdup(tmp))) {
441                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
442                 return AST_MODULE_LOAD_DECLINE;
443         }
444         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
445                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
446                 tmp = "5432";
447         }
448         if (pgdbport)
449                 ast_free(pgdbport);
450         if (!(pgdbport = ast_strdup(tmp))) {
451                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
452                 return AST_MODULE_LOAD_DECLINE;
453         }
454         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
455                 ast_log(LOG_WARNING,"CEL table not specified.  Assuming cel\n");
456                 tmp = "cel";
457         }
458         if (table)
459                 ast_free(table);
460         if (!(table = ast_strdup(tmp))) {
461                 return AST_MODULE_LOAD_DECLINE;
462         }
463         cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
464         if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
465                 cel_show_user_def = ast_true(tmp) ? 1 : 0;
466         }
467         if (option_debug) {
468                 if (ast_strlen_zero(pghostname)) {
469                         ast_debug(3, "cel_pgsql: using default unix socket\n");
470                 } else {
471                         ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
472                 }
473                 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
474                 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
475                 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
476                 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
477                 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
478                 ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
479                         cel_show_user_def ? "Yes" : "No");
480         }
481
482         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
483         if (PQstatus(conn) != CONNECTION_BAD) {
484                 char sqlcmd[512];
485                 char *fname, *ftype, *flen, *fnotnull, *fdef;
486                 char *tableptr;
487                 int i, rows;
488
489                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
490                 connected = 1;
491
492                 /* Remove any schema name from the table */
493                 if ((tableptr = strrchr(table, '.'))) {
494                         tableptr++;
495                 } else {
496                         tableptr = table;
497                 }
498
499                 /* Query the columns */
500                 snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc from pg_class c, pg_type t, pg_attribute a left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", tableptr);
501                 result = PQexec(conn, sqlcmd);
502                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
503                         pgerror = PQresultErrorMessage(result);
504                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
505                         PQclear(result);
506                         unload_module();
507                         return AST_MODULE_LOAD_DECLINE;
508                 }
509
510                 rows = PQntuples(result);
511                 for (i = 0; i < rows; i++) {
512                         fname = PQgetvalue(result, i, 0);
513                         ftype = PQgetvalue(result, i, 1);
514                         flen = PQgetvalue(result, i, 2);
515                         fnotnull = PQgetvalue(result, i, 3);
516                         fdef = PQgetvalue(result, i, 4);
517                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
518                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
519                         if (cur) {
520                                 sscanf(flen, "%30d", &cur->len);
521                                 cur->name = (char *)cur + sizeof(*cur);
522                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
523                                 strcpy(cur->name, fname);
524                                 strcpy(cur->type, ftype);
525                                 if (*fnotnull == 't') {
526                                         cur->notnull = 1;
527                                 } else {
528                                         cur->notnull = 0;
529                                 }
530                                 if (!ast_strlen_zero(fdef)) {
531                                         cur->hasdefault = 1;
532                                 } else {
533                                         cur->hasdefault = 0;
534                                 }
535                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
536                         }
537                 }
538                 PQclear(result);
539         } else {
540                 pgerror = PQerrorMessage(conn);
541                 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
542                 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
543                 connected = 0;
544         }
545         return AST_MODULE_LOAD_SUCCESS;
546 }
547
548 static int my_load_module(int reload)
549 {
550         struct ast_config *cfg;
551         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
552
553         if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
554                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
555                 return AST_MODULE_LOAD_DECLINE;
556         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
557                 return AST_MODULE_LOAD_SUCCESS;
558         }
559
560         process_my_load_module(cfg);
561         ast_config_destroy(cfg);
562
563         event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
564
565         if (!event_sub) {
566                 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
567                 return AST_MODULE_LOAD_DECLINE;
568         }
569
570         return AST_MODULE_LOAD_SUCCESS;
571 }
572
573 static int load_module(void)
574 {
575         return my_load_module(0);
576 }
577
578 static int reload(void)
579 {
580         my_unload_module();
581         return my_load_module(1);
582 }
583
584 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
585         .load = load_module,
586         .unload = unload_module,
587         .reload = reload,
588         .load_pri = AST_MODPRI_CDR_DRIVER,
589 );