/usr/bin/pyclustercheck is in percona-xtradb-cluster-server-5.6 5.6.21-25.8-0ubuntu3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #! /usr/bin/python
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
import MySQLdb
import MySQLdb.cursors
import optparse
import time
'''
__author__="See AUTHORS.txt at https://github.com/Oneiroi/clustercheck"
__copyright__="David Busby, Percona Ireland Ltd"
__license__="GNU v3 + section 7: Redistribution/Reuse of this code is permitted under the GNU v3 license, as an additional term ALL code must carry the original Author(s) credit in comment form."
__dependencies__="MySQLdb (python26-mysqldb (el5) / MySQL-python (el6) / python-mysqldb (ubuntu))"
__description__="Provides a stand alone http service, evaluating wsrep_local_state intended for use with HAProxy. Listens on 8000"
'''
class opts:
available_when_donor = 0
cache_time = 1
last_query_time = 0
last_query_result = 0
cnf_file = '~/.my.cnf'
being_updated = False
# Overriding the connect timeout so that status check doesn't hang
c_timeout = 10
class clustercheck(BaseHTTPServer.BaseHTTPRequestHandler):
def do_OPTIONS(self):
self.do_GET()
def do_GET(self):
ctime = time.time()
if ((ctime - opts.last_query_time) > opts.cache_time) and opts.being_updated == False:
#cache expired
opts.being_updated = True
opts.last_query_time = ctime
conn = None
try:
conn = MySQLdb.connect(
read_default_file = opts.cnf_file,
connect_timeout = opts.c_timeout,
cursorclass = MySQLdb.cursors.DictCursor)
except MySQLdb.OperationalError:
opts.being_updated = False #corrects a bug where the flag is never reset on communication failiure
if conn:
curs = conn.cursor()
curs.execute("SHOW STATUS LIKE 'wsrep_local_state'")
res = curs.fetchall()
else:
res = ''
if len(res) == 0:
self.send_response(503)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("Percona XtraDB Cluster Node state could not be retrieved.")
elif res[0]['Value'] == '4' or (int(opts.available_when_donor) == 1 and res[0]['Value'] == '2'):
opts.last_query_result = res[0]['Value']
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("Percona XtraDB Cluster Node is synced.")
else:
opts.last_query_result = res[0]['Value']
self.send_response(503)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("Percona XtraDB Cluster Node is not synced.")
if conn:
conn.close()
opts.being_updated = False
else:
#use cache result
if opts.last_query_result == '4' or (opts.available_when_donor == 1 and opts.last_query_result == '2'):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("CACHED: Percona XtraDB Cluster Node is synced.")
else:
self.send_response(503)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("CACHED: Percona XtraDB Cluster Node is not synced.")
"""
Usage:
pyclustercheck &>$LOGFILE &
To test:
curl http://127.0.0.1:8000
"""
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-a','--available-when-donor', dest='awd', default=0, help="Available when donor [default: %default]")
parser.add_option('-c','--cache-time', dest='cache', default=1, help="Cache the last response for N seconds [default: %default]")
parser.add_option('-f','--conf', dest='cnf', default='~/.my.cnf', help="MySQL Config file to use [default: %default]")
parser.add_option('-p','--port', dest='port', default=8000, help="Port to listen on [default: %default]")
options, args = parser.parse_args()
opts.available_when_donor = options.awd
opts.cnf_file = options.cnf
opts.cache_time = options.cache
server_class = BaseHTTPServer.HTTPServer
httpd = server_class(('',int(options.port)),clustercheck)
httpd.serve_forever()
|