From 4c7c681a14053532e5d643ce13de478024450b6c Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Thu, 5 Jul 2018 10:47:31 +0900 Subject: [PATCH] repmgr: have "cluster show" exit with a non-zero value if issues detected If any issues are detected (e.g. node not reachable, unexpected node status etc.), "repmgr cluster show" returns exit code 25 ("ERR_NODE_STATUS"). Note that exit code 25 was introduced recently as "ERR_CLUSTER_CHECK", however it makes sense to use this to indicate issues detected by any command which can detect node issues. Addresses GitHub #456. --- HISTORY | 2 ++ doc/appendix-release-notes.sgml | 8 +++++++- doc/repmgr-cluster-crosscheck.sgml | 2 +- doc/repmgr-cluster-matrix.sgml | 2 +- doc/repmgr-cluster-show.sgml | 29 ++++++++++++++++++++++++++ errcode.h | 2 +- repmgr-action-cluster.c | 33 +++++++++++++++++++++++++++--- 7 files changed, 71 insertions(+), 7 deletions(-) diff --git a/HISTORY b/HISTORY index fd9e4aea..26e063b5 100644 --- a/HISTORY +++ b/HISTORY @@ -2,6 +2,8 @@ repmgr: add "--missing-slots" check to "repmgr node check" (Ian) repmgr: improve command line error handling; GitHub #464 (Ian) repmgr: fix "standby register --wait-sync" when no timeout provided (Ian) + repmgr: "cluster show" returns non-zero value if an issue encountered; + GitHub #456 (Ian) repmgrd: create a PID file by default; GitHub #457 (Ian) repmgrd: daemonize process by default; GitHub #458 (Ian) diff --git a/doc/appendix-release-notes.sgml b/doc/appendix-release-notes.sgml index fc2e3135..f8049e92 100644 --- a/doc/appendix-release-notes.sgml +++ b/doc/appendix-release-notes.sgml @@ -46,6 +46,12 @@ + + + repmgr cluster-show + returns non-zero exit code if node status issues detected (GitHub #456). + + @@ -76,7 +82,7 @@ - repmgr: fix repmgr standby register--wait-sync + repmgr: fix repmgr standby register --wait-sync when no timeout provided. diff --git a/doc/repmgr-cluster-crosscheck.sgml b/doc/repmgr-cluster-crosscheck.sgml index c3c0f2f2..c3aaeb75 100644 --- a/doc/repmgr-cluster-crosscheck.sgml +++ b/doc/repmgr-cluster-crosscheck.sgml @@ -56,7 +56,7 @@ - + One or more nodes could not be reached. diff --git a/doc/repmgr-cluster-matrix.sgml b/doc/repmgr-cluster-matrix.sgml index fbc8aff2..90992199 100644 --- a/doc/repmgr-cluster-matrix.sgml +++ b/doc/repmgr-cluster-matrix.sgml @@ -116,7 +116,7 @@ - + One or more nodes could not be reached. diff --git a/doc/repmgr-cluster-show.sgml b/doc/repmgr-cluster-show.sgml index 6616bbc5..227108e3 100644 --- a/doc/repmgr-cluster-show.sgml +++ b/doc/repmgr-cluster-show.sgml @@ -113,4 +113,33 @@ + + + Exit codes + + Following exit codes can be emitted by repmgr cluster show: + + + + + + + + No issues were detected. + + + + + + + + + One or more issues were detected. + + + + + + + diff --git a/errcode.h b/errcode.h index 481c0fe5..b7d4c688 100644 --- a/errcode.h +++ b/errcode.h @@ -46,6 +46,6 @@ #define ERR_SWITCHOVER_INCOMPLETE 22 #define ERR_FOLLOW_FAIL 23 #define ERR_REJOIN_FAIL 24 -#define ERR_CLUSTER_CHECK 25 +#define ERR_NODE_STATUS 25 #endif /* _ERRCODE_H_ */ diff --git a/repmgr-action-cluster.c b/repmgr-action-cluster.c index 76cd9ca2..b6bc06a5 100644 --- a/repmgr-action-cluster.c +++ b/repmgr-action-cluster.c @@ -83,6 +83,7 @@ do_cluster_show(void) int i = 0; ItemList warnings = {NULL, NULL}; bool success = false; + bool error_found = false; /* Connect to local database to obtain cluster connection data */ log_verbose(LOG_INFO, _("connecting to database")); @@ -218,6 +219,7 @@ do_cluster_show(void) else { appendPQExpBuffer(&details, "- failed"); + error_found = true; } } } @@ -281,6 +283,7 @@ do_cluster_show(void) else { appendPQExpBuffer(&details, "- failed"); + error_found = true; } } } @@ -292,17 +295,27 @@ do_cluster_show(void) if (cell->node_info->node_status == NODE_STATUS_UP) { if (cell->node_info->active == true) + { appendPQExpBuffer(&details, "* running"); + } else + { appendPQExpBuffer(&details, "! running"); + error_found = true; + } } /* node is unreachable */ else { if (cell->node_info->active == true) + { appendPQExpBuffer(&details, "? unreachable"); + } else + { appendPQExpBuffer(&details, "- failed"); + error_found = true; + } } } break; @@ -310,6 +323,7 @@ do_cluster_show(void) { /* this should never happen */ appendPQExpBuffer(&details, "? unknown node type"); + error_found = true; } break; } @@ -414,7 +428,6 @@ do_cluster_show(void) PQfinish(conn); /* emit any warnings */ - if (warnings.head != NULL && runtime_options.terse == false && runtime_options.output_mode != OM_CSV) { ItemListCell *cell = NULL; @@ -425,6 +438,20 @@ do_cluster_show(void) printf(_(" - %s\n"), cell->string); } } + + /* + * If warnings were noted, even if they're not displayed (e.g. in --csv node), + * that means something's not right so we need to emit a non-zero exit code. + */ + if (warnings.head != NULL) + { + error_found = true; + } + + if (error_found == true) + { + exit(ERR_NODE_STATUS); + } } @@ -696,7 +723,7 @@ do_cluster_crosscheck(void) if (error_found == true) { - exit(ERR_CLUSTER_CHECK); + exit(ERR_NODE_STATUS); } } @@ -786,7 +813,7 @@ do_cluster_matrix() if (error_found == true) { - exit(ERR_CLUSTER_CHECK); + exit(ERR_NODE_STATUS); } }