Last active
June 9, 2022 19:51
-
-
Save mjf/a842bbf13db389b2bce2f8f7beb2eed3 to your computer and use it in GitHub Desktop.
Generate HAProxy PostgreSQL TCP Check
This file contains hidden or 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
#! /bin/sh | |
# haproxy-pgsql-tcp-check - Generate HAProxy PostgreSQL TCP Check | |
# Copyright (C) 2021 Matous Jan Fialka, <https://mjf.cz/> | |
# Released under the terms of "The MIT License" | |
# | |
# See https://www.postgresql.org/docs/current/protocol-message-formats.html | |
if [ $# -lt 2 -o $# -gt 3 ]; then | |
printf -- 'Usage: %s USER DATABASE ["leader"]\n' "${0##*/}" >&2 | |
exit 1 | |
fi | |
cat <<- EOT | |
option tcp-check | |
tcp-check connect | |
EOT | |
printf -- 'tcp-check send-binary %08x00030000%s00%s00%s00%s0000\n' \ | |
$((0x19 + ${#1} + ${#2})) \ | |
$(tr -d '\n' <<< 'user' |xxd -ps) \ | |
$(tr -d '\n' <<< "$1" |xxd -ps) \ | |
$(tr -d '\n' <<< 'database' |xxd -ps) \ | |
$(tr -d '\n' <<< "$2" |xxd -ps) | |
if [ "$3" == 'leader' ]; then | |
r='66' | |
else | |
r='74' | |
fi | |
cat <<- EOT | |
tcp-check expect binary 520000000800000000 | |
tcp-check send-binary 510000002073656c6563742070675f69735f696e5f7265636f7665727928293b00 | |
tcp-check expect binary 540000002a000170675f69735f696e5f7265636f7665727900000000000000000000100001ffffffff0000440000000b000100000001$r | |
tcp-check send-binary 5800000004 | |
EOT | |
# vi:ft=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment