Use this script to monitor an RDS instance endpoint, RDS proxy endpoint, Route 53 endpoint during any testing or failover operations.
$ watch --differences -n 1 -t "./monitor-mysql-connection.sh"
#!/usr/bin/env bash | |
[ -z "${DBA_USER}" ] && echo "ERROR: DBA_USER is not defined" && exit 1 | |
[ -z "${DBA_PASSWD}" ] && echo "ERROR: DBA_PASSWD is not defined" && exit 1 | |
[ -z "${PORT}" ] && echo "ERROR: PORT is not defined" && exit 1 | |
[ -z "${INSTANCE_ENDPOINT}" ] && echo "ERROR: INSTANCE_ENDPOINT is not defined" && exit 1 | |
DOCKERIZE="" | |
if [[ -n "${USE_DOCKER}" ]]; then | |
[[ ! $(type -p docker) ]] && echo "ERROR: 'docker' client not installed or in PATH" && exit 1 | |
# This takes took look to lookup each time | |
# OUTPUT=$(docker pull mysql) | |
DOCKERIZE="docker run -it --rm mysql" | |
else | |
[[ ! $(type -p mysql) ]] && echo "ERROR: 'mysql' client not installed or in PATH" && exit 1 | |
fi | |
INSTANCE_ID=$(cut -d. -f1 <<< "${INSTANCE_ENDPOINT}") | |
OUTPUT=$(${DOCKERIZE} mysql -h"${INSTANCE_ENDPOINT}" -u"${DBA_USER}" -p"${DBA_PASSWD}" -P"${PORT}" -AUNs --show-warnings --connect-timeout=1 -e 'SELECT NOW(),VERSION(),@@read_only' 2>/dev/null) | |
RC="$?" | |
[[ ${RC} -ne 0 ]] && OUTPUT="ERROR [${RC}] $(date)" | |
echo "${INSTANCE_ID}"$'\t'"${OUTPUT}" | |
exit "${RC}" |