Monitor MySQL synchronization, modify DNS resolution

Source: Internet
Author: User

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


This article is from the "Original Python publishing area" blog, so be sure to keep this source http://cuihailong.blog.51cto.com/2923450/1409795

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.