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