Blame SOURCES/clustercheck.sh

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