#!/usr/bin/env python
#! _*_ coding:utf-8 _*_
import os,re,time
#注 请把故障库启动后再执行此脚本
#1.获取mha日志中change语句
stmt = os.popen("ssh exsddb3 'cat /var/log/masterha/app1/app1.log'|grep 'All other slaves'|grep "`date +%d|cut -d'0' -f2` "|cut -d':' -f4|awk '{sub(/^[ ]+/, "")};1'").read().strip('
')
ddd = 'CHANGE MASTER TO MASTER_HOST='
ip = re.findall(r'd+.d+.d+.d+',stmt)[0]
userf = 'replication'
passwf = 'replication'
logfile = re.findall(r"MASTER_LOG_FILE='(.*)', MASTER_LOG",stmt)[0]
logpos = re.findall(r"MASTER_LOG_POS=(.*), MASTER_USER=",stmt)[0]
user = 'root'
passw = 'abc123'
mysqlbase = '/usr/local/mysql/bin/'
#2.转换成标准change语句
change = "%s'%s',MASTER_PASSWORD='%s',MASTER_PORT=%s,MASTER_LOG_FILE='%s',MASTER_LOG_POS=%s,MASTER_USER='%s'" %(ddd,ip,passwf,'3306',logfile,logpos,userf)
status = os.popen("%smysql -u'%s' -p'%s' -e 'show slave statusG'" %(mysqlbase,user,passw)).read()
print(' ')
#3.在故障库中change到新主库,并启动slave,查看salve状态
os.system("%smysql -u'%s' -p'%s' -e '%s'" %(mysqlbase,user,passw,change))
os.system("%smysql -u'%s' -p'%s' -e 'start slave'" %(mysqlbase,user,passw))
os.system("%smysql -u'%s' -p'%s' -e 'show slave statusG'" %(mysqlbase,user,passw))
#4.在manager节点上检查MHA复制状态
os.system("ssh exsddb3 '/usr/bin/masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf' >/tmp/check_repl")
print(' ')
print(' ')
ifok = os.popen("cat /tmp/check_repl |grep 'MySQL Replication Health is OK.'").read().strip('
')
#5.判断MHA集群是否OK,若OK就启动manager进程对MHA进行监控,然后查看MHA集群状态
if ifok == 'MySQL Replication Health is OK.':
print(' ')
os.system("ssh exsddb3 'sh /sh/mha_start.sh'")
time.sleep(2)
os.system("ssh exsddb3 '/usr/bin/masterha_check_status --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf'")
else:
print(' ')
print('MySQL Replication Health is not OK!')
#@_@ 欢迎交流!联系人 广西宾阳 阿桂 qq149951292