From c4b59d34db126a4d3471162ff34a04661ff41c99 Mon Sep 17 00:00:00 2001 From: jwansek Date: Tue, 2 Feb 2021 15:58:31 +0000 Subject: added status page with timeouts --- app.py | 15 ++++++- requirements.txt | 11 +++-- services.py | 52 ++++++++++++++++++++++-- static/style.css | 20 +++++++++ templates/services.html | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ templates/template.html | 1 + 6 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 templates/services.html diff --git a/app.py b/app.py index b52cd0b..935ef12 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import configparser import database +import services import flask app = flask.Flask(__name__) @@ -27,9 +28,21 @@ def discord(): with database.Database() as db: return flask.render_template( "discord.html", - **get_template_items("Discord", db), + **get_template_items("discord", db), discord = CONFIG["discord"]["username"] ) +@app.route("/services") +def serve_services(): + with database.Database() as db: + return flask.render_template( + "services.html", + **get_template_items("services", db), + docker = services.get_docker_stats(), + qbit = services.get_qbit_stats(), + trans = services.get_trans_stats(), + pihole = services.get_pihole_stats() + ) + if __name__ == "__main__": app.run(host = "0.0.0.0", debug = True) diff --git a/requirements.txt b/requirements.txt index 0fed313..e86483b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ -Flask==1.1.2 +python_qbittorrent==0.4.2 PyMySQL==1.0.2 -docker==4.4.1 -python-qbittorrent==0.4.2 -transmission-clutch \ No newline at end of file +Flask==1.1.2 +PiHole_api==2.6 +clutch==0.1a2 +docker_py==1.10.6 +pihole==0.1.2 +qbittorrent==0.1.6 diff --git a/services.py b/services.py index 16268ef..dd70b33 100644 --- a/services.py +++ b/services.py @@ -1,8 +1,12 @@ import qbittorrent +import multiprocessing import docker import clutch -import app +import pihole +import queue import json +import time +import app def humanbytes(B): 'Return the given bytes as a human friendly KB, MB, GB, or TB string' @@ -23,13 +27,41 @@ def humanbytes(B): elif TB <= B: return '{0:.2f} TB'.format(B/TB) +def timeout(func): + # cant get this to work with queue.Queue() for some reason? + # this works but Manager() uses an extra thread than Queue() + manager = multiprocessing.Manager() + returnVan = manager.list() + ti = time.time() + + def runFunc(q, func): + q.append(func()) + + def beginTimeout(): + t = multiprocessing.Process(target = runFunc, args = (returnVan, func)) + t.start() + + t.join(timeout = app.CONFIG["servicetimeout"].getint("seconds")) + + # print("Request took:", time.time() - ti) + try: + return returnVan[0] + except IndexError: + if t.is_alive(): + t.terminate() + + return beginTimeout + +@timeout def get_docker_stats(): + client = docker.DockerClient(base_url = "tcp://%s:%s" % (app.CONFIG["docker"]["url"], app.CONFIG["docker"]["port"])) return { container.name: container.status for container in client.containers.list(all = True) } +@timeout def get_qbit_stats(): numtorrents = 0 bytes_dl = 0 @@ -46,9 +78,10 @@ def get_qbit_stats(): "bytes_dl": humanbytes(bytes_dl), "bytes_up": humanbytes(bytes_up), "num": numtorrents, - "ratio": bytes_up / bytes_dl + "ratio": "%.3f" % (float(bytes_up) / float(bytes_dl)) } +@timeout def get_trans_stats(): client = clutch.client.Client( address = "http://%s:%s/transmission/rpc" % (app.CONFIG["transmission"]["url"], app.CONFIG["transmission"]["port"]), @@ -60,9 +93,20 @@ def get_trans_stats(): "bytes_dl": humanbytes(stats["arguments"]["cumulative_stats"]["downloaded_bytes"]), "bytes_up": humanbytes(stats["arguments"]["cumulative_stats"]["uploaded_bytes"]), "num": stats["arguments"]["torrent_count"], - "ratio": stats["arguments"]["cumulative_stats"]["uploaded_bytes"] / stats["arguments"]["cumulative_stats"]["downloaded_bytes"] + "ratio": "%.3f" % (float(stats["arguments"]["cumulative_stats"]["uploaded_bytes"]) / float(stats["arguments"]["cumulative_stats"]["downloaded_bytes"])) + } + +@timeout +def get_pihole_stats(): + ph = pihole.PiHole(app.CONFIG["pihole"]["url"]) + return { + "status": ph.status, + "queries": ph.total_queries, + "clients": ph.unique_clients, + "percentage": ph.ads_percentage, + "blocked": ph.blocked } if __name__ == "__main__": - print(get_trans_stats()) \ No newline at end of file + print(get_qbit_stats()) \ No newline at end of file diff --git a/static/style.css b/static/style.css index 952d3ca..7bc2190 100644 --- a/static/style.css +++ b/static/style.css @@ -82,6 +82,26 @@ nav a { background-color: white; } +article section table { + font-family: monospace; +} + +article section table td { + text-align: right; +} + +.running { + background-color: green; + padding: 1, 1, 1, 1; + margin: 0, 0, 2, 3; +} + +.notRunning { + background-color: red; + padding: 1, 1, 1, 1; + margin: 0, 0, 2, 3; +} + header img { max-height: 110px; } diff --git a/templates/services.html b/templates/services.html new file mode 100644 index 0000000..321a7a0 --- /dev/null +++ b/templates/services.html @@ -0,0 +1,105 @@ +{% extends "template.html" %} +{% block content %} +
+
+

docker

+ {% if docker == None %} +

Couldn't access the docker API. Is sherpa running?

+ {% else %} + + {% for name, status in docker.items() %} + + + {% if status == "running" %} + + {% else %} + + {% endif %} + + {% endfor %} +
{{name}}{{status}}{{status}}
+ {% endif %} +
+
+

qbittorrent

+ {% if qbit == None %} +

Couldn't access the qbittorrent API. Is docker container running?

+ {% else %} + + + + + + + + + + + + + + + + + +
downloaded{{qbit["bytes_dl"]}}
uploaded{{qbit["bytes_up"]}}
torrents{{qbit["num"]}}
ratio{{qbit["ratio"]}}
+ {% endif %} +

transmission

+ {% if trans == None %} +

Couldn't access the transmission API. Is docker container running?

+ {% else %} + + + + + + + + + + + + + + + + + +
downloaded{{trans["bytes_dl"]}}
uploaded{{trans["bytes_up"]}}
torrents{{trans["num"]}}
ratio{{trans["ratio"]}}
+ {% endif %} +
+
+

pihole

+ {% if pihole == None %} +

Couldn't access the pihole API. Is docker container running?

+ {% else %} + + + + {% if pihole["status"] == "enabled" %} + + {% else %} + + {% endif %} + + + + + + + + + + + + + + + + + +
status{{pihole["status"]}}{{pihole["status"]}}
queries{{pihole["queries"]}}
clients{{pihole["clients"]}}
percentage blocked{{pihole["percentage"]}}%
blocked{{pihole["blocked"]}}
+ {% endif %} +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/template.html b/templates/template.html index d6c1a0b..6b0d894 100644 --- a/templates/template.html +++ b/templates/template.html @@ -2,6 +2,7 @@ + edaweb :: {{title}}
-- cgit v1.2.3