homelab-brain/homelab-ai-bot/tools/grafana.py

73 lines
2.3 KiB
Python

"""Grafana Monitoring Dashboard Tool."""
import requests
TOOLS = [
{
"type": "function",
"function": {
"name": "get_grafana_status",
"description": "Grafana Monitoring-Status: Health, Dashboards, Datasources, Alerts. Nutze bei 'Grafana', 'Monitoring Dashboard', 'Alerts'.",
"parameters": {"type": "object", "properties": {}, "required": []},
},
},
]
GRAFANA_URL = "http://100.109.206.43:3000"
GRAFANA_USER = "admin"
GRAFANA_PASS = "astral66"
def _api(path):
try:
r = requests.get(
f"{GRAFANA_URL}{path}",
auth=(GRAFANA_USER, GRAFANA_PASS),
timeout=10,
)
if r.ok:
return r.json()
return None
except Exception:
return None
def handle_get_grafana_status(**kw):
health = _api("/api/health")
if not health:
return "Grafana nicht erreichbar (http://100.109.206.43:3000)"
lines = [f"Grafana v{health.get('version', '?')} — DB: {health.get('database', '?')}"]
lines.append(f"URL: https://grafana.orbitalo.net")
datasources = _api("/api/datasources") or []
lines.append(f"\nDatasources: {len(datasources)}")
if datasources:
for ds in datasources:
lines.append(f" {ds.get('name', '?')} ({ds.get('type', '?')}) — {'aktiv' if ds.get('access') else 'inaktiv'}")
else:
lines.append(" Keine konfiguriert — Prometheus muss noch als Datasource hinzugefuegt werden")
dashboards = _api("/api/search?type=dash-db") or []
lines.append(f"\nDashboards: {len(dashboards)}")
for db in dashboards[:10]:
lines.append(f" {db.get('title', '?')} (/{db.get('uri', '?')})")
if not dashboards:
lines.append(" Keine Dashboards vorhanden")
alerts = _api("/api/alertmanager/grafana/api/v2/alerts") or []
if alerts:
firing = [a for a in alerts if a.get("status", {}).get("state") == "active"]
lines.append(f"\nAlerts: {len(alerts)} gesamt, {len(firing)} aktiv")
for a in firing[:5]:
labels = a.get("labels", {})
lines.append(f" {labels.get('alertname', '?')}{labels.get('severity', '?')}")
else:
lines.append("\nAlerts: keine")
return "\n".join(lines)
HANDLERS = {
"get_grafana_status": handle_get_grafana_status,
}