Skip to content

Instantly share code, notes, and snippets.

@keiya
Last active December 15, 2015 23:49
Show Gist options
  • Select an option

  • Save keiya/5342889 to your computer and use it in GitHub Desktop.

Select an option

Save keiya/5342889 to your computer and use it in GitHub Desktop.
this script checks the health status of the http server periodically. health report will be write out as csv format: ISO-8601,HTTP_STATUS_CODE,RESPONSE_TIME_MICROSEC USAGE: $ python3 -u http_heartbeat.py | tee ~/hb.log
#!/usr/bin/python
import pprint,http.client,sys
from datetime import datetime
from time import sleep
if (len(sys.argv) != 2) :
print ( "Usage: python3 {0} host" .format(sys.argv[0]) )
quit()
while 1:
conn = http.client.HTTPConnection( sys.argv[1] )
start = datetime.now()
conn.request("GET","/")
res = conn.getresponse()
end = datetime.now()
duration = (end-start).microseconds
timestamp = start.strftime("%Y-%m-%dT%H:%M:%S")
if res.status == 200 :
print (timestamp + "," + str(res.status) + "," + str(duration))
else :
print (timestamp + "," + str(res.status) + "," + str(duration))
conn.close
sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment