mysql数据库主从运行。为了知道从机的同步情况,写了个脚本,放在crontab中,如果同步出错,则邮件报警。去年写的,放上来做个备忘。
#!/usr/local/bin/python # ------------------------------------------------------------------------------- # Filename: .py # Revision: 1.0 # Date: 2012-03-20 # Author: simonzhang # Web: www.simonzhang.net # Email: simon-zzm@163.com # ------------------------------------------------------------------------------- import os import pexpect import time import smtplib from email.mime.text import MIMEText #### base se mysql_bin = '/mysql5/bin/mysql' mysql_user = '' mysql_pass = '' mail_host = 'smtp.exmail.qq.com' mail_user = 'XXX@XXX.net' mail_pwd = 'XXXX' mail_to = "xxxxx@xxx.com" #### def mail_warn(error_ip): content = 'IP %s mysql slave is error!'%error_ip msg = MIMEText(content) msg['From'] = mail_user msg['Subject'] = 'mysql warnning %s'%error_ip msg['To'] = mail_to try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pwd) s.sendmail(mail_user,[mail_to],msg.as_string()) s.close() except Exception ,e: print e def main(): status = os.popen("%s -u%s -p%s -e 'show slave status\G'"% (mysql_bin,mysql_user,mysql_pass)).read() io_status = status[status.find('Slave_IO_Running: ')+18] sql_status = status[status.find('Slave_SQL_Running: ')+19] if (io_status == 'Y') or (sql_status == 'Y'): ip = os.popen("/sbin/ifconfig|grep 'inet addr'|awk '{print $2}'").read() get_local_ip = ip[ip.find(':')+1:ip.find('n')] mail_warn("%s"%get_local_ip) if __name__ == "__main__": main()
发表评论