Python Learning note-dns domain name round-robin business monitoring

Source: Internet
Author: User

Most DNS resolution is a domain name corresponding to an IP address, but through DNS round robin technology can be a domain name corresponding to multiple IP addresses, so that can achieve simple and efficient load balancing, but the round-robin technology has a disadvantage is that when the target host is not available, cannot be automatically deleted, This leads to monitoring the availability of the service for the business host.

This example through the analysis of the current domain name resolution IP, combined with the service port detection to achieve automatic monitoring, in the domain name resolution to add, delete IP, no need to monitor the pace of change.

! The "Httplib" module in Python 2.x changes to "Http.client" in Python 3.x

#!/usr/bin/python3#import dns.resolverimport osimport http.clientiplist=[] #定义域名IP列表变量appdomain = "Www.google.cn" #定义业务域名def  get_iplist (domain= ""): #域名解析函数, parse successful IP will be appended to iplisttry:a =  Dns.resolver.query (domain,  ' A ') #解析A记录类型except  Exception as e:print  ("dns  resolver error:  ") +str (e) returnfor i in a.response.answer:for j in  I.items:iplist.append (j.address) #追加到iplistreturn  truedef checkip (IP): checkurl = ip+ ": 80" getcontent =  "" Http.client.socket.setdefaulttimeout (5) #定义http连接超时时间 (5 seconds) conn =  Http.client.HTTPConnection (Checkurl) #创建http连接对象try: Conn.request ("GET",  "/",  headers = {" Host ":  appdomain}) #发起url请求, add host Host Header R = conn.getresponse () Getcontent = r.read (15) # Get the first 15 characters of the URL page to make the availability check finally:if getcontent ==  "<!doctype html>": #监控URL页的内容一般是事先定义好的 , such as "HTTP200" print  (ip+ " [ok]") ELSE:PRINT&NBSP, (ip+ " [error]") #此处可放告警程序, can be mail, SMS notification if __name__ ==  "__main__": If get_iplist ( AppDomain)  and len (IPList) >0: #条件: the domain name resolves correctly and returns at least one Ipfor ip in iplist:checkip (IP) Else: print  ("Dns resolver error.")


This article is from the "Shei" blog, make sure to keep this source http://kurolz.blog.51cto.com/11433546/1935044

Python Learning note-dns domain name round-robin business monitoring

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.