Mysql主從檢測指令碼

來源:互聯網
上載者:User

標籤:檢測   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 = ‘[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

Mysql主從檢測指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.