aboutsummaryrefslogtreecommitdiffstats
path: root/templates/database/clustercheck.erb
blob: cc3231b3b3fbe3720d8c8c99e13e0e2cada01500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Managed by puppet
#
# Script to make a proxy (ie HAProxy) capable of monitoring Galera cluster
#
# Author: Olaf van Zandwijk <olaf.vanzandwijk@nedap.com>
#         Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
# 
# Documentation and download: https://github.com/olafz/percona-clustercheck
#
# Based on the original script from Unai Rodriguez
#
MYSQL_USERNAME='<%= @galera_clustercheck_dbuser %>'
MYSQL_PASSWORD='<%= @galera_clustercheck_dbpassword %>'

TIMEOUT=10
ERR_FILE="/dev/null"
AVAILABLE_WHEN_DONOR=0

MYSQL_CMDLINE="mysql -nNE --connect-timeout=$TIMEOUT --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} "

mysql_get_status(){
    ( $MYSQL_CMDLINE -e "SHOW STATUS LIKE '$1';" | tail -1 ) 2>>${ERR_FILE}
}
mysql_get_var(){
    ( $MYSQL_CMDLINE -e "SHOW GLOBAL VARIABLES LIKE '$1';" | tail -1 ) 2>>${ERR_FILE}
}

http_response(){
    status=$1
    shift
    msg="$@"
    if [ "$status" == 200 ]; then
        /bin/echo -en "HTTP/1.1 200 OK\r\n"
    else
        /bin/echo -en "HTTP/1.1 503 Service Unavailable\r\n"
    fi
    /bin/echo -en "Content-Type: text/plain\r\n"
    /bin/echo -en "\r\n"
    /bin/echo -en "$msg\r\n"
    /bin/echo -en "\r\n"
}


WSREP_LOCAL_STATE=$(mysql_get_status wsrep_local_state)
WSREP_READY=$(mysql_get_status wsrep_ready)
WSREP_CONNECTED=$(mysql_get_status wsrep_connected)
READY_ONLY=$(mysql_get_var read_only)

case ${AVAILABLE_WHEN_DONOR}-${WSREP_LOCAL_STATE}-${WSREP_READY}-${WSREP_CONNECTED}-${READY_ONLY} in 
    1-2-ON-ON-OFF|0-4-ON-ON-OFF) http_response 200 "Mariadb Cluster Node is synced, ready and connected." ;;
    *-*-OFF-*-*) http_response 503 "Mariadb Cluster Node is not ready." ;;
    *-*-*-OFF-*) http_response 503 "Mariadb Cluster Node is not connected" ;;
    *-*-*-*-ON) http_response 503 "Mariadb Cluster Node is readonly" ;;
    *) http_response 503 "Mariadb Cluster Node is not synced" ;;
esac