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