2 * Asterisk -- A telephony toolkit for Linux.
4 * Copyright (C) 1999-2005, Digium, Inc.
6 * Manuel Guesdon <mguesdon@oxymium.net> - Postgresql RealTime Driver Author/Adaptor
7 * Mark Spencer <markster@digium.com> - Asterisk Author
8 * Matthew Boehm <mboehm@cytelcom.com> - MySQL RealTime Driver Author
10 * res_config_pgsql.c <Postgresql plugin for RealTime configuration engine>
12 * v1.0 - (07-11-05) - Initial version based on res_config_mysql v2.0
17 * \brief Postgresql plugin for Asterisk RealTime Architecture
19 * \author Mark Spencer <markster@digium.com>
20 * \author Manuel Guesdon <mguesdon@oxymium.net> - Postgresql RealTime Driver Author/Adaptor
22 * \arg http://www.postgresql.org
27 #include <libpq-fe.h> /* PostgreSQL */
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/config.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/options.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/cli.h"
44 static char *res_config_pgsql_desc = "Postgresql RealTime Configuration Driver";
46 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
47 #define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
48 PGconn* pgsqlConn = NULL;
49 static char dbhost[50]="";
50 static char dbuser[50]="";
51 static char dbpass[50]="";
52 static char dbname[50]="";
53 static char dbsock[50]="";
54 static int dbport=5432;
55 static time_t connect_time=0;
57 static int parse_config(void);
58 static int pgsql_reconnect(const char *database);
59 static int realtime_pgsql_status(int fd, int argc, char **argv);
65 static char cli_realtime_pgsql_status_usage[] =
66 "Usage: realtime pgsql status\n"
67 " Shows connection information for the Postgresql RealTime driver\n";
69 static struct ast_cli_entry cli_realtime_pgsql_status = {
70 { "realtime", "pgsql", "status", NULL }, realtime_pgsql_status,
71 "Shows connection information for the Postgresql RealTime driver", cli_realtime_pgsql_status_usage, NULL };
73 static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap)
75 PGresult *result = NULL;
81 const char *newparam, *newval;
82 struct ast_variable *var=NULL, *prev=NULL;
85 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
89 /* Get the first parameter and first value in our list of passed paramater/value pairs */
90 newparam = va_arg(ap, const char *);
91 newval = va_arg(ap, const char *);
92 if(!newparam || !newval) {
93 ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
102 /* Create the first part of the query using the first parameter/value pairs we just extracted
103 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
105 if(!strchr(newparam, ' ')) op = " ="; else op = "";
107 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, newval);
108 while((newparam = va_arg(ap, const char *))) {
109 newval = va_arg(ap, const char *);
110 if(!strchr(newparam, ' ')) op = " ="; else op = "";
111 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam, op, newval);
115 /* We now have our complete statement; Lets connect to the server and execute it. */
116 ast_mutex_lock(&pgsql_lock);
117 if(!pgsql_reconnect(database)) {
118 ast_mutex_unlock(&pgsql_lock);
122 if(!(result=PQexec(pgsqlConn, sql))) {
123 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
124 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
125 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
126 ast_mutex_unlock(&pgsql_lock);
130 ExecStatusType result_status=PQresultStatus(result);
131 if (result_status!=PGRES_COMMAND_OK
132 && result_status!=PGRES_TUPLES_OK
133 && result_status!=PGRES_NONFATAL_ERROR)
135 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
136 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
137 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
138 PQresultErrorMessage(result),PQresStatus(result_status));
139 ast_mutex_unlock(&pgsql_lock);
144 ast_log(LOG_DEBUG, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql);
146 if((num_rows=PQntuples(result))>0) {
149 int numFields = PQnfields(result);
150 char** fieldnames=NULL;
152 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
154 fieldnames=malloc(numFields*sizeof(char*));
156 /* If I can't alloc memory at this point, why bother doing anything else? */
157 ast_log(LOG_WARNING, "Out of memory!\n");
158 ast_mutex_unlock(&pgsql_lock);
162 for(i = 0; i < numFields; i++)
163 fieldnames[i]=PQfname(result,i);
164 for(rowIndex=0;rowIndex<num_rows;rowIndex++)
166 for(i = 0; i < numFields; i++) {
167 stringp = PQgetvalue(result,rowIndex,i);
169 chunk = strsep(&stringp, ";");
170 if(chunk && !ast_strlen_zero(ast_strip(chunk))) {
172 prev->next = ast_variable_new(fieldnames[i], chunk);
177 prev = var = ast_variable_new(fieldnames[i], chunk);
185 ast_log(LOG_WARNING, "Postgresql RealTime: Could not find any rows in table %s.\n", table);
188 ast_mutex_unlock(&pgsql_lock);
194 static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap)
196 PGresult *result = NULL;
199 const char *initfield = NULL;
203 const char *newparam, *newval;
204 struct ast_realloca ra;
205 struct ast_variable *var=NULL;
206 struct ast_config *cfg = NULL;
207 struct ast_category *cat = NULL;
210 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
214 memset(&ra, 0, sizeof(ra));
216 cfg = ast_config_new();
218 /* If I can't alloc memory at this point, why bother doing anything else? */
219 ast_log(LOG_WARNING, "Out of memory!\n");
223 /* Get the first parameter and first value in our list of passed paramater/value pairs */
224 newparam = va_arg(ap, const char *);
225 newval = va_arg(ap, const char *);
226 if(!newparam || !newval) {
227 ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
236 initfield = ast_strdupa(newparam);
237 if(initfield && (op = strchr(initfield, ' '))) {
241 /* Create the first part of the query using the first parameter/value pairs we just extracted
242 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
244 if(!strchr(newparam, ' ')) op = " ="; else op = "";
246 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, newval);
247 while((newparam = va_arg(ap, const char *))) {
248 newval = va_arg(ap, const char *);
249 if(!strchr(newparam, ' ')) op = " ="; else op = "";
250 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam, op, newval);
254 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
259 /* We now have our complete statement; Lets connect to the server and execute it. */
260 ast_mutex_lock(&pgsql_lock);
261 if(!pgsql_reconnect(database)) {
262 ast_mutex_unlock(&pgsql_lock);
266 if(!(result=PQexec(pgsqlConn, sql))) {
267 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
268 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
269 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
270 ast_mutex_unlock(&pgsql_lock);
274 ExecStatusType result_status=PQresultStatus(result);
275 if (result_status!=PGRES_COMMAND_OK
276 && result_status!=PGRES_TUPLES_OK
277 && result_status!=PGRES_NONFATAL_ERROR)
279 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
280 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
281 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
282 PQresultErrorMessage(result),PQresStatus(result_status));
283 ast_mutex_unlock(&pgsql_lock);
288 ast_log(LOG_DEBUG, "2Postgresql RealTime: Result=%p Query: %s\n", result, sql);
290 if((num_rows=PQntuples(result))>0) {
291 int numFields = PQnfields(result);
294 char** fieldnames=NULL;
296 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
298 fieldnames=malloc(numFields*sizeof(char*));
300 /* If I can't alloc memory at this point, why bother doing anything else? */
301 ast_log(LOG_WARNING, "Out of memory!\n");
302 ast_mutex_unlock(&pgsql_lock);
306 for(i = 0; i < numFields; i++)
307 fieldnames[i]=PQfname(result,i);
309 for(rowIndex=0;rowIndex<num_rows;rowIndex++)
312 cat = ast_category_new("");
314 ast_log(LOG_WARNING, "Out of memory!\n");
317 for(i = 0; i < numFields; i++) {
318 stringp = PQgetvalue(result,rowIndex,i);
320 chunk = strsep(&stringp, ";");
321 if(chunk && !ast_strlen_zero(ast_strip(chunk))) {
322 if(initfield && !strcmp(initfield, fieldnames[i])) {
323 ast_category_rename(cat, chunk);
325 var = ast_variable_new(fieldnames[i], chunk);
326 ast_variable_append(cat, var);
330 ast_category_append(cfg, cat);
334 ast_log(LOG_WARNING, "Postgresql RealTime: Could not find any rows in table %s.\n", table);
337 ast_mutex_unlock(&pgsql_lock);
343 static int update_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
345 PGresult* result = NULL;
348 const char *newparam, *newval;
351 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
355 /* Get the first parameter and first value in our list of passed paramater/value pairs */
356 newparam = va_arg(ap, const char *);
357 newval = va_arg(ap, const char *);
358 if(!newparam || !newval) {
359 ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
368 /* Create the first part of the query using the first parameter/value pairs we just extracted
369 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
371 snprintf(sql, sizeof(sql), "UPDATE %s SET %s = '%s'", table, newparam, newval);
372 while((newparam = va_arg(ap, const char *))) {
373 newval = va_arg(ap, const char *);
374 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s = '%s'", newparam, newval);
377 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s = '%s'", keyfield, lookup);
379 ast_log(LOG_DEBUG,"Postgresql RealTime: Update SQL: %s\n", sql);
381 /* We now have our complete statement; Lets connect to the server and execute it. */
382 ast_mutex_lock(&pgsql_lock);
383 if(!pgsql_reconnect(database)) {
384 ast_mutex_unlock(&pgsql_lock);
388 if(!(result=PQexec(pgsqlConn, sql))) {
389 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
390 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
391 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
392 ast_mutex_unlock(&pgsql_lock);
396 ExecStatusType result_status=PQresultStatus(result);
397 if (result_status!=PGRES_COMMAND_OK
398 && result_status!=PGRES_TUPLES_OK
399 && result_status!=PGRES_NONFATAL_ERROR)
401 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
402 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
403 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
404 PQresultErrorMessage(result),PQresStatus(result_status));
405 ast_mutex_unlock(&pgsql_lock);
410 numrows = atoi(PQcmdTuples(result));
411 ast_mutex_unlock(&pgsql_lock);
413 ast_log(LOG_DEBUG,"Postgresql RealTime: Updated %d rows on table: %s\n", numrows, table);
415 /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
416 * An integer greater than zero indicates the number of rows affected
417 * Zero indicates that no records were updated
418 * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
427 static struct ast_config *config_pgsql(const char *database, const char *table, const char *file, struct ast_config *cfg)
429 PGresult *result = NULL;
431 struct ast_config *new;
432 struct ast_variable *cur_v, *new_v;
433 struct ast_category *cur_cat, *new_cat;
438 int last_cat_metric = 0;
442 if(!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
443 ast_log(LOG_WARNING, "Postgresql RealTime: Cannot configure myself.\n");
447 snprintf(sql, sizeof(sql), "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id", table, file);
449 ast_log(LOG_DEBUG, "Postgresql RealTime: Static SQL: %s\n", sql);
451 /* We now have our complete statement; Lets connect to the server and execute it. */
452 ast_mutex_lock(&pgsql_lock);
453 if(!pgsql_reconnect(database)) {
454 ast_mutex_unlock(&pgsql_lock);
458 if(!(result=PQexec(pgsqlConn, sql))) {
459 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
460 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
461 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
462 ast_mutex_unlock(&pgsql_lock);
466 ExecStatusType result_status=PQresultStatus(result);
467 if (result_status!=PGRES_COMMAND_OK
468 && result_status!=PGRES_TUPLES_OK
469 && result_status!=PGRES_NONFATAL_ERROR)
471 ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
472 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
473 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
474 PQresultErrorMessage(result),PQresStatus(result_status));
475 ast_mutex_unlock(&pgsql_lock);
480 if((num_rows=PQntuples(result))>0) {
481 int numFields = PQnfields(result);
484 char** fieldnames=NULL;
486 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
488 fieldnames=malloc(numFields*sizeof(char*));
490 /* If I can't alloc memory at this point, why bother doing anything else? */
491 ast_log(LOG_WARNING, "Out of memory!\n");
492 ast_mutex_unlock(&pgsql_lock);
496 for(i = 0; i < numFields; i++)
497 fieldnames[i]=PQfname(result,i);
499 for(rowIndex=0;rowIndex<num_rows;rowIndex++)
501 char* field_category=PQgetvalue(result,rowIndex,0);
502 char* field_var_name=PQgetvalue(result,rowIndex,1);
503 char* field_var_val=PQgetvalue(result,rowIndex,2);
504 char* field_cat_metric=PQgetvalue(result,rowIndex,3);
505 if(!strcmp(field_var_name, "#include")) {
506 if (!ast_config_internal_load(field_var_val, cfg)) {
508 ast_mutex_unlock(&pgsql_lock);
514 if(strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
515 cur_cat = ast_category_new(field_category);
517 ast_log(LOG_WARNING, "Out of memory!\n");
520 strcpy(last, field_category);
521 last_cat_metric = atoi(field_cat_metric);
522 ast_category_append(cfg, cur_cat);
524 new_v = ast_variable_new(field_var_name, field_var_val);
525 ast_variable_append(cur_cat, new_v);
528 ast_log(LOG_WARNING, "Postgresql RealTime: Could not find config '%s' in database.\n", file);
532 ast_mutex_unlock(&pgsql_lock);
537 static struct ast_config_engine pgsql_engine = {
539 .load_func = config_pgsql,
540 .realtime_func = realtime_pgsql,
541 .realtime_multi_func = realtime_multi_pgsql,
542 .update_func = update_pgsql
545 int load_module (void)
549 ast_mutex_lock(&pgsql_lock);
551 if(!pgsql_reconnect(NULL)) {
552 ast_log(LOG_WARNING, "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
553 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n", PQerrorMessage(pgsqlConn));
556 ast_config_engine_register(&pgsql_engine);
558 ast_verbose("Postgresql RealTime driver loaded.\n");
560 ast_cli_register(&cli_realtime_pgsql_status);
562 ast_mutex_unlock(&pgsql_lock);
567 int unload_module (void)
569 /* Aquire control before doing anything to the module itself. */
570 ast_mutex_lock(&pgsql_lock);
577 ast_cli_unregister(&cli_realtime_pgsql_status);
578 ast_config_engine_deregister(&pgsql_engine);
580 ast_verbose("Postgresql RealTime unloaded.\n");
583 STANDARD_HANGUP_LOCALUSERS;
585 /* Unlock so something else can destroy the lock. */
586 ast_mutex_unlock(&pgsql_lock);
593 /* Aquire control before doing anything to the module itself. */
594 ast_mutex_lock(&pgsql_lock);
603 if(!pgsql_reconnect(NULL)) {
604 ast_log(LOG_WARNING, "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
605 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n", PQerrorMessage(pgsqlConn));
608 ast_verbose(VERBOSE_PREFIX_2 "Postgresql RealTime reloaded.\n");
610 /* Done reloading. Release lock so others can now use driver. */
611 ast_mutex_unlock(&pgsql_lock);
616 int parse_config (void)
618 struct ast_config *config;
621 config = ast_config_load(RES_CONFIG_PGSQL_CONF);
624 if(!(s=ast_variable_retrieve(config, "general", "dbuser"))) {
625 ast_log(LOG_WARNING, "Postgresql RealTime: No database user found, using 'asterisk' as default.\n");
626 strncpy(dbuser, "asterisk", sizeof(dbuser) - 1);
628 strncpy(dbuser, s, sizeof(dbuser) - 1);
631 if(!(s=ast_variable_retrieve(config, "general", "dbpass"))) {
632 ast_log(LOG_WARNING, "Postgresql RealTime: No database password found, using 'asterisk' as default.\n");
633 strncpy(dbpass, "asterisk", sizeof(dbpass) - 1);
635 strncpy(dbpass, s, sizeof(dbpass) - 1);
638 if(!(s=ast_variable_retrieve(config, "general", "dbhost"))) {
639 ast_log(LOG_WARNING, "Postgresql RealTime: No database host found, using localhost via socket.\n");
642 strncpy(dbhost, s, sizeof(dbhost) - 1);
645 if(!(s=ast_variable_retrieve(config, "general", "dbname"))) {
646 ast_log(LOG_WARNING, "Postgresql RealTime: No database name found, using 'asterisk' as default.\n");
647 strncpy(dbname, "asterisk", sizeof(dbname) - 1);
649 strncpy(dbname, s, sizeof(dbname) - 1);
652 if(!(s=ast_variable_retrieve(config, "general", "dbport"))) {
653 ast_log(LOG_WARNING, "Postgresql RealTime: No database port found, using 5432 as default.\n");
659 if(dbhost && !(s=ast_variable_retrieve(config, "general", "dbsock"))) {
660 ast_log(LOG_WARNING, "Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default.\n");
661 strncpy(dbsock, "/tmp/pgsql.sock", sizeof(dbsock) - 1);
663 strncpy(dbsock, s, sizeof(dbsock) - 1);
666 ast_config_destroy(config);
669 ast_log(LOG_DEBUG, "Postgresql RealTime Host: %s\n", dbhost);
670 ast_log(LOG_DEBUG, "Postgresql RealTime Port: %i\n", dbport);
672 ast_log(LOG_DEBUG, "Postgresql RealTime Socket: %s\n", dbsock);
674 ast_log(LOG_DEBUG, "Postgresql RealTime User: %s\n", dbuser);
675 ast_log(LOG_DEBUG, "Postgresql RealTime Password: %s\n", dbpass);
676 ast_log(LOG_DEBUG, "Postgresql RealTime DBName: %s\n", dbname);
681 char *description (void)
683 return res_config_pgsql_desc;
688 /* Try and get a lock. If unsuccessful, than that means another thread is using the pgsql object. */
689 if(ast_mutex_trylock(&pgsql_lock)) {
690 ast_log(LOG_DEBUG, "Postgresql RealTime: Module usage count is 1.\n");
693 ast_mutex_unlock(&pgsql_lock);
699 return ASTERISK_GPL_KEY;
702 static int pgsql_reconnect(const char *database)
704 char my_database[50];
706 if(!database || ast_strlen_zero(database))
707 ast_copy_string(my_database, dbname, sizeof(my_database));
709 ast_copy_string(my_database, database, sizeof(my_database));
711 /* mutex lock should have been locked before calling this function. */
713 if (pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
718 if((!pgsqlConn) && (dbhost || dbsock) && dbuser && dbpass && my_database) {
720 unsigned int size=100
724 +strlen(my_database);
725 connInfo=malloc(size);
728 ast_log(LOG_WARNING, "Postgresql RealTime: Insufficient memory to allocate Pgsql resource.\n");
733 sprintf(connInfo,"host=%s port=%d dbname=%s user=%s password=%s",
734 dbhost,dbport,my_database,dbuser,dbpass);
735 ast_log(LOG_DEBUG, "%u connInfo=%s\n", size,connInfo);
736 pgsqlConn=PQconnectdb(connInfo);
737 ast_log(LOG_DEBUG, "%u connInfo=%s\n", size,connInfo);
740 ast_log(LOG_DEBUG, "pgsqlConn=%p\n", pgsqlConn);
743 ast_log(LOG_DEBUG, "Postgresql RealTime: Successfully connected to database.\n");
744 connect_time = time(NULL);
747 ast_log(LOG_ERROR, "Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info.\n", dbname, dbhost);
748 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n", PQresultErrorMessage(NULL));
754 ast_log(LOG_DEBUG, "Postgresql RealTime: Everything is fine.\n");
759 static int realtime_pgsql_status(int fd, int argc, char **argv)
761 char status[256], status2[100] = "";
762 int ctime = time(NULL) - connect_time;
764 if(pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
766 snprintf(status, 255, "Connected to %s@%s, port %d", dbname, dbhost, dbport);
768 snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
770 snprintf(status, 255, "Connected to %s@%s", dbname, dbhost);
773 if(dbuser && *dbuser) {
774 snprintf(status2, 99, " with username %s", dbuser);
777 if (ctime > 31536000) {
778 ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
779 } else if (ctime > 86400) {
780 ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
781 } else if (ctime > 3600) {
782 ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
783 } else if (ctime > 60) {
784 ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
786 ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
789 return RESULT_SUCCESS;
791 return RESULT_FAILURE;