From a922cd5558866b98352968d1fd3c855e9ab59e56 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Mon, 1 Aug 2016 14:57:40 +0900 Subject: [PATCH] Suppress connection error display in `repmgr cluster show` This prevents connection error messages being mixed in with `repmgr cluster show` output. Error message output can still be enabled with the --verbose flag. Fixes GitHub #215 --- HISTORY | 4 ++++ README.md | 13 ++++++++----- dbutils.c | 51 +++++++++++++++++++++++++++++++++++++++------------ dbutils.h | 7 ++++--- log.h | 4 +++- repmgr.c | 8 ++++---- 6 files changed, 62 insertions(+), 25 deletions(-) diff --git a/HISTORY b/HISTORY index 49e623b4..72508957 100644 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,7 @@ +3.2 2016- + repmgr: suppress connection error display in `repmgr cluster show` + unless `--verbose` supplied (Ian) + 3.1.4 2016-07-12 repmgr: new configuration option for setting "restore_command" in the recovery.conf file generated by repmgr (Martín) diff --git a/README.md b/README.md index bbe4b86b..b798d308 100644 --- a/README.md +++ b/README.md @@ -1382,17 +1382,20 @@ which contains connection details for the local database. when analyzing connectivity from a particular node. This command requires a valid `repmgr.conf` file to be provided; no - additional arguments are required. + additional arguments are needed. Example: $ repmgr -f /etc/repmgr.conf cluster show Role | Name | Upstream | Connection String - ----------+-------|----------|-------------------------------------------- - * master | node1 | | host=repmgr_node1 dbname=repmgr user=repmgr - standby | node2 | node1 | host=repmgr_node1 dbname=repmgr user=repmgr - standby | node3 | node2 | host=repmgr_node1 dbname=repmgr user=repmgr + ----------+-------|----------|---------------------------------------- + * master | node1 | | host=db_node1 dbname=repmgr user=repmgr + standby | node2 | node1 | host=db_node2 dbname=repmgr user=repmgr + standby | node3 | node2 | host=db_node3 dbname=repmgr user=repmgr + + To show database connection errors when polling nodes, run the command in + `--verbose` mode. * `cluster cleanup` diff --git a/dbutils.c b/dbutils.c index c962ba0d..f2ef645d 100644 --- a/dbutils.c +++ b/dbutils.c @@ -34,7 +34,7 @@ char repmgr_schema_quoted[MAXLEN] = ""; static int _get_node_record(PGconn *conn, char *cluster, char *sqlquery, t_node_info *node_info); PGconn * -_establish_db_connection(const char *conninfo, const bool exit_on_error, const bool log_notice) +_establish_db_connection(const char *conninfo, const bool exit_on_error, const bool log_notice, const bool verbose_only) { /* Make a connection to the database */ PGconn *conn = NULL; @@ -50,15 +50,23 @@ _establish_db_connection(const char *conninfo, const bool exit_on_error, const b /* Check to see that the backend connection was successfully made */ if ((PQstatus(conn) != CONNECTION_OK)) { - if (log_notice) + bool emit_log = true; + + if (verbose_only == true && verbose_logging == false) + emit_log = false; + + if (emit_log) { - log_notice(_("connection to database failed: %s\n"), - PQerrorMessage(conn)); - } - else - { - log_err(_("connection to database failed: %s\n"), - PQerrorMessage(conn)); + if (log_notice) + { + log_notice(_("connection to database failed: %s\n"), + PQerrorMessage(conn)); + } + else + { + log_err(_("connection to database failed: %s\n"), + PQerrorMessage(conn)); + } } if (exit_on_error) @@ -71,16 +79,35 @@ _establish_db_connection(const char *conninfo, const bool exit_on_error, const b return conn; } + +/* + * Establish a database connection, optionally exit on error + */ PGconn * establish_db_connection(const char *conninfo, const bool exit_on_error) { - return _establish_db_connection(conninfo, exit_on_error, false); + return _establish_db_connection(conninfo, exit_on_error, false, false); } +/* + * Attempt to establish a database connection, never exit on error, only + * output error messages if --verbose option used + */ PGconn * -test_db_connection(const char *conninfo, const bool exit_on_error) +establish_db_connection_quiet(const char *conninfo) { - return _establish_db_connection(conninfo, exit_on_error, true); + return _establish_db_connection(conninfo, false, false, true); +} + +/* + * Attempt to establish a database connection, never exit on error, + * output connection error messages as NOTICE (useful when connection + * failure is expected) + */ +PGconn * +test_db_connection(const char *conninfo) +{ + return _establish_db_connection(conninfo, false, true, false); } diff --git a/dbutils.h b/dbutils.h index d96667fa..18b97918 100644 --- a/dbutils.h +++ b/dbutils.h @@ -81,11 +81,12 @@ typedef struct s_replication_slot PGconn *_establish_db_connection(const char *conninfo, const bool exit_on_error, - const bool log_notice); + const bool log_notice, + const bool verbose_only); PGconn *establish_db_connection(const char *conninfo, const bool exit_on_error); -PGconn *test_db_connection(const char *conninfo, - const bool exit_on_error); +PGconn *establish_db_connection_quiet(const char *conninfo); +PGconn *test_db_connection(const char *conninfo); PGconn *establish_db_connection_by_params(const char *keywords[], const char *values[], const bool exit_on_error); diff --git a/log.h b/log.h index e717be98..58f7b459 100644 --- a/log.h +++ b/log.h @@ -130,5 +130,7 @@ __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); extern int log_type; extern int log_level; +extern int verbose_logging; +extern int terse_logging; -#endif +#endif /* _REPMGR_LOG_H_ */ diff --git a/repmgr.c b/repmgr.c index 4605ef39..3dfe4310 100644 --- a/repmgr.c +++ b/repmgr.c @@ -871,7 +871,7 @@ do_cluster_show(void) upstream_length, conninfo_length = 0; - /* We need to connect to check configuration */ + /* Connect to local database to obtain cluster connection data */ log_info(_("connecting to database\n")); conn = establish_db_connection(options.conninfo, true); @@ -946,7 +946,8 @@ do_cluster_show(void) for (i = 0; i < PQntuples(res); i++) { - conn = establish_db_connection(PQgetvalue(res, i, 0), false); + conn = establish_db_connection_quiet(PQgetvalue(res, i, 0)); + if (PQstatus(conn) != CONNECTION_OK) strcpy(node_role, " FAILED"); else if (strcmp(PQgetvalue(res, i, 1), "witness") == 0) @@ -3463,8 +3464,7 @@ do_standby_switchover(void) for(i = 0; i < options.reconnect_attempts; i++) { /* Check whether primary is available */ - - remote_conn = test_db_connection(remote_conninfo, false); /* don't fail on error */ + remote_conn = test_db_connection(remote_conninfo); if (PQstatus(remote_conn) == CONNECTION_OK) {