60bf3d
#!/bin/bash
60bf3d
#
60bf3d
# Script to make a proxy (ie HAProxy) capable of monitoring Galera cluster nodes properly
60bf3d
#
60bf3d
# Author: Olaf van Zandwijk <olaf.vanzandwijk@nedap.com>
60bf3d
# Author: Raghavendra Prabhu <raghavendra.prabhu@percona.com>
60bf3d
# Author: Ryan O'Hara <rohara@redhat.com>
60bf3d
#
60bf3d
# Documentation and download: https://github.com/olafz/percona-clustercheck
60bf3d
#
60bf3d
# Based on the original script from Unai Rodriguez
60bf3d
#
60bf3d
60bf3d
if [ -f @INSTALL_SYSCONFDIR@/sysconfig/clustercheck ]; then
60bf3d
    . @INSTALL_SYSCONFDIR@/sysconfig/clustercheck
60bf3d
fi
60bf3d
60bf3d
MYSQL_USERNAME="${MYSQL_USERNAME-clustercheckuser}"
60bf3d
MYSQL_PASSWORD="${MYSQL_PASSWORD-clustercheckpassword!}"
60bf3d
MYSQL_HOST="${MYSQL_HOST:-127.0.0.1}"
60bf3d
MYSQL_PORT="${MYSQL_PORT:-3306}"
60bf3d
ERR_FILE="${ERR_FILE:-/dev/null}"
60bf3d
AVAILABLE_WHEN_DONOR=${AVAILABLE_WHEN_DONOR:-0}
60bf3d
AVAILABLE_WHEN_READONLY=${AVAILABLE_WHEN_READONLY:-1}
60bf3d
DEFAULTS_EXTRA_FILE=${DEFAULTS_EXTRA_FILE:-@INSTALL_SYSCONFDIR@/my.cnf}
60bf3d
60bf3d
#Timeout exists for instances where mysqld may be hung
60bf3d
TIMEOUT=10
60bf3d
60bf3d
if [[ -r $DEFAULTS_EXTRA_FILE ]];then
60bf3d
    MYSQL_CMDLINE="mysql --defaults-extra-file=$DEFAULTS_EXTRA_FILE -nNE \
60bf3d
                    --connect-timeout=$TIMEOUT \
60bf3d
                    --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} \
60bf3d
                    --host=${MYSQL_HOST} --port=${MYSQL_PORT}"
60bf3d
else
60bf3d
    MYSQL_CMDLINE="mysql -nNE --connect-timeout=$TIMEOUT \
60bf3d
                    --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} \
60bf3d
                    --host=${MYSQL_HOST} --port=${MYSQL_PORT}"
60bf3d
fi
60bf3d
#
60bf3d
# Perform the query to check the wsrep_local_state
60bf3d
#
60bf3d
WSREP_STATUS=$($MYSQL_CMDLINE -e "SHOW STATUS LIKE 'wsrep_local_state';" \
60bf3d
    2>${ERR_FILE} | tail -1 2>>${ERR_FILE})
60bf3d
60bf3d
if [[ "${WSREP_STATUS}" == "4" ]] || [[ "${WSREP_STATUS}" == "2" && ${AVAILABLE_WHEN_DONOR} == 1 ]]
60bf3d
then
60bf3d
    # Check only when set to 0 to avoid latency in response.
60bf3d
    if [[ $AVAILABLE_WHEN_READONLY -eq 0 ]];then
60bf3d
        READ_ONLY=$($MYSQL_CMDLINE -e "SHOW GLOBAL VARIABLES LIKE 'read_only';" \
60bf3d
                    2>${ERR_FILE} | tail -1 2>>${ERR_FILE})
60bf3d
60bf3d
        if [[ "${READ_ONLY}" == "ON" ]];then
60bf3d
            # Galera cluster node local state is 'Synced', but it is in
60bf3d
            # read-only mode. The variable AVAILABLE_WHEN_READONLY is set to 0.
60bf3d
            # => return HTTP 503
60bf3d
            # Shell return-code is 1
60bf3d
            echo -en "HTTP/1.1 503 Service Unavailable\r\n"
60bf3d
            echo -en "Content-Type: text/plain\r\n"
60bf3d
            echo -en "Connection: close\r\n"
60bf3d
            echo -en "Content-Length: 35\r\n"
60bf3d
            echo -en "\r\n"
60bf3d
            echo -en "Galera cluster node is read-only.\r\n"
60bf3d
            sleep 0.1
60bf3d
            exit 1
60bf3d
        fi
60bf3d
    fi
60bf3d
    # Galera cluster node local state is 'Synced' => return HTTP 200
60bf3d
    # Shell return-code is 0
60bf3d
    echo -en "HTTP/1.1 200 OK\r\n"
60bf3d
    echo -en "Content-Type: text/plain\r\n"
60bf3d
    echo -en "Connection: close\r\n"
60bf3d
    echo -en "Content-Length: 32\r\n"
60bf3d
    echo -en "\r\n"
60bf3d
    echo -en "Galera cluster node is synced.\r\n"
60bf3d
    sleep 0.1
60bf3d
    exit 0
60bf3d
else
60bf3d
    # Galera cluster node local state is not 'Synced' => return HTTP 503
60bf3d
    # Shell return-code is 1
60bf3d
    echo -en "HTTP/1.1 503 Service Unavailable\r\n"
60bf3d
    echo -en "Content-Type: text/plain\r\n"
60bf3d
    echo -en "Connection: close\r\n"
60bf3d
    echo -en "Content-Length: 36\r\n"
60bf3d
    echo -en "\r\n"
60bf3d
    echo -en "Galera cluster node is not synced.\r\n"
60bf3d
    sleep 0.1
60bf3d
    exit 1
60bf3d
fi