Skip to content

Instantly share code, notes, and snippets.

@ronaldbradford
Last active January 18, 2024 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronaldbradford/6577261de8b97d7b48194fadbd51cd13 to your computer and use it in GitHub Desktop.
Save ronaldbradford/6577261de8b97d7b48194fadbd51cd13 to your computer and use it in GitHub Desktop.
Monitor a connection to a MySQL Endpoint

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}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment