Skip to content

Instantly share code, notes, and snippets.

@alexs77
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexs77/f30b95093dbdcbad56f6 to your computer and use it in GitHub Desktop.
Save alexs77/f30b95093dbdcbad56f6 to your computer and use it in GitHub Desktop.
Invoking Shell script with Upstart "start on socket"
# /etc/xinetd.d/mysqlchk
# description: mysqlchk
service mysqlchk
{
flags = REUSE
socket_type = stream
port = 33066
wait = no
user = nobody
server = /opt/tools/mysqlchk.sh
log_on_failure += USERID
disable = no
# only_from = 0.0.0.0/0
# recommended to put the IPs that need
# to connect exclusively (security purposes)
per_source = UNLIMITED
# Recently added (May 20, 2010)
# Prevents the system from complaining
# about having too many connections open from
# the same IP. More info:
# http://www.linuxfocus.org/English/November2000/article175.shtml
}
start on socket PROTO=inet PORT=23066 ADDR=0.0.0.0
setuid nobody
exec /usr/bin/python3 /opt/tools/socket-handler.py /opt/tools/mysqlchk.sh
#!/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="3306"
MYSQL_USERNAME="cmon"
MYSQL_PASSWORD="…"
MYSQL_OPTS="-N -q -A"
FORCE_FAIL="/tmp/proxyoff"
MYSQL_BIN="$(which mysql)"
CHECK_QUERY="show global status where variable_name='wsrep_local_state'"
return_ok()
{
printf "HTTP/1.1 200 OK\r\n"
sleep 0.05
printf "Content-Type: text/plain\r\n"
#sleep 0.05
# printf "Content-Length: 43\r\n"
sleep 0.05
printf "\r\n"
sleep 0.05
printf "Galera Cluster Node running and synced.\r\n"
sleep 0.05
printf "\r\n"
exit 0
}
return_fail()
{
printf "HTTP/1.1 503 Service Unavailable\r\n"
sleep 0.05
printf "Content-Type: text/plain\r\n"
sleep 0.05
printf "Content-Length: 42\r\n"
sleep 0.05
printf "\r\n"
sleep 0.05
printf "Galera Cluster Node *down*.\r\n"
sleep 0.05
printf "$STDERR" | sed -e 's/\n$/\r\n/'
sleep 0.05
printf "\r\n"
sleep 0.05
exit 1
}
if [ -f "$FORCE_FAIL" ]; then
STDERR="$FORCE_FAIL found"
return_fail;
fi
STDOUT=$( { STDERR=$($MYSQL_BIN $MYSQL_OPTS --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "$CHECK_QUERY" 2>&1 1>&$out); } {out}>&1 )
if [ $? -ne 0 ]; then
return_fail;
fi
status=`echo "$STDOUT" | awk '{print $2;}'`
if [ $status -ne 4 ]; then
return_fail;
fi
return_ok;
# EOF
#!/usr/bin/python3
import os, socket, subprocess, sys
# Create the socket file descriptor from the the env var
sock_fd = socket.fromfd(int(os.environ["UPSTART_FDS"]),
socket.AF_INET, socket.SOCK_STREAM)
# Accept the connection, create a connection file descriptor
conn, addr = sock_fd.accept()
# Start the specified command and captue the output
# http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output
output = subprocess.Popen(sys.argv[1:], stdout=subprocess.PIPE).communicate()[0]
# Write
#conn.send(output.decode('UTF-8'))
conn.send(output)
# Finish
conn.close()
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment