Python实现mysql主从监控脚本
Python实现mysql主从监控脚本
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import MySQLdb
import time
import mail163
now=time.strftime("%Y/%m/%d-%H:%M:%S",time.localtime())
class Mydb(object):
def __init__ (self,host,user,port,passwd,sock):
self.host=host
self.user=user
self.port=port
self.passwd=passwd
self.sock=sock
try:
self.db=MySQLdb.connect(host=self.host,user=self.user,port=self.port,passwd=self.passwd,unix_socket=self.sock)
except:
print "cann't connect to mysql server %s %s "%(self.host,self.port)
else:
self.cursor=self.db.cursor()
def get_slave(self):
self.cursor.execute(' show slave status ;')
slave=self.cursor.fetchall()
return slave
def check_status (self):
slave=self.get_slave()
if (slave[0])[10]!='Yes' or (slave[0])[11]!='Yes':
return 'stop'
else:
return 'running'
def close(self):
self.cursor.close()
def main ():
db=Mydb('localhost','root',3306,'JD#mysql87##','/var/lib/mysql/mysql.sock')
status=db.check_status()
print status
if status=='stop':
mail163.mail('64.88 slave down at %s'%(now))
db.close()
if __name__=='__main__':
main()
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 悩姜!


