设为首页 加入收藏

TOP

用Python对MySQL同步状态进行监控(二)
2015-11-21 01:38:00 来源: 作者: 【 】 浏览:1
Tags:Python MySQL 同步 状态 进行 监控
): """ 数据库同步是否正常 :return: None同步未开启,False同步中断,True同步正常 """ cur = self.__instance__.cursor(dictionary=True) cur.execute("SHOW SLAVE STATUS") result = cur.fetchone() cur.close() if result: if result["Slave_SQL_Running"] == "Yes" and result["Slave_IO_Running"] == "Yes": return True else: if result["Slave_SQL_Running"] == "No": self.__error__.append( result["Last_SQL_Error"] ) else: self.__error__.append( result["Last_IO_Error"] ) return False def get_last_error(self): """ 获取第一个错误信息 :return: String """ if self.__error__: return self.__error__.pop(0) def notify(self,title,message): """ 发送消息提醒 :param title: 消息的标题 :param message: 消息的内容 :return: """ msg = [title,message] pool = [] notify = notify_email( self.__configParseSMTP__() ) pool.append( notify ) for item in pool: item.ring( msg ) def close(self): """ 关闭数据库链接 """ if self.__instance__: self.__instance__.close() class notify_email(object): def __init__(self,config): self.config = config def ring(self, message=[]): subject = message.pop(0) messageBody = "".join( message ) mailList = self.config["to"].split(";") datetime = time.strftime("%Y-%m-%d %H:%M:%S") for to in mailList: body = """

管理员{admin},你好:

收到这封邮件说明你的数据库同步出现异常,请您及时进行处理。

异常信息:
{body}

{date}

""".format( admin=to, body=messageBody, date=datetime ) msg = MIMEText( body, "html", "utf-8" ) msg["From"] = self.config["from"] msg["To"] = to msg["Subject"] = subject smtp = smtplib.SMTP() smtp.connect( self.config["smtp_host"] ) if self.config.has_key("smtp_user"): smtp.login( self.config["smtp_user"], self.config["smtp_password"] ) smtp.sendmail( self.config["from"], to, msg.as_string() ) smtp.quit() if __name__ == "__main__": #命令行参数列表 usage = """usage: MySQLStat [options]""" opt = optparse.OptionParser(usage=usage) opt.add_option("-H","--host",dest="host",help="MySQL host (default: localhost)") opt.add_option("-u","--user",dest="user",help="MySQL user") opt.add_option("-p","--password",dest="password",help="MySQL password") opt.add_option("-P","--port",dest="port",help="MySQL port (default: 3306)") opt.add_option("","--smtp_host",dest="smtp_host",help="SMTP host (default: localhost)") opt.add_option("","--smtp_user",dest="smtp_user",help="SMTP user") opt.add_option("","--smtp_password",dest="smtp_password",help="SMTP password") opt.add_option("","--from",dest="from",help="Email from") opt.add_option("","--to",dest="to",help="Email to") opt.add_option("","--defaults-file",dest="defaults_file",help="config file path") (options,args) = opt.parse_args() options = options.__dict__ Statu = SlaveStatu( **options ) subject = "服务中心异常信息提醒" if Statu.connect() is False or Statu.isSlave() is False: Statu.notify( subject, Statu.get_last_error() ) Statu.close()

server1.cnf 设置文件内容

[config]
smtp_host=smtp.aliyun.com
smtp_user=xxxx@aliyun.com
smtp_password=xxxxxx
from=管理中心
host=xxx.xxx.xxx.xxx
user=root
password=123456

完成了以上的配置之后,我们在定时任务里添加一条任务,就可以让程序为我们监控MySQL的服务器状态了。
crontab设置

*/2 * * * * python slavecheckpoint.py --defaults-file=server1.cnf --to=dba@abc.com

github项目地址: https://github.com/yagas/checkpoint.git

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysql创建、删除用户与授权(linux.. 下一篇MySQL查看运行时间

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: