consul: add handling DB without replica to health.sh

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2023-11-23 13:07:56 +01:00
parent 1ff92d5fc5
commit 6b296be196
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
2 changed files with 11 additions and 13 deletions

View File

@ -15,7 +15,7 @@
tcp: 'localhost:{{ postgres_ha_cont_port }}'
- id: '{{ postgres_ha_service_name }}-mirror'
name: 'PostgreSQL Replica Healthcheck'
name: 'PostgreSQL {{ postgres_ha_replica_enabled | ternary("Replica", "Tables") }} Healthcheck'
disabled: '{{ not postgres_ha_replica_enabled }}'
type: 'script'
script: '{{ postgres_ha_service_path }}/health.sh'

View File

@ -2,29 +2,27 @@
# vim: set ft=sh:
set -Eeo pipefail
{% if postgres_ha_is_master %}
{% if not postgres_ha_replica_enabled %}
STATUS_QUERY='SELECT oid,datname,dattablespace FROM pg_catalog.pg_database WHERE datistemplate = false;'
HEALTH_REGEX='datname +| ({{ postgres_ha_databases | map(attribute="name") | join("|") }})'
{% elif postgres_ha_is_master %}
STATUS_QUERY='SELECT * FROM pg_stat_replication'
HEALTH_REGEX='status +| streaming'
{% else %}
STATUS_QUERY='SELECT * FROM pg_stat_wal_receiver'
HEALTH_REGEX='status +| streaming'
{% endif %}
REPLICA_STATUS=$(
"{{ postgres_ha_service_path }}/admin.sh" -x -c "${STATUS_QUERY}"
)
function fail() {
echo "ERROR: Replication broken!"
if [[ ! "${REPLICA_STATUS}" =~ ${HEALTH_REGEX} ]]; then
echo "ERROR: {{ postgres_ha_replica_enabled | ternary("Replication", "Tables") }} broken!"
echo
echo "${REPLICA_STATUS}";
exit 2;
}
fi
{% if postgres_ha_is_master %}
REGEX='state +| streaming'
{% else %}
REGEX='status +| streaming'
{% endif %}
[[ ! "${REPLICA_STATUS}" =~ ${REGEX} ]] && fail
echo "Replication healthy."
echo "{{ postgres_ha_replica_enabled | ternary("Replication", "Tables") }} healthy."
echo
echo "${REPLICA_STATUS}" | grep -v -e '_lsn' -e '_lag'