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