cd50af0e51fe69f9daa2c02909c37624f6df1c73
[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  * PostgreSQL http://www.postgresql.org/
33  *
34  * See also
35  * \arg \ref Config_cel
36  * 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 #define PGSQL_BACKEND_NAME "CEL PGSQL backend"
62
63 static char *config = "cel_pgsql.conf";
64 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
65 static int connected = 0;
66 static int maxsize = 512, maxsize2 = 512;
67
68 /*! \brief show_user_def is off by default */
69 #define CEL_SHOW_USERDEF_DEFAULT        0
70
71 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
72 static unsigned char cel_show_user_def;
73
74 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
75
76 static PGconn   *conn = NULL;
77 static PGresult *result = NULL;
78
79 struct columns {
80         char *name;
81         char *type;
82         int len;
83         unsigned int notnull:1;
84         unsigned int hasdefault:1;
85         AST_RWLIST_ENTRY(columns) list;
86 };
87
88 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
89
90 #define LENGTHEN_BUF1(size) \
91         do { \
92                 /* Lengthen buffer, if necessary */ \
93                 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
94                         if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
95                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
96                                 ast_free(sql); \
97                                 ast_free(sql2); \
98                                 AST_RWLIST_UNLOCK(&psql_columns); \
99                                 return; \
100                         } \
101                 } \
102         } while (0)
103
104 #define LENGTHEN_BUF2(size) \
105         do { \
106                 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
107                         if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
108                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
109                                 ast_free(sql); \
110                                 ast_free(sql2); \
111                                 AST_RWLIST_UNLOCK(&psql_columns); \
112                                 return; \
113                         } \
114                 } \
115         } while (0)
116
117 static void pgsql_log(struct ast_event *event)
118 {
119         struct ast_tm tm;
120         char timestr[128];
121         char *pgerror;
122         struct ast_cel_event_record record = {
123                 .version = AST_CEL_EVENT_RECORD_VERSION,
124         };
125
126         if (ast_cel_fill_record(event, &record)) {
127                 return;
128         }
129
130         ast_mutex_lock(&pgsql_lock);
131
132         ast_localtime(&record.event_time, &tm, NULL);
133         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
134
135         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
136                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
137                 if (PQstatus(conn) != CONNECTION_BAD) {
138                         connected = 1;
139                 } else {
140                         pgerror = PQerrorMessage(conn);
141                         ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
142                         ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
143                         PQfinish(conn);
144                         conn = NULL;
145                 }
146         }
147         if (connected) {
148                 struct columns *cur;
149                 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
150                 char buf[257], escapebuf[513];
151                 const char *value;
152                 int first = 1;
153
154                 if (!sql || !sql2) {
155                         goto ast_log_cleanup;
156                 }
157
158                 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
159                 ast_str_set(&sql2, 0, " VALUES (");
160
161 #define SEP (first ? "" : ",")
162
163                 AST_RWLIST_RDLOCK(&psql_columns);
164                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
165                         LENGTHEN_BUF1(strlen(cur->name) + 2);
166                         ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
167
168                         if (strcmp(cur->name, "eventtime") == 0) {
169                                 if (strncmp(cur->type, "int", 3) == 0) {
170                                         LENGTHEN_BUF2(13);
171                                         ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
172                                 } else if (strncmp(cur->type, "float", 5) == 0) {
173                                         LENGTHEN_BUF2(31);
174                                         ast_str_append(&sql2, 0, "%s%f",
175                                                 SEP,
176                                                 (double) record.event_time.tv_sec +
177                                                 (double) record.event_time.tv_usec / 1000000.0);
178                                 } else {
179                                         /* char, hopefully */
180                                         LENGTHEN_BUF2(31);
181                                         ast_localtime(&record.event_time, &tm, NULL);
182                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
183                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
184                                 }
185                         } else if (strcmp(cur->name, "eventtype") == 0) {
186                                 if (cur->type[0] == 'i') {
187                                         /* Get integer, no need to escape anything */
188                                         LENGTHEN_BUF2(5);
189                                         ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
190                                 } else if (strncmp(cur->type, "float", 5) == 0) {
191                                         LENGTHEN_BUF2(31);
192                                         ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
193                                 } else {
194                                         /* Char field, probably */
195                                         const char *event_name;
196
197                                         event_name = (!cel_show_user_def
198                                                 && record.event_type == AST_CEL_USER_DEFINED)
199                                                 ? record.user_defined_name : record.event_name;
200                                         LENGTHEN_BUF2(strlen(event_name) + 1);
201                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
202                                 }
203                         } else if (strcmp(cur->name, "amaflags") == 0) {
204                                 if (strncmp(cur->type, "int", 3) == 0) {
205                                         /* Integer, no need to escape anything */
206                                         LENGTHEN_BUF2(13);
207                                         ast_str_append(&sql2, 0, "%s%u", SEP, record.amaflag);
208                                 } else {
209                                         /* Although this is a char field, there are no special characters in the values for these fields */
210                                         LENGTHEN_BUF2(31);
211                                         ast_str_append(&sql2, 0, "%s'%u'", SEP, record.amaflag);
212                                 }
213                         } else {
214                                 /* Arbitrary field, could be anything */
215                                 if (strcmp(cur->name, "userdeftype") == 0) {
216                                         value = record.user_defined_name;
217                                 } else if (strcmp(cur->name, "cid_name") == 0) {
218                                         value = record.caller_id_name;
219                                 } else if (strcmp(cur->name, "cid_num") == 0) {
220                                         value = record.caller_id_num;
221                                 } else if (strcmp(cur->name, "cid_ani") == 0) {
222                                         value = record.caller_id_ani;
223                                 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
224                                         value = record.caller_id_rdnis;
225                                 } else if (strcmp(cur->name, "cid_dnid") == 0) {
226                                         value = record.caller_id_dnid;
227                                 } else if (strcmp(cur->name, "exten") == 0) {
228                                         value = record.extension;
229                                 } else if (strcmp(cur->name, "context") == 0) {
230                                         value = record.context;
231                                 } else if (strcmp(cur->name, "channame") == 0) {
232                                         value = record.channel_name;
233                                 } else if (strcmp(cur->name, "appname") == 0) {
234                                         value = record.application_name;
235                                 } else if (strcmp(cur->name, "appdata") == 0) {
236                                         value = record.application_data;
237                                 } else if (strcmp(cur->name, "accountcode") == 0) {
238                                         value = record.account_code;
239                                 } else if (strcmp(cur->name, "peeraccount") == 0) {
240                                         value = record.peer_account;
241                                 } else if (strcmp(cur->name, "uniqueid") == 0) {
242                                         value = record.unique_id;
243                                 } else if (strcmp(cur->name, "linkedid") == 0) {
244                                         value = record.linked_id;
245                                 } else if (strcmp(cur->name, "userfield") == 0) {
246                                         value = record.user_field;
247                                 } else if (strcmp(cur->name, "peer") == 0) {
248                                         value = record.peer;
249                                 } else if (strcmp(cur->name, "extra") == 0) {
250                                         value = record.extra;
251                                 } else {
252                                         value = NULL;
253                                 }
254
255                                 if (value == NULL) {
256                                         ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
257                                 } else if (strncmp(cur->type, "int", 3) == 0) {
258                                         long long whatever;
259                                         if (value && sscanf(value, "%30lld", &whatever) == 1) {
260                                                 LENGTHEN_BUF2(26);
261                                                 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
262                                         } else {
263                                                 LENGTHEN_BUF2(2);
264                                                 ast_str_append(&sql2, 0, "%s0", SEP);
265                                         }
266                                 } else if (strncmp(cur->type, "float", 5) == 0) {
267                                         long double whatever;
268                                         if (value && sscanf(value, "%30Lf", &whatever) == 1) {
269                                                 LENGTHEN_BUF2(51);
270                                                 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
271                                         } else {
272                                                 LENGTHEN_BUF2(2);
273                                                 ast_str_append(&sql2, 0, "%s0", SEP);
274                                         }
275                                         /* XXX Might want to handle dates, times, and other misc fields here XXX */
276                                 } else {
277                                         if (value) {
278                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
279                                         } else {
280                                                 escapebuf[0] = '\0';
281                                         }
282                                         LENGTHEN_BUF2(strlen(escapebuf) + 3);
283                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
284                                 }
285                         }
286                         first = 0;
287                 }
288                 AST_RWLIST_UNLOCK(&psql_columns);
289                 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
290                 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
291
292                 ast_debug(3, "Inserting a CEL record: [%s].\n", ast_str_buffer(sql));
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                 PQclear(result);
336
337 ast_log_cleanup:
338                 ast_free(sql);
339                 ast_free(sql2);
340         }
341
342         ast_mutex_unlock(&pgsql_lock);
343 }
344
345 static int my_unload_module(void)
346 {
347         struct columns *current;
348
349         ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
350         AST_RWLIST_WRLOCK(&psql_columns);
351         if (conn) {
352                 PQfinish(conn);
353                 conn = NULL;
354         }
355         if (pghostname) {
356                 ast_free(pghostname);
357                 pghostname = NULL;
358         }
359         if (pgdbname) {
360                 ast_free(pgdbname);
361                 pgdbname = NULL;
362         }
363         if (pgdbuser) {
364                 ast_free(pgdbuser);
365                 pgdbuser = NULL;
366         }
367         if (pgpassword) {
368                 ast_free(pgpassword);
369                 pgpassword = NULL;
370         }
371         if (pgdbport) {
372                 ast_free(pgdbport);
373                 pgdbport = NULL;
374         }
375         if (table) {
376                 ast_free(table);
377                 table = NULL;
378         }
379         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
380                 ast_free(current);
381         }
382         AST_RWLIST_UNLOCK(&psql_columns);
383         return 0;
384 }
385
386 static int unload_module(void)
387 {
388         return my_unload_module();
389 }
390
391 static int process_my_load_module(struct ast_config *cfg)
392 {
393         struct ast_variable *var;
394         char *pgerror;
395         const char *tmp;
396         PGresult *result;
397         struct columns *cur;
398
399         if (!(var = ast_variable_browse(cfg, "global"))) {
400                 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
401                 return AST_MODULE_LOAD_DECLINE;
402         }
403         if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
404                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
405                 tmp = "";       /* connect via UNIX-socket by default */
406         }
407         if (pghostname)
408                 ast_free(pghostname);
409         if (!(pghostname = ast_strdup(tmp))) {
410                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
411                 return AST_MODULE_LOAD_DECLINE;
412         }
413         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
414                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
415                 tmp = "asteriskceldb";
416         }
417         if (pgdbname)
418                 ast_free(pgdbname);
419         if (!(pgdbname = ast_strdup(tmp))) {
420                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
421                 return AST_MODULE_LOAD_DECLINE;
422         }
423         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
424                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
425                 tmp = "asterisk";
426         }
427         if (pgdbuser)
428                 ast_free(pgdbuser);
429         if (!(pgdbuser = ast_strdup(tmp))) {
430                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
431                 return AST_MODULE_LOAD_DECLINE;
432         }
433         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
434                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
435                 tmp = "";
436         }
437         if (pgpassword)
438                 ast_free(pgpassword);
439         if (!(pgpassword = ast_strdup(tmp))) {
440                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
441                 return AST_MODULE_LOAD_DECLINE;
442         }
443         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
444                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
445                 tmp = "5432";
446         }
447         if (pgdbport)
448                 ast_free(pgdbport);
449         if (!(pgdbport = ast_strdup(tmp))) {
450                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
451                 return AST_MODULE_LOAD_DECLINE;
452         }
453         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
454                 ast_log(LOG_WARNING,"CEL table not specified.  Assuming cel\n");
455                 tmp = "cel";
456         }
457         if (table)
458                 ast_free(table);
459         if (!(table = ast_strdup(tmp))) {
460                 return AST_MODULE_LOAD_DECLINE;
461         }
462         cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
463         if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
464                 cel_show_user_def = ast_true(tmp) ? 1 : 0;
465         }
466         if (option_debug) {
467                 if (ast_strlen_zero(pghostname)) {
468                         ast_debug(3, "cel_pgsql: using default unix socket\n");
469                 } else {
470                         ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
471                 }
472                 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
473                 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
474                 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
475                 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
476                 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
477                 ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
478                         cel_show_user_def ? "Yes" : "No");
479         }
480
481         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
482         if (PQstatus(conn) != CONNECTION_BAD) {
483                 char sqlcmd[512];
484                 char *fname, *ftype, *flen, *fnotnull, *fdef;
485                 char *tableptr;
486                 int i, rows;
487
488                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
489                 connected = 1;
490
491                 /* Remove any schema name from the table */
492                 if ((tableptr = strrchr(table, '.'))) {
493                         tableptr++;
494                 } else {
495                         tableptr = table;
496                 }
497
498                 /* Query the columns */
499                 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);
500                 result = PQexec(conn, sqlcmd);
501                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
502                         pgerror = PQresultErrorMessage(result);
503                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
504                         PQclear(result);
505                         unload_module();
506                         return AST_MODULE_LOAD_DECLINE;
507                 }
508
509                 rows = PQntuples(result);
510                 for (i = 0; i < rows; i++) {
511                         fname = PQgetvalue(result, i, 0);
512                         ftype = PQgetvalue(result, i, 1);
513                         flen = PQgetvalue(result, i, 2);
514                         fnotnull = PQgetvalue(result, i, 3);
515                         fdef = PQgetvalue(result, i, 4);
516                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
517                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
518                         if (cur) {
519                                 sscanf(flen, "%30d", &cur->len);
520                                 cur->name = (char *)cur + sizeof(*cur);
521                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
522                                 strcpy(cur->name, fname);
523                                 strcpy(cur->type, ftype);
524                                 if (*fnotnull == 't') {
525                                         cur->notnull = 1;
526                                 } else {
527                                         cur->notnull = 0;
528                                 }
529                                 if (!ast_strlen_zero(fdef)) {
530                                         cur->hasdefault = 1;
531                                 } else {
532                                         cur->hasdefault = 0;
533                                 }
534                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
535                         }
536                 }
537                 PQclear(result);
538         } else {
539                 pgerror = PQerrorMessage(conn);
540                 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
541                 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
542                 connected = 0;
543         }
544         return AST_MODULE_LOAD_SUCCESS;
545 }
546
547 static int my_load_module(int reload)
548 {
549         struct ast_config *cfg;
550         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
551
552         if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
553                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
554                 return AST_MODULE_LOAD_DECLINE;
555         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
556                 return AST_MODULE_LOAD_SUCCESS;
557         }
558
559         if (reload) {
560                 my_unload_module();
561         }
562
563         process_my_load_module(cfg);
564         ast_config_destroy(cfg);
565
566         if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
567                 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
568                 return AST_MODULE_LOAD_DECLINE;
569         }
570
571         return AST_MODULE_LOAD_SUCCESS;
572 }
573
574 static int load_module(void)
575 {
576         return my_load_module(0);
577 }
578
579 static int reload(void)
580 {
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 );