#!/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
原文地址:http://mkernel.blog.51cto.com/8015041/1598347