Detects MySQL master-slave status and, if never synchronized, sends an e-mail to the OPS person. Use timed tasks for second-level monitoring!
# -*- coding: utf-8 -*#! /usr/bin/env pythonfrom fabric.api import *from fabric.colors import *from fabric.context_managers import *from fabric.contrib.console import confirmimport stringimport smtplib#client ipenv.port= ' 9527 ' env.user= ' dyt ' env.hosts=[' 192.168.129.138 ', ' 192.168.129.139 ']env.password= ' dyt2015 ' #env. mysql _port = ' 3306 ' @taskdef check (): slave_ip = run ("ip Add|grep global ") for ip in env.hosts: if ip in slave_ip: slave_ip = ip slave_io = run ("MySQL -uroot -S /tmp/mysql_3306.sock -e ' Show slave status\g ' |grep slave_io _running:|awk ' {print $2} ') slave_sql = run ("Mysql -uroot -s /tmp/mysql_3306.sock -e ' show slave status\g ' |grep slave_sql_running:|awk ' {print $2} ') if slave_io == ' yes ' and slave_sql == ' yes ': pass else: HOST = "smtp.qq.com" subject = "MYSQL MASTER-SLAVE WARNING  "to = " [email protected] "from = " [email protected] "text = "%-20s mysql master-slave status : down " % slave_ip BODY = String.Join ( "from: %s" % FROM, "to: %s" % TO, "subject: %s" % subject , "", text ), "\ r \ n") server = smtplib. SMTP () Server.connect (HOST, "Server.starttls") Server.login ("[email protected]", "password") Server.sendmail (From, [to], body) server.quit ()
This article is from the "ˉt, Stand Wentao ┃﹎" blog, please be sure to keep this source http://hypocritical.blog.51cto.com/3388028/1680778
Monitor MySQL master-slave status with Python