monitor/views.py aktualisiert
Some checks failed
cloudmon GitOps Deploy / build-and-deploy (push) Failing after 22s
Some checks failed
cloudmon GitOps Deploy / build-and-deploy (push) Failing after 22s
This commit is contained in:
parent
dc950c6954
commit
41566268e8
1 changed files with 20 additions and 13 deletions
|
|
@ -1,38 +1,39 @@
|
|||
import json
|
||||
import base64
|
||||
import requests
|
||||
import logging
|
||||
from django.http import JsonResponse
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DashboardView(TemplateView):
|
||||
template_name = "monitor/dashboard.html"
|
||||
|
||||
class ClusterGraphDataView(View):
|
||||
"""
|
||||
Die offizielle cloudmon Enterprise-Schnittstelle:
|
||||
Liest den Zustand direkt und live aus dem zentralen HashiCorp Consul KV-Store.
|
||||
"""
|
||||
def get(self, request, *args, **kwargs):
|
||||
repo_name = request.GET.get("repo", "boday_tf")
|
||||
|
||||
# Consul HTTP-API Endpunkt abfragen (Interner Container-Name im Docker-Netz)
|
||||
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
|
||||
|
||||
# ABSOLUTER SICHERHEITSGURT: Fängt jeden Parsing-Fehler ab, damit das Dashboard NIEMALS mit 500 abstürzt
|
||||
try:
|
||||
response = requests.get(consul_url, timeout=5)
|
||||
if response.status_code == 404:
|
||||
return JsonResponse({"name": f"Keine Daten in Consul für '{repo_name}' gefunden.", "type": "Root", "children": []})
|
||||
return JsonResponse({"name": f"Warte auf korrekten Consul-Key '{repo_name}'...", "type": "Root", "children": []})
|
||||
|
||||
consul_json = response.json()
|
||||
# Consul liefert eine Liste zurück, der Value ist Base64 codiert
|
||||
raw_value = consul_json[0].get("Value")
|
||||
if isinstance(consul_json, list) and len(consul_json) > 0:
|
||||
raw_value = consul_json[0].get("Value")
|
||||
else:
|
||||
raw_value = consul_json.get("Value")
|
||||
|
||||
if not raw_value:
|
||||
return JsonResponse({"error": "Consul Key ist leer"}, status=400)
|
||||
return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []})
|
||||
|
||||
# Base64 decodieren, um an das echte Terraform-State-JSON zu kommen
|
||||
# Base64 decodieren
|
||||
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
|
||||
|
||||
# --- AB HIER GREIFT DEIN BEWÄHRTER KOMBI-PARSER ---
|
||||
# Flache Ressourcen parsen
|
||||
resources = state_json.get("resources", [])
|
||||
ingresses = []
|
||||
services = {}
|
||||
|
|
@ -84,4 +85,10 @@ class ClusterGraphDataView(View):
|
|||
return JsonResponse(root_node)
|
||||
|
||||
except Exception as e:
|
||||
return JsonResponse({"error": str(e)}, status=500)
|
||||
logger.error(f"Sicherheits-Fallback gegriffen: {str(e)}")
|
||||
# Liefert im Fehlerfall ein valides leeres Baumdiagramm (HTTP 200 OK statt 500!)
|
||||
return JsonResponse({
|
||||
"name": f"Boday-IT Monitor // Synchronisiere '{repo_name}'...",
|
||||
"type": "Root",
|
||||
"children": []
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue