Mysql主从检测脚本

#!/usr/bin/env python 


#Kiss Python 


#*/10 * * * * /usr/bin/python /root/checkslave.py


import MySQLdb

import time

import paramiko


ips = [‘127.0.0.1‘]

mailcontact = ‘[email protected]

mobilecontact = ‘18701669895‘


checklog=open(‘slave.log‘,‘a‘)

checklog.write(str(time.strftime(‘%Y-%m-%d %H:%M‘,time.localtime(time.time())))+"  ")

def checksql(host):

    conn=MySQLdb.connect(host=host,user=‘root‘,passwd=‘zradbslave‘)

    cur=conn.cursor(MySQLdb.cursors.DictCursor)

    cur.execute("show slave status;")

    sql=cur.fetchall()

    cur.close()


    if sql[0][‘Slave_IO_Running‘] == ‘Yes‘ and sql[0][‘Slave_SQL_Running‘] == ‘Yes‘:

        checklog.write(str(host) + " Check DBSlave OK !" + "\n")

    else:

        mailconnect = paramiko.SSHClient()

        mailconnect.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        mailconnect.connect(‘172.16.5.114‘, 22, username=‘root‘, password=‘‘, timeout=6)

        stdin, stdout, stderr = mailconnect.exec_command(‘printf "%s ZraDBSlave Error !\n\n \

                Master_Host: %s\nSlave_IO_Running: %s\n Slave_SQL_Running: %s"| mail -s \

                 "ZraDBSlave Error" %s‘ % (host,sql[0][‘Master_Host‘],sql[0][‘Slave_IO_Running‘], \

                sql[0][‘Slave_SQL_Running‘],mailcontact))

        stdin, stdout, stderr = mailconnect.exec_command(‘cd /home/weiyiqiang/fetion; \

                 LD_LIBRARY_PATH =. ./fetion  --mobile=18701669895 --pwd=pass --to=%s \

                --msg-utf8="ZraDBSlave Error!\n\nMaster_Host: %s\nSlave_IO_Running: \                                  %s\nSlave_SQL_Running: %s"‘ % (mobilecontact,sql[0][‘Master_Host‘],sql[0] \            

                [‘Slave_IO_Running‘],sql[0][‘Slave_SQL_Running‘]))

        mailconnect.close()

        checklog.write(str(host) + " Check DBSlave Error Send Message to  %s !" % mobilecontact + "\n")




for ip in ips:

        checksql(ip)






checklog.close()


本文出自 “Crazy_Linux” 博客,请务必保留此出处http://mkernel.blog.51cto.com/8015041/1598347

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。