Add CEL extra field to cel_pgsql.
[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 if (strcmp(cur->name, "extra") == 0) {
243                                         value = record.extra;
244                                 } else {
245                                         value = NULL;
246                                 }
247
248                                 if (value == NULL) {
249                                         ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
250                                 } else if (strncmp(cur->type, "int", 3) == 0) {
251                                         long long whatever;
252                                         if (value && sscanf(value, "%30lld", &whatever) == 1) {
253                                                 LENGTHEN_BUF2(26);
254                                                 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
255                                         } else {
256                                                 LENGTHEN_BUF2(2);
257                                                 ast_str_append(&sql2, 0, "%s0", SEP);
258                                         }
259                                 } else if (strncmp(cur->type, "float", 5) == 0) {
260                                         long double whatever;
261                                         if (value && sscanf(value, "%30Lf", &whatever) == 1) {
262                                                 LENGTHEN_BUF2(51);
263                                                 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
264                                         } else {
265                                                 LENGTHEN_BUF2(2);
266                                                 ast_str_append(&sql2, 0, "%s0", SEP);
267                                         }
268                                         /* XXX Might want to handle dates, times, and other misc fields here XXX */
269                                 } else {
270                                         if (value) {
271                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
272                                         } else {
273                                                 escapebuf[0] = '\0';
274                                         }
275                                         LENGTHEN_BUF2(strlen(escapebuf) + 3);
276                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
277                                 }
278                         }
279                         first = 0;
280                 }
281                 AST_RWLIST_UNLOCK(&psql_columns);
282                 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
283                 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
284                 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
285
286                 ast_debug(2, "inserting a CEL record.\n");
287                 /* Test to be sure we're still connected... */
288                 /* If we're connected, and connection is working, good. */
289                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
290                 if (PQstatus(conn) == CONNECTION_OK) {
291                         connected = 1;
292                 } else {
293                         ast_log(LOG_ERROR, "Connection was lost... attempting to reconnect.\n");
294                         PQreset(conn);
295                         if (PQstatus(conn) == CONNECTION_OK) {
296                                 ast_log(LOG_ERROR, "Connection reestablished.\n");
297                                 connected = 1;
298                         } else {
299                                 pgerror = PQerrorMessage(conn);
300                                 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
301                                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
302                                 PQfinish(conn);
303                                 conn = NULL;
304                                 connected = 0;
305                                 ast_mutex_unlock(&pgsql_lock);
306                                 ast_free(sql);
307                                 ast_free(sql2);
308                                 return;
309                         }
310                 }
311                 result = PQexec(conn, ast_str_buffer(sql));
312                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
313                         pgerror = PQresultErrorMessage(result);
314                         ast_log(LOG_ERROR, "Failed to insert call detail record into database!\n");
315                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
316                         ast_log(LOG_ERROR, "Connection may have been lost... attempting to reconnect.\n");
317                         PQreset(conn);
318                         if (PQstatus(conn) == CONNECTION_OK) {
319                                 ast_log(LOG_ERROR, "Connection reestablished.\n");
320                                 connected = 1;
321                                 PQclear(result);
322                                 result = PQexec(conn, ast_str_buffer(sql));
323                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
324                                         pgerror = PQresultErrorMessage(result);
325                                         ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
326                                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
327                                 }
328                         }
329                         ast_mutex_unlock(&pgsql_lock);
330                         PQclear(result);
331                         ast_free(sql);
332                         ast_free(sql2);
333                         return;
334                 }
335                 ast_mutex_unlock(&pgsql_lock);
336         }
337 }
338
339 static int my_unload_module(void)
340 {
341         struct columns *current;
342         AST_RWLIST_WRLOCK(&psql_columns);
343         if (event_sub) {
344                 event_sub = ast_event_unsubscribe(event_sub);
345                 event_sub = NULL;
346         }
347         if (conn) {
348                 PQfinish(conn);
349                 conn = NULL;
350         }
351         if (pghostname) {
352                 ast_free(pghostname);
353                 pghostname = NULL;
354         }
355         if (pgdbname) {
356                 ast_free(pgdbname);
357                 pgdbname = NULL;
358         }
359         if (pgdbuser) {
360                 ast_free(pgdbuser);
361                 pgdbuser = NULL;
362         }
363         if (pgpassword) {
364                 ast_free(pgpassword);
365                 pgpassword = NULL;
366         }
367         if (pgdbport) {
368                 ast_free(pgdbport);
369                 pgdbport = NULL;
370         }
371         if (table) {
372                 ast_free(table);
373                 table = NULL;
374         }
375         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
376                 ast_free(current);
377         }
378         AST_RWLIST_UNLOCK(&psql_columns);
379         return 0;
380 }
381
382 static int unload_module(void)
383 {
384         return my_unload_module();
385 }
386
387 static int process_my_load_module(struct ast_config *cfg)
388 {
389         struct ast_variable *var;
390         char *pgerror;
391         const char *tmp;
392         PGresult *result;
393         struct columns *cur;
394
395         if (!(var = ast_variable_browse(cfg, "global"))) {
396                 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
397                 return AST_MODULE_LOAD_DECLINE;
398         }
399         if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
400                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
401                 tmp = "";       /* connect via UNIX-socket by default */
402         }
403         if (pghostname)
404                 ast_free(pghostname);
405         if (!(pghostname = ast_strdup(tmp))) {
406                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
407                 return AST_MODULE_LOAD_DECLINE;
408         }
409         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
410                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
411                 tmp = "asteriskceldb";
412         }
413         if (pgdbname)
414                 ast_free(pgdbname);
415         if (!(pgdbname = ast_strdup(tmp))) {
416                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
417                 return AST_MODULE_LOAD_DECLINE;
418         }
419         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
420                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
421                 tmp = "asterisk";
422         }
423         if (pgdbuser)
424                 ast_free(pgdbuser);
425         if (!(pgdbuser = ast_strdup(tmp))) {
426                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
427                 return AST_MODULE_LOAD_DECLINE;
428         }
429         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
430                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
431                 tmp = "";
432         }
433         if (pgpassword)
434                 ast_free(pgpassword);
435         if (!(pgpassword = ast_strdup(tmp))) {
436                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
437                 return AST_MODULE_LOAD_DECLINE;
438         }
439         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
440                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
441                 tmp = "5432";
442         }
443         if (pgdbport)
444                 ast_free(pgdbport);
445         if (!(pgdbport = ast_strdup(tmp))) {
446                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
447                 return AST_MODULE_LOAD_DECLINE;
448         }
449         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
450                 ast_log(LOG_WARNING,"CEL table not specified.  Assuming cel\n");
451                 tmp = "cel";
452         }
453         if (table)
454                 ast_free(table);
455         if (!(table = ast_strdup(tmp))) {
456                 return AST_MODULE_LOAD_DECLINE;
457         }
458         if (option_debug) {
459                 if (ast_strlen_zero(pghostname)) {
460                         ast_debug(3, "cel_pgsql: using default unix socket\n");
461                 } else {
462                         ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
463                 }
464                 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
465                 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
466                 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
467                 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
468                 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
469         }
470
471         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
472         if (PQstatus(conn) != CONNECTION_BAD) {
473                 char sqlcmd[512];
474                 char *fname, *ftype, *flen, *fnotnull, *fdef;
475                 char *tableptr;
476                 int i, rows;
477
478                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
479                 connected = 1;
480
481                 /* Remove any schema name from the table */
482                 if ((tableptr = strrchr(table, '.'))) {
483                         tableptr++;
484                 } else {
485                         tableptr = table;
486                 }
487
488                 /* Query the columns */
489                 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);
490                 result = PQexec(conn, sqlcmd);
491                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
492                         pgerror = PQresultErrorMessage(result);
493                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
494                         PQclear(result);
495                         unload_module();
496                         return AST_MODULE_LOAD_DECLINE;
497                 }
498
499                 rows = PQntuples(result);
500                 for (i = 0; i < rows; i++) {
501                         fname = PQgetvalue(result, i, 0);
502                         ftype = PQgetvalue(result, i, 1);
503                         flen = PQgetvalue(result, i, 2);
504                         fnotnull = PQgetvalue(result, i, 3);
505                         fdef = PQgetvalue(result, i, 4);
506                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
507                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
508                         if (cur) {
509                                 sscanf(flen, "%30d", &cur->len);
510                                 cur->name = (char *)cur + sizeof(*cur);
511                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
512                                 strcpy(cur->name, fname);
513                                 strcpy(cur->type, ftype);
514                                 if (*fnotnull == 't') {
515                                         cur->notnull = 1;
516                                 } else {
517                                         cur->notnull = 0;
518                                 }
519                                 if (!ast_strlen_zero(fdef)) {
520                                         cur->hasdefault = 1;
521                                 } else {
522                                         cur->hasdefault = 0;
523                                 }
524                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
525                         }
526                 }
527                 PQclear(result);
528         } else {
529                 pgerror = PQerrorMessage(conn);
530                 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
531                 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
532                 connected = 0;
533         }
534         return AST_MODULE_LOAD_SUCCESS;
535 }
536
537 static int my_load_module(int reload)
538 {
539         struct ast_config *cfg;
540         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
541
542         if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
543                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
544                 return AST_MODULE_LOAD_DECLINE;
545         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
546                 return AST_MODULE_LOAD_SUCCESS;
547         }
548
549         process_my_load_module(cfg);
550         ast_config_destroy(cfg);
551
552         event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
553
554         if (!event_sub) {
555                 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
556                 return AST_MODULE_LOAD_DECLINE;
557         }
558
559         return AST_MODULE_LOAD_SUCCESS;
560 }
561
562 static int load_module(void)
563 {
564         return my_load_module(0);
565 }
566
567 static int reload(void)
568 {
569         my_unload_module();
570         return my_load_module(1);
571 }
572
573 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
574         .load = load_module,
575         .unload = unload_module,
576         .reload = reload,
577         .load_pri = AST_MODPRI_CDR_DRIVER,
578 );