forked from ashraf-s9s/mysqlchk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
galerachk.mysql
executable file
·96 lines (88 loc) · 2.93 KB
/
galerachk.mysql
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
#
# This script checks if a mysql server is healthy running on localhost. It will
# return:
# "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
# - OR -
# "HTTP/1.x 500 Internal Server Error\r" (else)
#
# The purpose of this script is make haproxy capable of monitoring mysql properly
#
SLAVE_LAG_LIMIT=5
MYSQL_HOST="localhost"
MYSQL_USERNAME='@MYSQL_USER@'
MYSQL_PASSWORD='@MYSQL_PASSWORD@'
MYSQL_BIN='@MYSQL_BIN@'
MYSQL_OPTS="-q -A --connect-timeout=10"
TMP_FILE="/tmp/mysqlchk.$$.out"
ERR_FILE="/tmp/mysqlchk.$$.err"
FORCE_FAIL="/dev/shm/proxyoff"
preflight_check()
{
for I in "$TMP_FILE" "$ERR_FILE"; do
if [ -f "$I" ]; then
if [ ! -w $I ]; then
echo -e "HTTP/1.1 503 Service Unavailable\r\n"
echo -e "Content-Type: Content-Type: text/plain\r\n"
echo -e "\r\n"
echo -e "Cannot write to $I\r\n"
echo -e "\r\n"
exit 1
fi
fi
done
}
return_ok()
{
echo -e "HTTP/1.1 200 OK\r\n"
echo -e "Content-Type: text/html\r\n"
echo -e "Content-Length: 43\r\n"
echo -e "\r\n"
#if [ $role == "master" ]; then
# echo -e "<html><body>MySQL master is running.</body></html>\r\n"
#elif [ $role == "slave" ]; then
# echo -e "<html><body>MySQL slave is running. (Slave lag: $SLAVE_LAG)</body></html>\r\n"
#else
echo -e "<html><body>MySQL is running.</body></html>\r\n"
#fi
echo -e "\r\n"
# rm $ERR_FILE $TMP_FILE
exit 0
}
return_fail()
{
echo -e "HTTP/1.1 503 Service Unavailable\r\n"
echo -e "Content-Type: text/html\r\n"
echo -e "Content-Length: 42\r\n"
echo -e "\r\n"
echo -e "<html><body>MySQL is *down*.</body></html>\r\n"
echo -e "\r\n"
exit 1
}
preflight_check
if [ -f "$FORCE_FAIL" ]; then
echo "$FORCE_FAIL found" > $ERR_FILE
return_fail
fi
CMDLINE="mysql $MYSQL_OPTS --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e"
# SLAVE_IO=$(${CMDLINE} 'SHOW SLAVE STATUS' --vertical 2>/dev/null | grep Slave_IO_Running | tail -1 | awk {'print $2'})
# SLAVE_SQL=$(${CMDLINE} 'SHOW SLAVE STATUS' --vertical 2>/dev/null | grep Slave_SQL_Running | head -1 | awk {'print $2'})
PRIMARY=$(mysql --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD --batch -e 'SHOW GLOBAL STATUS' | grep wsrep_cluster_status | cut -f2)
#if [[ "${SLAVE_IO}" == "Yes" ]] && [[ "${SLAVE_SQL}" == "Yes" ]]; then
# role='slave'
# SLAVE_LAG=$(${CMDLINE} 'SHOW SLAVE STATUS' --vertical 2>/dev/null | grep Seconds_Behind_Master | tail -1 | awk {'print $2'})
# if [[ $SLAVE_LAG = 0 ]]; then
# return_ok
# elif [ $SLAVE_LAG -lt $SLAVE_LAG_LIMIT ] ; then
# return_ok
# fi
#else
# role='master'
# READ_ONLY=$($CMDLINE 'SHOW GLOBAL VARIABLES LIKE "read_only"' --vertical 2>/dev/null | tail -1 | awk {'print $2'})
# [[ "${READ_ONLY}" == "OFF" ]] && return_ok
#fi
if [[ "${PRIMARY}" == "Primary" ]]; then
return_ok
else
return_fail
fi