Created
April 2, 2014 01:04
-
-
Save Firehed/9926159 to your computer and use it in GitHub Desktop.
MySQL/Galera Health Check for haproxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
MYSQL_HOST="127.0.0.1" | |
MYSQL_PORT="53306" | |
MYSQL_USERNAME="mysqlchk" | |
MYSQL_PASSWORD="" | |
MYSQL_OPTS="-N -q -A" | |
TMP_FILE="/dev/shm/mysqlchk.$$.out" | |
ERR_FILE="/dev/shm/mysqlchk.$$.err" | |
FORCE_FAIL="/dev/shm/proxyoff" | |
MYSQL_BIN="/usr/bin/mysql" | |
CHECK_QUERY="show global status where variable_name='wsrep_local_state'" | |
preflight_check() | |
{ | |
for I in "$TMP_FILE" "$ERR_FILE"; do | |
if [ -f "$I" ]; then | |
if [ ! -w $I ]; then | |
printf "HTTP/1.1 503 Service Unavailable\r\n" | |
printf "Content-Type: Content-Type: text/plain\r\n" | |
printf "\r\n" | |
printf "Cannot write to $I\r\n" | |
printf "\r\n" | |
exit 1 | |
fi | |
fi | |
done | |
} | |
return_ok() | |
{ | |
printf "HTTP/1.1 200 OK\r\n" | |
printf "Content-Type: text/html\r\n" | |
printf "Content-Length: 43\r\n" | |
printf "\r\n" | |
printf "<html><body>MySQL is running.</body></html>\r\n" | |
printf "\r\n" | |
rm $ERR_FILE $TMP_FILE | |
exit 0 | |
} | |
return_fail() | |
{ | |
printf "HTTP/1.1 503 Service Unavailable\r\n" | |
printf "Content-Type: text/html\r\n" | |
printf "Content-Length: 42\r\n" | |
printf "\r\n" | |
printf "<html><body>MySQL is *down*.</body></html>\r\n" | |
sed -e 's/\n$/\r\n/' $ERR_FILE | |
printf "\r\n" | |
rm $ERR_FILE $TMP_FILE | |
exit 1 | |
} | |
preflight_check | |
if [ -f "$FORCE_FAIL" ]; then | |
echo "$FORCE_FAIL found" > $ERR_FILE | |
return_fail; | |
fi | |
$MYSQL_BIN $MYSQL_OPTS --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "$CHECK_QUERY" > $TMP_FILE 2> $ERR_FILE | |
if [ $? -ne 0 ]; then | |
return_fail; | |
fi | |
status=`cat $TMP_FILE | awk '{print $2;}'` | |
if [ $status -ne 4 ]; then | |
return_fail; | |
fi | |
return_ok; | |
[estern@sbd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment