File: //usr/lib/python2.7/site-packages/lap/xtradb_cluster.py
import MySQLdb
import yaml
def __run__(params):
conn = None
# get the parameters to connect
try:
f = params.get("defaults-file", "/etc/check_mk/mysql.cfg")
conn = MySQLdb.connect(read_default_file=f)
cursor = conn.cursor()
#check the role of node
cursor.execute("select variable_value from information_schema.global_status where variable_name='wsrep_cluster_status'")
node_role = cursor.fetchone()[0]
if node_role != "Primary":
return[2,'CRITICAL: Node is not Primary! Check your cluster! ']
else:
node_role_status = 0
# check if wsrep (MySQL Write Set Replication), is connected
cursor.execute("select variable_value from information_schema.global_status where variable_name='wsrep_connected';")
wsrep_connected = cursor.fetchone()[0]
if wsrep_connected != "ON":
return[2,'CRITICAL: wsrep is not connected ']
else:
wsrep_connected_status = 0
#check the number of nodes
cursor.execute("select variable_value from information_schema.global_status where variable_name='wsrep_cluster_size'")
number_nodes = cursor.fetchone()[0]
cursor.execute("select variable_value from information_schema.global_variables where variable_name='wsrep_cluster_address';")
nodes_adresses = cursor.fetchall()
if number_nodes == "2":
return[2, 'CRITICAL: 1 node is down! Check your cluster! Hosts: %s ' % nodes_adresses ]
else:
number_nodes_status = 0
# check sync between nodes
cursor.execute("select variable_value from information_schema.global_status where variable_name='wsrep_local_state'")
wsrep_local_state = cursor.fetchone()[0]
cursor.execute("select variable_value from information_schema.global_status where variable_name='wsrep_local_state_comment'")
wsrep_local_state_comment = cursor.fetchone()[0]
if wsrep_local_state != "4" or wsrep_local_state_comment != "Synced":
return[2,'CRITICAL: The node are not synced']
else:
wsrep_local_state_status = 0
wsrep_local_state_comment_status =0
# validating checks
if node_role_status == 0 and wsrep_connected_status == 0 and number_nodes_status == 0 and wsrep_local_state_status == 0 and wsrep_local_state_comment_status == 0:
return[0,'OK: Cluster is working fine!' ]
except Exception, e:
return [2, 'CRITICAL: {0}'.format(str(e))]
finally:
try:
if conn:
conn.close()
except:
# finally clause, don't bother about errors here
pass