Last active
September 9, 2019 10:57
-
-
Save nmrshll/4cf8d7dbcbb0a3cd8e24ca3021f2e18d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pgping() { | |
help_flags=(help -h --help) | |
if [[ ${help_flags[*]} =~ "$1" ]] || ([ -z "$1" ] || [ -z "$2" ]); then | |
echo "usage:" | |
echo | |
echo "pgping <server> <port> [<username>]" | |
return 0 | |
fi | |
PROTOCOL_VERSION="\x00\x03\x00\x00" | |
COMMAND="user" | |
NC_TIMEOUT=3 | |
POSTGRES_SERVER=$1 | |
POSTGRES_PORT=$2 | |
if [ ! -z "$3" ]; then | |
USERNAME=$3 | |
else | |
USERNAME="username" | |
fi | |
PACKET_SIZE="\x00\x00\x00\x$(printf '%02x' $(( | |
4 + | |
${#PROTOCOL_VERSION} / 4 + | |
${#COMMAND} + | |
1 + | |
${#USERNAME} + | |
2 | |
)))" | |
test "$( | |
echo -ne "${PACKET_SIZE}${PROTOCOL_VERSION}${COMMAND}\x00${USERNAME}\x00\x00" | | |
nc -w $NC_TIMEOUT $POSTGRES_SERVER $POSTGRES_PORT 2>/dev/null | head -c1 | |
)" == "R" | |
if [ $? -eq 0 ]; then | |
echo "health check passed" | |
return 0 | |
else | |
echo "health check failed" | |
return 1 | |
fi | |
} | |
pgping "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment