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_FILE_VERSION(__FILE__, "$Revision$")
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";
64 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
65 static int connected = 0;
66 static int maxsize = 512, maxsize2 = 512;
68 /*! \brief show_user_def is off by default */
69 #define CEL_SHOW_USERDEF_DEFAULT 0
71 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
72 static unsigned char cel_show_user_def;
74 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
76 static PGconn *conn = NULL;
77 static PGresult *result = NULL;
83 unsigned int notnull:1;
84 unsigned int hasdefault:1;
85 AST_RWLIST_ENTRY(columns) list;
88 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
90 #define LENGTHEN_BUF1(size) \
92 /* Lengthen buffer, if necessary */ \
93 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
94 if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
95 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
98 AST_RWLIST_UNLOCK(&psql_columns); \
104 #define LENGTHEN_BUF2(size) \
106 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
107 if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
108 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
111 AST_RWLIST_UNLOCK(&psql_columns); \
117 static void pgsql_log(struct ast_event *event)
122 struct ast_cel_event_record record = {
123 .version = AST_CEL_EVENT_RECORD_VERSION,
126 if (ast_cel_fill_record(event, &record)) {
130 ast_mutex_lock(&pgsql_lock);
132 ast_localtime(&record.event_time, &tm, NULL);
133 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
135 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
136 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
137 if (PQstatus(conn) != CONNECTION_BAD) {
140 pgerror = PQerrorMessage(conn);
141 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
142 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
149 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
150 char buf[257], escapebuf[513];
155 goto ast_log_cleanup;
158 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
159 ast_str_set(&sql2, 0, " VALUES (");
161 #define SEP (first ? "" : ",")
163 AST_RWLIST_RDLOCK(&psql_columns);
164 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
165 LENGTHEN_BUF1(strlen(cur->name) + 2);
166 ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
168 if (strcmp(cur->name, "eventtime") == 0) {
169 if (strncmp(cur->type, "int", 3) == 0) {
171 ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
172 } else if (strncmp(cur->type, "float", 5) == 0) {
174 ast_str_append(&sql2, 0, "%s%f",
176 (double) record.event_time.tv_sec +
177 (double) record.event_time.tv_usec / 1000000.0);
179 /* char, hopefully */
181 ast_localtime(&record.event_time, &tm, NULL);
182 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
183 ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
185 } else if (strcmp(cur->name, "eventtype") == 0) {
186 if (cur->type[0] == 'i') {
187 /* Get integer, no need to escape anything */
189 ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
190 } else if (strncmp(cur->type, "float", 5) == 0) {
192 ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
194 /* Char field, probably */
195 const char *event_name;
197 event_name = (!cel_show_user_def
198 && record.event_type == AST_CEL_USER_DEFINED)
199 ? record.user_defined_name : record.event_name;
200 LENGTHEN_BUF2(strlen(event_name) + 1);
201 ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
203 } else if (strcmp(cur->name, "amaflags") == 0) {
204 if (strncmp(cur->type, "int", 3) == 0) {
205 /* Integer, no need to escape anything */
207 ast_str_append(&sql2, 0, "%s%d", SEP, record.amaflag);
209 /* Although this is a char field, there are no special characters in the values for these fields */
211 ast_str_append(&sql2, 0, "%s'%d'", SEP, record.amaflag);
214 /* Arbitrary field, could be anything */
215 if (strcmp(cur->name, "userdeftype") == 0) {
216 value = record.user_defined_name;
217 } else if (strcmp(cur->name, "cid_name") == 0) {
218 value = record.caller_id_name;
219 } else if (strcmp(cur->name, "cid_num") == 0) {
220 value = record.caller_id_num;
221 } else if (strcmp(cur->name, "cid_ani") == 0) {
222 value = record.caller_id_ani;
223 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
224 value = record.caller_id_rdnis;
225 } else if (strcmp(cur->name, "cid_dnid") == 0) {
226 value = record.caller_id_dnid;
227 } else if (strcmp(cur->name, "exten") == 0) {
228 value = record.extension;
229 } else if (strcmp(cur->name, "context") == 0) {
230 value = record.context;
231 } else if (strcmp(cur->name, "channame") == 0) {
232 value = record.channel_name;
233 } else if (strcmp(cur->name, "appname") == 0) {
234 value = record.application_name;
235 } else if (strcmp(cur->name, "appdata") == 0) {
236 value = record.application_data;
237 } else if (strcmp(cur->name, "accountcode") == 0) {
238 value = record.account_code;
239 } else if (strcmp(cur->name, "peeraccount") == 0) {
240 value = record.peer_account;
241 } else if (strcmp(cur->name, "uniqueid") == 0) {
242 value = record.unique_id;
243 } else if (strcmp(cur->name, "linkedid") == 0) {
244 value = record.linked_id;
245 } else if (strcmp(cur->name, "userfield") == 0) {
246 value = record.user_field;
247 } else if (strcmp(cur->name, "peer") == 0) {
249 } else if (strcmp(cur->name, "extra") == 0) {
250 value = record.extra;
256 ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
257 } else if (strncmp(cur->type, "int", 3) == 0) {
259 if (value && sscanf(value, "%30lld", &whatever) == 1) {
261 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
264 ast_str_append(&sql2, 0, "%s0", SEP);
266 } else if (strncmp(cur->type, "float", 5) == 0) {
267 long double whatever;
268 if (value && sscanf(value, "%30Lf", &whatever) == 1) {
270 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
273 ast_str_append(&sql2, 0, "%s0", SEP);
275 /* XXX Might want to handle dates, times, and other misc fields here XXX */
278 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
282 LENGTHEN_BUF2(strlen(escapebuf) + 3);
283 ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
288 AST_RWLIST_UNLOCK(&psql_columns);
289 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
290 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
291 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
293 ast_debug(2, "inserting a CEL record.\n");
294 /* Test to be sure we're still connected... */
295 /* If we're connected, and connection is working, good. */
296 /* Otherwise, attempt reconnect. If it fails... sorry... */
297 if (PQstatus(conn) == CONNECTION_OK) {
300 ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
302 if (PQstatus(conn) == CONNECTION_OK) {
303 ast_log(LOG_NOTICE, "Connection reestablished.\n");
306 pgerror = PQerrorMessage(conn);
307 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
308 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
312 goto ast_log_cleanup;
315 result = PQexec(conn, ast_str_buffer(sql));
316 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
317 pgerror = PQresultErrorMessage(result);
318 ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
319 ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
320 ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
322 if (PQstatus(conn) == CONNECTION_OK) {
323 ast_log(LOG_NOTICE, "Connection reestablished.\n");
326 result = PQexec(conn, ast_str_buffer(sql));
327 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
328 pgerror = PQresultErrorMessage(result);
329 ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
330 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
334 goto ast_log_cleanup;
343 ast_mutex_unlock(&pgsql_lock);
346 static int my_unload_module(void)
348 struct columns *current;
350 ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
351 AST_RWLIST_WRLOCK(&psql_columns);
357 ast_free(pghostname);
369 ast_free(pgpassword);
380 while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
383 AST_RWLIST_UNLOCK(&psql_columns);
387 static int unload_module(void)
389 return my_unload_module();
392 static int process_my_load_module(struct ast_config *cfg)
394 struct ast_variable *var;
400 if (!(var = ast_variable_browse(cfg, "global"))) {
401 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
402 return AST_MODULE_LOAD_DECLINE;
404 if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
405 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
406 tmp = ""; /* connect via UNIX-socket by default */
409 ast_free(pghostname);
410 if (!(pghostname = ast_strdup(tmp))) {
411 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
412 return AST_MODULE_LOAD_DECLINE;
414 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
415 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
416 tmp = "asteriskceldb";
420 if (!(pgdbname = ast_strdup(tmp))) {
421 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
422 return AST_MODULE_LOAD_DECLINE;
424 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
425 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
430 if (!(pgdbuser = ast_strdup(tmp))) {
431 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
432 return AST_MODULE_LOAD_DECLINE;
434 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
435 ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
439 ast_free(pgpassword);
440 if (!(pgpassword = ast_strdup(tmp))) {
441 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
442 return AST_MODULE_LOAD_DECLINE;
444 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
445 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
450 if (!(pgdbport = ast_strdup(tmp))) {
451 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
452 return AST_MODULE_LOAD_DECLINE;
454 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
455 ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
460 if (!(table = ast_strdup(tmp))) {
461 return AST_MODULE_LOAD_DECLINE;
463 cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
464 if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
465 cel_show_user_def = ast_true(tmp) ? 1 : 0;
468 if (ast_strlen_zero(pghostname)) {
469 ast_debug(3, "cel_pgsql: using default unix socket\n");
471 ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
473 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
474 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
475 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
476 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
477 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
478 ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
479 cel_show_user_def ? "Yes" : "No");
482 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
483 if (PQstatus(conn) != CONNECTION_BAD) {
485 char *fname, *ftype, *flen, *fnotnull, *fdef;
489 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
492 /* Remove any schema name from the table */
493 if ((tableptr = strrchr(table, '.'))) {
499 /* Query the columns */
500 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);
501 result = PQexec(conn, sqlcmd);
502 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
503 pgerror = PQresultErrorMessage(result);
504 ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
507 return AST_MODULE_LOAD_DECLINE;
510 rows = PQntuples(result);
511 for (i = 0; i < rows; i++) {
512 fname = PQgetvalue(result, i, 0);
513 ftype = PQgetvalue(result, i, 1);
514 flen = PQgetvalue(result, i, 2);
515 fnotnull = PQgetvalue(result, i, 3);
516 fdef = PQgetvalue(result, i, 4);
517 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
518 cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
520 sscanf(flen, "%30d", &cur->len);
521 cur->name = (char *)cur + sizeof(*cur);
522 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
523 strcpy(cur->name, fname);
524 strcpy(cur->type, ftype);
525 if (*fnotnull == 't') {
530 if (!ast_strlen_zero(fdef)) {
535 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
540 pgerror = PQerrorMessage(conn);
541 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
542 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
545 return AST_MODULE_LOAD_SUCCESS;
548 static int my_load_module(int reload)
550 struct ast_config *cfg;
551 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
553 if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
554 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
555 return AST_MODULE_LOAD_DECLINE;
556 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
557 return AST_MODULE_LOAD_SUCCESS;
564 process_my_load_module(cfg);
565 ast_config_destroy(cfg);
567 if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
568 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
569 return AST_MODULE_LOAD_DECLINE;
572 return AST_MODULE_LOAD_SUCCESS;
575 static int load_module(void)
577 return my_load_module(0);
580 static int reload(void)
582 return my_load_module(1);
585 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
587 .unload = unload_module,
589 .load_pri = AST_MODPRI_CDR_DRIVER,