Practice: DNS round robin domain name service monitoring

  Most DNS resolution is a domain name corresponds to an IP address, but can be done through DNS round robin technique a domain name corresponding to multiple IP, enabling simple and efficient load balancing, enabling simple and efficient load balancing, but the biggest drawbacks of this approach is the destination host can not be automatically removed when not available, so do the services available to monitor vital business hosts. This example by analyzing resolve the IP of the current domain, combined with service port detection to automatically monitor, add in DNS, the IP is deleted, without the need for changes to the monitoring scripts. Implementation architecture shown in Figure 1-1.

1, step

(1) implement resolve domain names, access to all of the domain name A records resolve IP lists;

(2) the list of IP HTTP level of detection.

2, code analysis

The first step in this example obtained by dns.resolver.query () method Business A domain name record information, check out all the list of IP addresses in the request using httplib module () method in a manner GET request monitoring page monitoring service for all IP services whether normal service.

from DNS Import Resolver # dnspython introduction method 
Import OS
 Import the httplib2 
 
IPLIST = [] # define ip list variable domain 
AppDomain = ' www.baidu.com ' # define business domain 
 
DEF get_iplist (Domain = "" ):   # # as a function of resolving the domain name , appended to successfully resolve IPLIST 
    the try : 
        A = resolver.query (Domain, ' A ' ) # analytic type A record 
    the except Exception AS E:
         Print ( ' DNS Resolver error: ' +STR (E))
         return 
    for I in A: 
            iplist.append (I) # appended to ip iplist 
    return True 
 
DEF checkip (ip): # of available detectors for the IP iplist 
    checkurl = STR (ip) + " : 80 " 
    getContent = "" 
    httplib2.socket.setdefaulttimeout ( . 5) # define http connection timeout of five seconds 
    Conn = httplib2.HTTPConnectionWithTimeout (checkurl) # Create a connection object http 
 
    the try : 
        conn.request ( " the GET " , " / " , headers = {" HOST " : the AppDomain}) # initiating the request url, add a host header ## access to the target host business by constructing html head 
        the Response = conn.getresponse () 
        getContent = response.read (15) # get the first url 15 characters, do school test with 
    the finally :
         iF getContent == B " <! DOCTYPE HTML> " :      # # determines whether to return the same string with the expected 
                                                   # monitor url generally be defined first 

            Print (STR (IP) + ' [OK] ' )
         the else :
             Print (STR (IP) + ' [error] ' ) # here side warning message, SMS, 
if __name__ == " __main__ " :
     IF get_iplist (the AppDomain) and len (IPLIST)> 0: # DNS correctly returns at least one ip 
        for ip in IPLIST: 
            checkip (ip) 
    the else :
         Print ( ' dns error Resolve ' )

When writing the above code, there will be a lot of problems. Two modules need to be installed on a computer as follows:

Results are as follows:

Two DNS IP addresses, and the service is OK in normal.

 

Guess you like

Origin www.cnblogs.com/hmm01031007/p/11297830.html