Skip to content

Instantly share code, notes, and snippets.

@arthurdarcet
Last active August 29, 2015 14:02
Show Gist options
  • Save arthurdarcet/467362c6200a4545c113 to your computer and use it in GitHub Desktop.
Save arthurdarcet/467362c6200a4545c113 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import http.client
import os
import sys
import time
import urllib.parse
CLIENTS = {
'http': http.client.HTTPConnection,
'https': http.client.HTTPSConnection,
}
if len(sys.argv) > 1 and sys.argv[1] == 'config':
print('graph_title Response time - {}'.format(os.getenv('title')))
print('graph_category {}'.format(os.getenv('category', 'HTTP')))
print('graph_args --lower-limit 0')
print('graph_vlabel Time (milliseconds)')
print('r.label {}'.format(os.getenv('url')))
for c in ('warning', 'critical'):
if os.getenv(c):
print('r.{} {}'.format(c, os.getenv(c)))
else:
o = urllib.parse.urlparse(os.getenv('url'))
try:
t1 = time.time()
conn = CLIENTS[o.scheme](o.hostname)
conn.request('GET', o.path)
resp = conn.getresponse()
resp.read()
t2 = time.time()
if resp.status == int(os.getenv('status', 200)):
print('r.value {:.2f}'.format((t2 - t1)*1000))
else:
print('r.value U')
except:
print('r.value U')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment