Created
May 8, 2012 04:09
-
-
Save whatvn/2632507 to your computer and use it in GitHub Desktop.
This is what I think my application will use to perform heathcheck for http server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def heart_check(target): | |
global last_check | |
if int(time() - last_check) > 5: | |
logger(ctime() + ": Perform heart check") | |
p = re.compile("[a-z0-9-.]*?\.[a-z0-9]+$") | |
domain_tld = p.findall(target)[0] | |
conn = HTTPConnection(domain_tld, timeout = 5) | |
try: | |
accepted_responses = (404, 302, 304, 301, 200) | |
conn.request('HEAD', '/') | |
response = conn.getresponse() | |
if response.status in accepted_responses: return 1 | |
else: | |
logger(target + ' is down') | |
return 0 | |
except Exception, e: | |
logger(target + ' ' + str(e) ) | |
return 0 | |
finally: | |
conn.close() | |
last_check += 5 | |
else: | |
return 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment