超简易的查看mysql主从延时的脚本
1 代码逻辑
很简单,就是python连mysql,然后show slave status,然后取2个参数,后面加了一个log模块,然后做了一个循环,5s一次。
2. 代码本体
隐掉了一些敏感信息:
import MySQLdb
import logging
import time
FORMAT = ‘%(asctime)-15s %(message)s‘
logging.basicConfig(filename=‘/tmp/slave.log‘,level=logging.DEBUG,format=FORMAT)
#logging.basicConfig(format=FORMAT)
dbuser = "xx"
dbpass = "xx"
dbname = "xx"
d=[]
d.append(‘xx.mysql.rds.aliyuncs.com‘)
d.append(‘xx.mysql.rds.aliyuncs.com‘)
d.append(‘xx.mysql.rds.aliyuncs.com‘)
d.append(‘xx.mysql.rds.aliyuncs.com‘)
def get_vars(thedb ):
db1 = MySQLdb.connect(thedb,dbuser,dbpass,dbname)
#db1 = MySQLdb.connect(‘rdso8nxo733x5hq8q2a1.mysql.rds.aliyuncs.com‘,dbuser,dbpass,dbname)
c = db1.cursor()
c.execute("show slave status")
ret = c.fetchone()
c.close()
db1.close()
return (ret[32],ret[42])
while True:
for i in d:
print i
(v1,v2) = get_vars(i)
#print "Seconds_Behind_Master:%s \nSQL_Delay:%s\n" %(ret[32],ret[42])
logging.debug(‘Seconds_Behind_Master:%s|SQL_Delay:%s‘,v1,v2)
print "Seconds_Behind_Master:%s \nSQL_Delay:%s\n" %(v1,v2)
logging.debug(‘end‘)
time.sleep(5)
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-26 12:24:49