码迷,mamicode.com
首页 > 数据库 > 详细

Mysql主从检测脚本

时间:2015-01-02 07:36:11      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:检测   python   

#!/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 = ‘yiqiang.wei@kisspython.com‘

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

Mysql主从检测脚本

标签:检测   python   

原文地址:http://mkernel.blog.51cto.com/8015041/1598347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!