Browse Source

Make the prometheus query configurable

main
clerie 1 year ago
parent
commit
caacaced97
  1. 1
      README.md
  2. 1
      uptimestatus/__init__.py
  3. 3
      uptimestatus/views.py

1
README.md

@ -19,6 +19,7 @@ Create `config.cfg` with the following contents and edit values for your needs: @@ -19,6 +19,7 @@ Create `config.cfg` with the following contents and edit values for your needs:
```
PROMETHEUS_API_BASE="http://[::1]:9090"
PROMETHEUS_QUERY='(sum_over_time(up{job="node-exporter"}[6h]) / count_over_time(up{job="node-exporter"}[6h]))[14d:6h]'
```
Starten und updaten lässt sich die Flask-App folgendermaßen:

1
uptimestatus/__init__.py

@ -6,6 +6,7 @@ import os @@ -6,6 +6,7 @@ import os
app = Flask(__name__)
app.config["PROMETHEUS_API_BASE"] = "http://[::1]:9090"
app.config["PROMETHEUS_QUERY"] = '(sum_over_time(up{job="node-exporter"}[6h]) / count_over_time(up{job="node-exporter"}[6h]))[14d:6h]'
if "UPTIMESTATUS_SETTINGS" in os.environ:
app.config.from_envvar('UPTIMESTATUS_SETTINGS')

3
uptimestatus/views.py

@ -6,10 +6,11 @@ from .utils import process_instance @@ -6,10 +6,11 @@ from .utils import process_instance
from flask import render_template
import json
import requests
import urllib.parse
@app.route("/")
def status():
r = requests.get(app.config["PROMETHEUS_API_BASE"] + "/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")
r = requests.get(app.config["PROMETHEUS_API_BASE"] + "/api/v1/query?" + urllib.parse.urlencode({'query': app.config["PROMETHEUS_QUERY"]}))
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)

Loading…
Cancel
Save