Advertisement
Guest User

check_http_tao

a guest
Feb 21st, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. #!/usr/bin/python
  2. import httplib,sys
  3.  
  4. def make_request (uri):
  5.     if "http://" in uri:
  6.         uri = uri.split("http://")[1]
  7.     if "/" in uri:
  8.         host = uri.split("/")[0]
  9.         selector = "/"+uri.split("/",1)[1]
  10.     else:
  11.         host = uri
  12.         selector = "/"
  13.     if sys.version_info < (2,6):
  14.         h = httplib.HTTPConnection (host)
  15.     else:
  16.         h = httplib.HTTPConnection (host,timeout=10)
  17.     h.request ("GET", selector)
  18.     r = h.getresponse()
  19.     headers = {}
  20.     for pair in r.getheaders():
  21.         headers[pair[0]] = pair[1]
  22.     return (r.status, r.reason, headers, r.read())
  23.  
  24. def request_content (uri):
  25.     loop = 0
  26.     while loop < 30:
  27.         try:
  28.             response = make_request (uri)
  29.         except:
  30.             return (2, "HTTP_TAO CRITICAL - Unable to connect")
  31.  
  32.         if 300 <= response[0] <= 400:
  33.             if not "location" in response[2]:
  34.                 return (1, "HTTP_TAO WARNING: Unexpected response: %i %s" % (response[0],response[1]))
  35.             uri = response[2]["location"]
  36.             loop += 1
  37.         elif (response[0] == 200) or (len(sys.argv) > 2 and str(response[0]) in sys.argv[2:]):
  38.             return (0, "HTTP_TAO OK: Status Code %i, %i bytes read" % (response[0], len (response[3])))
  39.         else:
  40.             return (1, "HTTP_TAO WARNING: Unexpected response: %i %s" % (response[0],response[1]))
  41.  
  42.     return (2, "HTTP_TAO CRITICAL: Loop limit reached after 30 redirects")
  43.  
  44. if __name__ == "__main__":
  45.     if len (sys.argv) == 1:
  46.         print "USAGE: check_http_tao HOSTNAME list of allowed status codes\n\tWith HOSTNAME being the FQDN/IP of the target host, optionally followed by a list of accepted status codes (3xx will be attempted to resolve and 200 accepted by default)"
  47.         sys.exit (1)
  48.     retval = request_content (sys.argv[1])
  49.     print retval[1]
  50.     sys.exit(retval[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement