Measure HTTP(S) response code and time
This commit is contained in:
parent
4bf3508d26
commit
dd1b3056ac
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import argparse
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import pprint
|
||||
import random
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
import re
|
||||
import urllib.parse
|
||||
|
||||
import requests
|
||||
|
||||
import ltsdb_report
|
||||
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("url")
|
||||
args = ap.parse_args()
|
||||
|
||||
url = urllib.parse.urlparse(args.url)
|
||||
hostname = url.hostname
|
||||
port = url.port
|
||||
if port is None:
|
||||
if url.scheme == "https":
|
||||
port = 443
|
||||
elif url.scheme == "http":
|
||||
port = 80
|
||||
else:
|
||||
raise RuntimeError("cannot determine port number")
|
||||
|
||||
t0 = time.time()
|
||||
response = requests.get(args.url)
|
||||
t1 = time.time()
|
||||
dt = t1 - t0
|
||||
|
||||
report0 = []
|
||||
report0.append({ "measure": "rtt", "unit": "s", "value": dt })
|
||||
report0.append({ "measure": "http_status_code", "unit": "http_status_code", "value": response.status_code })
|
||||
report0.append({ "measure": "content_length", "unit": "bytes", "value": len(response.content) })
|
||||
|
||||
report = [
|
||||
{
|
||||
"description": {
|
||||
"hostname": hostname,
|
||||
"port": port,
|
||||
"url": args.url,
|
||||
"measure": r["measure"],
|
||||
"unit": r["unit"]
|
||||
},
|
||||
"data": [
|
||||
[t0, r["value"]]
|
||||
]
|
||||
}
|
||||
for r in report0
|
||||
]
|
||||
|
||||
success = ltsdb_report.send_report(report)
|
||||
exit(1 - success)
|
||||
|
Loading…
Reference in New Issue