Blame SOURCES/clustercheck.sh

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