From 9aaf2b0b7e5b6e172576679a6400f5411feb7889 Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 12 May 2021 21:50:43 +0200 Subject: [PATCH] Add software --- static/status.css | 76 +++++++++++++++++++++++++++++++++++++++++++ templates/status.html | 32 ++++++++++++++++++ uptime-status.py | 31 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 static/status.css create mode 100644 templates/status.html create mode 100644 uptime-status.py diff --git a/static/status.css b/static/status.css new file mode 100644 index 0000000..1be1de1 --- /dev/null +++ b/static/status.css @@ -0,0 +1,76 @@ +:root { + --us-ok: #34d058; + --us-warning: #fff432; + --us-critical: #FF3D33; + --us-gray: #d8d8d8; + + font-family: Roboto, Arial, sans-serif; + font-weight: 300; +} + +.container { + width: calc(100% - 1em); + max-width: 750px; + margin-left: auto; + margin-right: auto; + padding-left: 0.5em; + padding-right: 0.5em; +} + +.service { + display: flex; + width: 100%; + margin-top: 1em; + margin-bottom: 1em; +} + +.service-name { + width: 30%; +} + +.service-status { + width: 70%; +} + +.status-bar { + display: flex; + height: 2em; + width: 100%; +} + +.status-bar .status-cell { + background-color: var(--us-gray); + height: 100%; + width: 100%; + margin-left: 1px; + margin-right: 1px; + border-radius: 2px; +} + +.status-bar .status-cell.ok { + background-color: var(--us-ok); +} + +.status-bar .status-cell.warning { + background-color: var(--us-warning); +} + +.status-bar .status-cell.critical { + background-color: var(--us-critical); +} + +header { + margin-top: 5em; + margin-bottom: 5em; +} + +header h1 { + font-size: 6rem; + font-weight: 300; + line-height: 1.2; +} + +footer { + margin-top: 3em; + margin-bottom: 3em; +} diff --git a/templates/status.html b/templates/status.html new file mode 100644 index 0000000..b56c592 --- /dev/null +++ b/templates/status.html @@ -0,0 +1,32 @@ + + + + + Uptime Status + + + +
+

Uptime Status

+
+
+ {% for metric in metrics %} +
+
+ {{ metric.name }} +
+
+
+ {% for value in metric.i %} +
+ {% endfor %} +
+
+
+ {% endfor %} +
+ + + diff --git a/uptime-status.py b/uptime-status.py new file mode 100644 index 0000000..02dd700 --- /dev/null +++ b/uptime-status.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +from flask import Flask, render_template +import json +import requests + +app = Flask(__name__) + +def ratio_to_status(ratio): + if ratio > 0.99: + return "ok" + elif ratio > 0.8: + return "warning" + else: + return "critical" + +def process_instance(instance): + return { + "name": instance["metric"]["instance"].split(".")[0], + "i": [{"status": "unknown", "ratio": None} for i in range(56 - len(instance["values"]))] + [{"status": ratio_to_status(float(i[1])), "ratio": float(i[1])} for i in instance["values"]], + } + +@app.route("/") +def status(): + r = requests.get("https://prometheus.monitoring.clerie.de/api/v1/query?query=%28sum_over_time%28up%7Bjob%3D%22node-exporter%22%7D%5B6h%5D%29+%2F+count_over_time%28up%7Bjob%3D%22node-exporter%22%7D%5B6h%5D%29%29%5B14d%3A6h%5D") + j = json.loads(r.text) + metrics = sorted(map(process_instance, j["data"]["result"]), key=lambda m: m["name"]) + return render_template("status.html", metrics=metrics) + +if __name__ == "__main__": + app.run()