实践:DNS域名轮循业务监控

大部分的DNS解析都是一个域名对应一个IP地址,但是通过DNS轮循技术可以做到一个域名对应多个IP,从而实现最简单且高效的负载平衡,不过此方案最大的弊端是目标主机不可用时无法被自动剔除.

[root@bogon dns]# cat SJ.py

!/usr/bin/python

import dns.resolver
import os
import httplib

iplist=[]

appdomain=raw_input("please input Monitoring of the domain name:")

def get_iplist(domain=""):
try:
A = dns.resolver.query(domain, 'A')
except Exception,e:
print "dns resolver error:"+str(e)
return
for i in A.response.answer:
for j in i.items:
iplist.append(j.address)
return True

def checkip(ip):
checkurl=ip+":80"
getcontent=""
httplib.socket.setdefaulttimeout(5)
conn=httplib.HTTPConnection(checkurl)

try:  
    conn.request("GET", "/",headers = {"Host": appdomain})    
                                                              
    r=conn.getresponse()  
    getcontent =r.read(15)  
finally:  
    if getcontent=="":    
                                       
        print ip+" [OK]"  
    else:  
        print ip+" [Error]"    

if name=="main":
if get_iplist(appdomain) and len(iplist)>0:
for ip in iplist:
checkip(ip)
else:
print "dns resolver error."

测试:

输入你自己的域名,随便输入一个会报错

[root@bogon dns]# python SJ.py
please input Monitoring of the domain name:www.51auto.com
203.80.144.183 [Error]
[root@bogon dns]#

你可能感兴趣的:(实践:DNS域名轮循业务监控)