Skip to content

Instantly share code, notes, and snippets.

@jcockhren jcockhren/checks.py Secret
Created Apr 7, 2014

Embed
What would you like to do?
A small worker that records respond times to elasticsearch. This can be adapted to store the response time data anywhere.
# Assuming there's some list of urls to check in the form of:
'''
{ "url:"example.com",
"check_type": "https",
"path": "/"
}
'''
# Setup ES
es = Elasticsearch(config['ES_HOST'])
es_index = "{0}-{1}".format(config['ES_INDEX'],
datetime.date.today().strftime("%Y.%m.%d"))
for s in sites:
print s['value']['url']
r = requests.get("{0}://{1}{2}".format(s['value']['check_type'],
s['value']['url'],
s['value']['path']), verify=False)
# To Elasticsearch
check_time = datetime.datetime.utcnow()
e_check = {"url": s['value']['url'],
"path": s['value']['path'],
"owner_id": s['value']['owner_id'],
"check_type": s['value']['check_type'],
"port": s['value']['port']}
e_check['response_time'] = r.elapsed.microseconds
print "Elapsed Time: {0} ms".format(r.elapsed.microseconds/1000.0)
e_check['@timestamp'] = check_time.isoformat()
if s['value']['check_type'] in ['https', 'http']:
if r.status_code in [200, 301]:
e_check['response_value'] = 0
else:
e_check['response_value'] = 1
es.index(index=es_index, doc_type='check', body=e_check)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.