diff --git a/app.py b/app.py index bab18ef..343ae0b 100644 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ import json import logging import os -from flask import (Flask, request, jsonify, abort) +from flask import (Flask, request, jsonify, abort, render_template) from ltsdb_json import LTS @@ -108,3 +108,12 @@ def verify_node(d): else: abort(409, "timestamp out of sync") abort(401, "auth failed") + +@app.get("/v") +def visualize(): + timeseries_ids = request.args.getlist("ts") + timeseries_data = [] + for id in timeseries_ids: + ts = LTS(id=id) + timeseries_data.append(ts) + return render_template("visualize.html", ts=timeseries_data) diff --git a/clients/report_tlscert b/clients/report_tlscert new file mode 100755 index 0000000..382180d --- /dev/null +++ b/clients/report_tlscert @@ -0,0 +1,75 @@ +#!/usr/bin/python3 + +import argparse +import hmac +import json +import os +import socket +import ssl +import time + +import requests + +ap = argparse.ArgumentParser() +ap.add_argument("hostname") +ap.add_argument("port", type=int, default=443, nargs="?") +args = ap.parse_args() + +# It's a bit weird that this works. +myhostname = socket.gethostbyaddr(socket.gethostname())[0] + +now = time.time() +report0 = [] + +with socket.create_connection((args.hostname, args.port)) as sock: + context = ssl.create_default_context() + with context.wrap_socket(sock, server_hostname=args.hostname) as ssock: + cert = ssock.getpeercert() + not_after = ssl.cert_time_to_seconds(cert["notAfter"]) + delta = not_after - now + report0.append({ "measure": "tls_cert_ttl", "unit": "s", "value": delta }) + +report = [ + { + "description": { + "hostname": args.hostname, + "port": args.port, + "measure": r["measure"], + "unit": r["unit"] + }, + "data": [ + [now, r["value"]] + ] + } + for r in report0 +] + +for dir in (".", os.environ["HOME"] + "/.config/ltsdb", "/etc/ltsdb"): + try: + with open(dir + "/config.json") as fh: + client_config = json.load(fh) + baseurl = client_config["server"] + break + except FileNotFoundError: + pass +while True: + for r in report: + node = myhostname + timestamp = time.time() + msg = (node + " " + str(timestamp)).encode("UTF-8") + digest = hmac.new(client_config["key"].encode("UTF-8"), msg, "SHA256").hexdigest() + r["auth"] = { + "node": node, + "timestamp": timestamp, + "hmac": digest, + } + #pprint.pp(report) + r = requests.post(baseurl + "report", json=report) + print(r) + if r.status_code == 200: + exit(0) + elif r.status_code == 409: + time.sleep(0.5 + random.random()) + continue + else: + exit(1) diff --git a/ltsdb_json.py b/ltsdb_json.py index bb155cd..faa64af 100644 --- a/ltsdb_json.py +++ b/ltsdb_json.py @@ -90,3 +90,12 @@ class LTS: result &= ts return result + def data_json_by_row(self): + d = [] + for dp in self.data: + d.append({ + "t": dp[0], + "v": dp[1], + "utc": time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(dp[0])), + }) + return json.dumps(d) diff --git a/templates/visualize.html b/templates/visualize.html new file mode 100644 index 0000000..3fbb8d8 --- /dev/null +++ b/templates/visualize.html @@ -0,0 +1,65 @@ + + +
+ + + + + + + + + + + {% for t in ts %} +{{ k }} | +{{ v }} | +
---|---|
period | ++ {{ t.data[0][0] }} .. {{ t.data[-1][0] }} + | +