Python3 monitoring script for WEB servers in the DNS load balancing Environment

Source: Internet
Author: User
Many large websites use multiple server (IP) addresses to provide services for a single website to relieve stress and avoid spof. This includes using DNS round training for load balancing. But for administrators, how can they monitor these servers? The following script can help you implement it. Importsocket, urllib. parse

Many large websites use multiple server (IP) addresses to provide services for a single website to relieve stress and avoid spof.

This includes using DNS round training for load balancing.

But for administrators, how can they monitor these servers?

The following script can help you implement it.

  1. ImportSocket, urllib. parse, configparser, re, sys, http. client,Time
  2. # Coding: UTF-8
  3. # Domain Name Translation IP address Function
  4. DefDom2ip (dname ):
  5. Iplist = socket. gethostbyname _Ex(Dname )[2]
  6. ReturnIplist
  7. # Dictionary data generation function
  8. DefParse (url, key ):
  9. Zhlist [key] = {}
  10. Zhlist [key] [\ 'Host \'] = A. hostname
  11. Zhlist [key] [\ 'Path \'] = A. path
  12. Zhlist [key] [\ 'Port \'] = A. port
  13. Zhlist [key] [\ 'Query \'] = A. query
  14. Zhlist [key] [\ 'Url \'] = A. geturl ()
  15. TrY:
  16. Zhlist [key] [\ 'Ips \'] = Dom2ip (a. hostname)
  17. ExceptException as err:
  18. Print(\ 'Your configuration file format is incorrect, n error message :\', Err,\ 'N' program exited \')
  19. Sys. exit (1)
  20. # Headers
  21. Headers = {
  22. \ 'User-Agent \':\ 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko )\'
  23. }
  24. DefGetinfo (domain, ip, path =\'/\', Port =80):
  25. Headers [\ 'Host \'] = Domain
  26. H = http. client. HTTPConnection (ip, port, timeout =8)
  27. H. request (\ 'Get \', Path, headers = headers)
  28. R = h. getresponse ()
  29. Print(R. getheaders ())
  30. Print(\ 'Server return status \', R.StatUs)
  31. Print(\ 'Server return Result \', R. reason)
  32. Print(\ 'Destination time :\', R. getheader (\ 'Date \'))
  33. Print(\ 'Target Web end :\', R. getheader (\ 'Server \'))
  34. Print(\ 'Last modification time of the target webpage :\', R. getheader (\ 'Last-ModifiEd\'))
  35. Print(\ 'Target content returned :\', R. getheader (\ 'Content-Length \'),\ 'Byte \')
  36. Print(\ 'Destination return code :\', R. getheader (\ 'Content-ENcOding \'))
  37. Try:
  38. # Analyze the character set of a web page
  39. A = r. getheader (\ 'Content-Type \')
  40. B = re.FindAll (\ 'CharSet= (. *) $ \', A, re. I)
  41. ForIInB:
  42. Charset = I
  43. Try:
  44. Charset
  45. Res = res. decode (charset,\ 'Ignore \')
  46. Except:
  47. Res = r. read (2048)
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.