ltsdb/clients/record_https

63 lines
1.3 KiB
Python
Executable File

#!/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_record
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_record.record_observations(report)
exit(1 - success)