Compare commits

...

12 Commits

Author SHA1 Message Date
clerie caacaced97 Make the prometheus query configurable 2021-05-15 15:22:38 +02:00
clerie 137c81b245 Explain more, what it does. 2021-05-15 15:00:31 +02:00
clerie 14d4f76a46 Add screenshot 2021-05-15 14:56:11 +02:00
clerie 66cc55b499 Add attribution link 2021-05-15 14:51:45 +02:00
clerie 1b741da153 Add gitignore 2021-05-15 14:50:19 +02:00
clerie cfd1084196 Add license 2021-05-15 14:49:40 +02:00
clerie ad0351e9ab Add default api base 2021-05-15 14:45:52 +02:00
clerie fe09b75db9 Configure settings by config file 2021-05-15 14:40:53 +02:00
clerie 5039be644d Restructure code again 2021-05-15 14:31:07 +02:00
clerie 390744abe1 Expose app object for gunicorn 2021-05-15 14:00:49 +02:00
clerie d62971c915 Rearrange module files 2021-05-15 13:55:33 +02:00
clerie 5a0c2edf50 Package module 2021-05-12 21:59:48 +02:00
13 changed files with 125 additions and 32 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.cfg
__pycache__

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 clerie
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,3 +1,33 @@
# Uptime Status
Simple status page for server uptime.
![screenshot](screenshot.png)
Displays the up indicator of node-exporter from your prometheus for the last 14 days in 6h steps.
## Deployment
Init codebase
```
git clone https://github.com/clerie/uptime-status.git
cd uptime-status/
virtualenv -p python3 ENV
cd ..
```
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:
```
cd uptime-status/
git pull
source ENV/bin/activate
pip install -r requirements.txt
UPTIMESTATUS_SETTINGS=/path/to/config.cfg gunicorn uptimestatus:app
deactivate
```

6
app.py Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env python3
from uptimestatus import app
if __name__ == "__main__":
app.run(debug=True)

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
flask
requests

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

18
setup.py Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="uptime-status",
version="0.0.1",
author="clerie",
author_email="hallo@clerie.de",
description="Uptime Status",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://git.clerie.de/clerie/uptime-status",
packages=setuptools.find_packages(),
)

View File

@ -1,31 +0,0 @@
#!/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()

14
uptimestatus/__init__.py Normal file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env python3
from flask import Flask
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')
from .views import app

View File

@ -26,7 +26,7 @@
{% endfor %}
</section>
<footer class="container">
<center>Uptime Status</center>
<center><a href="https://git.clerie.de/clerie/uptime-status">Uptime Status</a> by <a href="https://clerie.de">clerie</a></center>
</footer>
</body>
</html>

15
uptimestatus/utils.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
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"]],
}

16
uptimestatus/views.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
from . import app
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?" + 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)