2 * Asterisk -- An open source telephony toolkit.
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
10 * Modified April, 2007; Dec, 2008
11 * Steve Murphy <murf@digium.com>
13 * Modified September 2003
14 * Matthew D. Hardeman <mhardemn@papersoft.com>
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.
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.
29 * \brief PostgreSQL CEL logger
31 * \author Steve Murphy <murf@digium.com>
32 * PostgreSQL http://www.postgresql.org/
35 * \arg \ref Config_cel
36 * PostgreSQL http://www.postgresql.org/
37 * \ingroup cel_drivers
41 <depend>pgsql</depend>
42 <support_level>extended</support_level>
47 ASTERISK_REGISTER_FILE()
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"
59 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
61 #define PGSQL_BACKEND_NAME "CEL PGSQL backend"
63 static char *config = "cel_pgsql.conf";
65 static char *pghostname;
66 static char *pgdbname;
67 static char *pgdbuser;
68 static char *pgpassword;
69 static char *pgappname;
70 static char *pgdbport;
73 static int connected = 0;
74 /* Optimization to reduce number of memory allocations */
75 static int maxsize = 512, maxsize2 = 512;
76 static int usegmtime = 0;
78 /*! \brief show_user_def is off by default */
79 #define CEL_SHOW_USERDEF_DEFAULT 0
81 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
82 static unsigned char cel_show_user_def;
84 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
86 static PGconn *conn = NULL;
87 static PGresult *result = NULL;
93 unsigned int notnull:1;
94 unsigned int hasdefault:1;
95 AST_RWLIST_ENTRY(columns) list;
98 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
100 #define LENGTHEN_BUF1(size) \
102 /* Lengthen buffer, if necessary */ \
103 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
104 if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
105 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", pghostname, table); \
108 AST_RWLIST_UNLOCK(&psql_columns); \
114 #define LENGTHEN_BUF2(size) \
116 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
117 if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
118 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", pghostname, table); \
121 AST_RWLIST_UNLOCK(&psql_columns); \
127 static void pgsql_reconnect(void)
129 struct ast_str *conn_info = ast_str_create(128);
131 ast_log(LOG_ERROR, "Failed to allocate memory for connection string.\n");
140 ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
141 pghostname, pgdbport, pgdbname, pgdbuser);
143 if (!ast_strlen_zero(pgappname)) {
144 ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
147 if (!ast_strlen_zero(pgpassword)) {
148 ast_str_append(&conn_info, 0, " password=%s", pgpassword);
151 conn = PQconnectdb(ast_str_buffer(conn_info));
156 static void pgsql_log(struct ast_event *event)
161 struct ast_cel_event_record record = {
162 .version = AST_CEL_EVENT_RECORD_VERSION,
165 if (ast_cel_fill_record(event, &record)) {
169 ast_mutex_lock(&pgsql_lock);
171 ast_localtime(&record.event_time, &tm, usegmtime ? "GMT" : NULL);
172 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
174 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
176 if (PQstatus(conn) != CONNECTION_BAD) {
179 pgerror = PQerrorMessage(conn);
180 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
181 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
188 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
189 char buf[257], escapebuf[513];
194 goto ast_log_cleanup;
197 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
198 ast_str_set(&sql2, 0, " VALUES (");
200 #define SEP (first ? "" : ",")
202 AST_RWLIST_RDLOCK(&psql_columns);
203 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
204 LENGTHEN_BUF1(strlen(cur->name) + 2);
205 ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
207 if (strcmp(cur->name, "eventtime") == 0) {
208 if (strncmp(cur->type, "int", 3) == 0) {
210 ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
211 } else if (strncmp(cur->type, "float", 5) == 0) {
213 ast_str_append(&sql2, 0, "%s%f",
215 (double) record.event_time.tv_sec +
216 (double) record.event_time.tv_usec / 1000000.0);
218 /* char, hopefully */
220 ast_localtime(&record.event_time, &tm, usegmtime ? "GMT" : NULL);
221 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
222 ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
224 } else if (strcmp(cur->name, "eventtype") == 0) {
225 if (cur->type[0] == 'i') {
226 /* Get integer, no need to escape anything */
228 ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
229 } else if (strncmp(cur->type, "float", 5) == 0) {
231 ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
233 /* Char field, probably */
234 const char *event_name;
236 event_name = (!cel_show_user_def
237 && record.event_type == AST_CEL_USER_DEFINED)
238 ? record.user_defined_name : record.event_name;
239 LENGTHEN_BUF2(strlen(event_name) + 1);
240 ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
242 } else if (strcmp(cur->name, "amaflags") == 0) {
243 if (strncmp(cur->type, "int", 3) == 0) {
244 /* Integer, no need to escape anything */
246 ast_str_append(&sql2, 0, "%s%u", SEP, record.amaflag);
248 /* Although this is a char field, there are no special characters in the values for these fields */
250 ast_str_append(&sql2, 0, "%s'%u'", SEP, record.amaflag);
253 /* Arbitrary field, could be anything */
254 if (strcmp(cur->name, "userdeftype") == 0) {
255 value = record.user_defined_name;
256 } else if (strcmp(cur->name, "cid_name") == 0) {
257 value = record.caller_id_name;
258 } else if (strcmp(cur->name, "cid_num") == 0) {
259 value = record.caller_id_num;
260 } else if (strcmp(cur->name, "cid_ani") == 0) {
261 value = record.caller_id_ani;
262 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
263 value = record.caller_id_rdnis;
264 } else if (strcmp(cur->name, "cid_dnid") == 0) {
265 value = record.caller_id_dnid;
266 } else if (strcmp(cur->name, "exten") == 0) {
267 value = record.extension;
268 } else if (strcmp(cur->name, "context") == 0) {
269 value = record.context;
270 } else if (strcmp(cur->name, "channame") == 0) {
271 value = record.channel_name;
272 } else if (strcmp(cur->name, "appname") == 0) {
273 value = record.application_name;
274 } else if (strcmp(cur->name, "appdata") == 0) {
275 value = record.application_data;
276 } else if (strcmp(cur->name, "accountcode") == 0) {
277 value = record.account_code;
278 } else if (strcmp(cur->name, "peeraccount") == 0) {
279 value = record.peer_account;
280 } else if (strcmp(cur->name, "uniqueid") == 0) {
281 value = record.unique_id;
282 } else if (strcmp(cur->name, "linkedid") == 0) {
283 value = record.linked_id;
284 } else if (strcmp(cur->name, "userfield") == 0) {
285 value = record.user_field;
286 } else if (strcmp(cur->name, "peer") == 0) {
288 } else if (strcmp(cur->name, "extra") == 0) {
289 value = record.extra;
295 ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
296 } else if (strncmp(cur->type, "int", 3) == 0) {
298 if (value && sscanf(value, "%30lld", &whatever) == 1) {
300 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
303 ast_str_append(&sql2, 0, "%s0", SEP);
305 } else if (strncmp(cur->type, "float", 5) == 0) {
306 long double whatever;
307 if (value && sscanf(value, "%30Lf", &whatever) == 1) {
309 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
312 ast_str_append(&sql2, 0, "%s0", SEP);
314 /* XXX Might want to handle dates, times, and other misc fields here XXX */
317 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
321 LENGTHEN_BUF2(strlen(escapebuf) + 3);
322 ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
327 AST_RWLIST_UNLOCK(&psql_columns);
328 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
329 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
331 ast_debug(3, "Inserting a CEL record: [%s].\n", ast_str_buffer(sql));
332 /* Test to be sure we're still connected... */
333 /* If we're connected, and connection is working, good. */
334 /* Otherwise, attempt reconnect. If it fails... sorry... */
335 if (PQstatus(conn) == CONNECTION_OK) {
338 ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
340 if (PQstatus(conn) == CONNECTION_OK) {
341 ast_log(LOG_NOTICE, "Connection reestablished.\n");
344 pgerror = PQerrorMessage(conn);
345 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
346 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
350 goto ast_log_cleanup;
353 result = PQexec(conn, ast_str_buffer(sql));
354 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
355 pgerror = PQresultErrorMessage(result);
356 ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
357 ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
358 ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
360 if (PQstatus(conn) == CONNECTION_OK) {
361 ast_log(LOG_NOTICE, "Connection reestablished.\n");
364 result = PQexec(conn, ast_str_buffer(sql));
365 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
366 pgerror = PQresultErrorMessage(result);
367 ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
368 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
372 goto ast_log_cleanup;
376 /* Next time, just allocate buffers that are that big to start with. */
377 if (ast_str_strlen(sql) > maxsize) {
378 maxsize = ast_str_strlen(sql);
380 if (ast_str_strlen(sql2) > maxsize2) {
381 maxsize2 = ast_str_strlen(sql2);
389 ast_mutex_unlock(&pgsql_lock);
392 static int my_unload_module(void)
394 struct columns *current;
396 ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
397 AST_RWLIST_WRLOCK(&psql_columns);
403 ast_free(pghostname);
415 ast_free(pgpassword);
430 while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
433 AST_RWLIST_UNLOCK(&psql_columns);
437 static int unload_module(void)
439 return my_unload_module();
442 static int process_my_load_module(struct ast_config *cfg)
444 struct ast_variable *var;
450 if (!(var = ast_variable_browse(cfg, "global"))) {
451 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
452 return AST_MODULE_LOAD_DECLINE;
454 if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
455 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
456 tmp = ""; /* connect via UNIX-socket by default */
459 ast_free(pghostname);
460 if (!(pghostname = ast_strdup(tmp))) {
461 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
462 return AST_MODULE_LOAD_DECLINE;
464 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
465 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
466 tmp = "asteriskceldb";
470 if (!(pgdbname = ast_strdup(tmp))) {
471 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
472 return AST_MODULE_LOAD_DECLINE;
474 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
475 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
480 if (!(pgdbuser = ast_strdup(tmp))) {
481 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
482 return AST_MODULE_LOAD_DECLINE;
484 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
485 ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
489 ast_free(pgpassword);
490 if (!(pgpassword = ast_strdup(tmp))) {
491 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
492 return AST_MODULE_LOAD_DECLINE;
494 if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
500 if (!(pgappname = ast_strdup(tmp))) {
501 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying appname info\n");
502 return AST_MODULE_LOAD_DECLINE;
505 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
506 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
511 if (!(pgdbport = ast_strdup(tmp))) {
512 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
513 return AST_MODULE_LOAD_DECLINE;
515 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
516 ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
521 if (!(table = ast_strdup(tmp))) {
522 return AST_MODULE_LOAD_DECLINE;
524 cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
525 if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
526 cel_show_user_def = ast_true(tmp) ? 1 : 0;
528 if ((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) {
529 usegmtime = ast_true(tmp);
534 if (ast_strlen_zero(pghostname)) {
535 ast_debug(3, "cel_pgsql: using default unix socket\n");
537 ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
539 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
540 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
541 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
542 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
543 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
544 ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
545 cel_show_user_def ? "Yes" : "No");
549 if (PQstatus(conn) != CONNECTION_BAD) {
551 char *fname, *ftype, *flen, *fnotnull, *fdef;
555 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
558 /* Remove any schema name from the table */
559 if ((tableptr = strrchr(table, '.'))) {
565 /* Query the columns */
566 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);
567 result = PQexec(conn, sqlcmd);
568 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
569 pgerror = PQresultErrorMessage(result);
570 ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
573 return AST_MODULE_LOAD_DECLINE;
576 rows = PQntuples(result);
577 for (i = 0; i < rows; i++) {
578 fname = PQgetvalue(result, i, 0);
579 ftype = PQgetvalue(result, i, 1);
580 flen = PQgetvalue(result, i, 2);
581 fnotnull = PQgetvalue(result, i, 3);
582 fdef = PQgetvalue(result, i, 4);
583 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
584 cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
586 sscanf(flen, "%30d", &cur->len);
587 cur->name = (char *)cur + sizeof(*cur);
588 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
589 strcpy(cur->name, fname);
590 strcpy(cur->type, ftype);
591 if (*fnotnull == 't') {
596 if (!ast_strlen_zero(fdef)) {
601 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
606 pgerror = PQerrorMessage(conn);
607 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
608 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
613 return AST_MODULE_LOAD_SUCCESS;
616 static int my_load_module(int reload)
618 struct ast_config *cfg;
619 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
621 if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
622 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
623 return AST_MODULE_LOAD_DECLINE;
624 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
625 return AST_MODULE_LOAD_SUCCESS;
632 process_my_load_module(cfg);
633 ast_config_destroy(cfg);
635 if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
636 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
637 return AST_MODULE_LOAD_DECLINE;
640 return AST_MODULE_LOAD_SUCCESS;
643 static int load_module(void)
645 return my_load_module(0);
648 static int reload(void)
650 return my_load_module(1);
653 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
654 .support_level = AST_MODULE_SUPPORT_EXTENDED,
656 .unload = unload_module,
658 .load_pri = AST_MODPRI_CDR_DRIVER,