#!/usr/bin/env python
import time,MySQLdb
from subprocess import Popen
from subprocess import PIPE
import dns.query
import dns.message
import dns.tsig
import dns.tsigkeyring
import dns.update
def set_dns(ip_list):
key=dns.tsigkeyring.from_text({"cui.com":"SIvcwHj3goqYl0vKS6yOtdugxIaqtwPE43LpB6p9QFpbRmY+ekPrscHz je1hla5RBrB1R1DBnzu9AzT9fVEtfw=="})
up=dns.update.Update("cui.com",keyring=key)
rl=[dns.rdata.from_text(dns.rdataclass.IN,dns.rdatatype.A,i) for i in ip_list]
rs=dns.rdataset.from_rdata_list(60,rl)
up.replace("s.db",rs)
dns.query.tcp(up,‘127.0.0.1‘)
def getSlaveMsg(host,port,user,passwd,cmd):
command="mysql -h %s -P %s -u %s -p%s -e ‘%s‘ "%(host,port,user,passwd,cmd)
p=Popen(command,shell=True,stdout=PIPE)
return p.stdout.readlines()
def getstatus(iostr,sqlstr,secstr,result):
resultdict={}
for i in result:
if iostr in i:
resultdict[iostr]=i.split(":")[1].split("\n")[0].strip()
elif sqlstr in i:
resultdict[sqlstr]=i.split(":")[1].split("\n")[0].strip()
elif secstr in i:
if not i.split(":")[1].split("\n")[0].strip()=="NULL":
resultdict[secstr]=int(i.split(":")[1].split("\n")[0].strip())
else:
resultdict[secstr]=None
else:
pass
return resultdict
def main(iostr,sqlstr,secstr,cmd,**slave_msg):
repl_msg={}
slave_status={}
ip_list=[]
for i in slave_msg.keys():
host=slave_msg[i][0]
port=slave_msg[i][1]
user=slave_msg[i][2]
passwd=slave_msg[i][3]
result=getSlaveMsg(host,port,user,passwd,cmd)
dic_result=getstatus(iostr,sqlstr,secstr,result)
repl_msg[i]=[host,dic_result]
for i in repl_msg.keys():
if repl_msg[i][1][iostr] == "Yes" and repl_msg[i][1][sqlstr] == "Yes":
slave_status[i]=True
elif repl_msg[i][1][secstr] and repl_msg[i][1][secstr] < 2:
slave_status[i]=True
else:
slave_status[i]=False
for i in slave_status.keys():
if slave_status[i]:
ip_list.append(repl_msg[i][0])
status_proportion=float(slave_status.values().count(True))/float(len(slave_status.values()))
if status_proportion > 0.6:
set_dns(ip_list)
else:
pass
return slave_status,ip_list
if __name__ =="__main__":
iostr,sqlstr,secstr,cmd=["Slave_IO_Running","Slave_SQL_Running","Seconds_Behind_Master","show slave status\\G"]
slave_msg={"hostname":["ipaddress",port,"user","passwd"],
"hostname":["ipaddress",port,"user","passwd"],
"hostname":["ipaddress",port,"user","passwd"],
}
repl_msg=main(iostr,sqlstr,secstr,cmd,**slave_msg)
print repl_msg
本文出自 “原创python发布区” 博客,请务必保留此出处http://cuihailong.blog.51cto.com/2923450/1409795
监控mysql同步,修改DNS解析,布布扣,bubuko.com
原文地址:http://cuihailong.blog.51cto.com/2923450/1409795