Replace service and delivery screenshots with live status
This commit is contained in:
@@ -3,10 +3,12 @@ import os
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
PROMETHEUS_URL = os.getenv("PROMETHEUS_URL", "http://host.docker.internal:9090").rstrip("/")
|
||||
TIMEOUT = float(os.getenv("PROMETHEUS_TIMEOUT", "2.5"))
|
||||
SITE_GIT_DIR = Path(os.getenv("SITE_GIT_DIR", "/site/.git"))
|
||||
|
||||
QUERIES = {
|
||||
"cpu_percent": '100-(avg(rate(node_cpu_seconds_total{mode="idle"}[2m]))*100)',
|
||||
@@ -15,11 +17,23 @@ QUERIES = {
|
||||
"running_containers": 'count(count by (name) (container_last_seen{name!=""}))',
|
||||
}
|
||||
|
||||
HTTP_CHECKS = {
|
||||
"gitea": "http://host.docker.internal:3000/api/v1/version",
|
||||
"woodpecker": "http://host.docker.internal:8010/",
|
||||
"uptime_kuma": "http://host.docker.internal:3001/",
|
||||
"prometheus": "http://host.docker.internal:9090/-/healthy",
|
||||
}
|
||||
|
||||
def get_json(url):
|
||||
request = urllib.request.Request(url, headers={"Accept": "application/json"})
|
||||
with urllib.request.urlopen(request, timeout=TIMEOUT) as response:
|
||||
return json.load(response)
|
||||
|
||||
def get_text(url):
|
||||
request = urllib.request.Request(url, headers={"Accept": "*/*"})
|
||||
with urllib.request.urlopen(request, timeout=TIMEOUT) as response:
|
||||
return response.status, response.read(1024).decode("utf-8", "replace")
|
||||
|
||||
def prometheus_query(expression):
|
||||
url = f"{PROMETHEUS_URL}/api/v1/query?{urllib.parse.urlencode({'query': expression})}"
|
||||
payload = get_json(url)
|
||||
@@ -43,9 +57,51 @@ def target_health():
|
||||
"jobs": jobs,
|
||||
}
|
||||
|
||||
def check_services():
|
||||
services = {}
|
||||
for name, url in HTTP_CHECKS.items():
|
||||
try:
|
||||
status, body = get_text(url)
|
||||
services[name] = {"up": 200 <= status < 400, "status": status}
|
||||
if name == "gitea":
|
||||
try:
|
||||
services[name]["version"] = json.loads(body).get("version")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
services[name] = {"up": False, "status": None}
|
||||
return services
|
||||
|
||||
def deployed_commit():
|
||||
try:
|
||||
head = (SITE_GIT_DIR / "HEAD").read_text(encoding="utf-8").strip()
|
||||
if head.startswith("ref: "):
|
||||
ref = head[5:]
|
||||
ref_path = SITE_GIT_DIR / ref
|
||||
if ref_path.exists():
|
||||
value = ref_path.read_text(encoding="utf-8").strip()
|
||||
else:
|
||||
value = None
|
||||
packed = SITE_GIT_DIR / "packed-refs"
|
||||
if packed.exists():
|
||||
for line in packed.read_text(encoding="utf-8").splitlines():
|
||||
if line and not line.startswith(("#", "^")):
|
||||
sha, name = line.split(" ", 1)
|
||||
if name == ref:
|
||||
value = sha
|
||||
break
|
||||
if not value:
|
||||
return None
|
||||
else:
|
||||
value = head
|
||||
return value[:7]
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def build_status():
|
||||
values = {name: prometheus_query(query) for name, query in QUERIES.items()}
|
||||
targets = target_health()
|
||||
services = check_services()
|
||||
return {
|
||||
"ok": True,
|
||||
"source": "Prometheus / node_exporter / cAdvisor",
|
||||
@@ -57,6 +113,11 @@ def build_status():
|
||||
},
|
||||
"containers": {"running": int(values["running_containers"])},
|
||||
"prometheus": targets,
|
||||
"services": services,
|
||||
"deployment": {
|
||||
"commit": deployed_commit(),
|
||||
"site": "bytegeist.casko.dev",
|
||||
},
|
||||
}
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
Reference in New Issue
Block a user