#!/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()