2022-11-26 19:17:00 +01:00
|
|
|
#!/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
|
|
|
|
|
2023-01-07 13:08:44 +01:00
|
|
|
import ltsdb_record
|
2022-11-26 19:17:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
]
|
|
|
|
|
2023-01-07 13:08:44 +01:00
|
|
|
success = ltsdb_record.record_observations(report)
|
2022-11-26 19:17:00 +01:00
|
|
|
exit(1 - success)
|
|
|
|
|