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