monitor/views.py aktualisiert
Some checks failed
cloudmon GitOps Deploy / build-and-deploy (push) Failing after 25s
Some checks failed
cloudmon GitOps Deploy / build-and-deploy (push) Failing after 25s
This commit is contained in:
parent
872ac0c510
commit
9987533f8a
1 changed files with 15 additions and 18 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
cat << 'EOF' > /home/bast/build/cloudmon/monitor/views.py
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -9,12 +10,7 @@ class DashboardView(TemplateView):
|
||||||
template_name = "monitor/dashboard.html"
|
template_name = "monitor/dashboard.html"
|
||||||
|
|
||||||
class ClusterGraphDataView(View):
|
class ClusterGraphDataView(View):
|
||||||
"""
|
|
||||||
Die Diagnose-Schnittstelle: Fängt jeden Fehler global ab
|
|
||||||
und liefert den echten Python-Fehler als Text zurück!
|
|
||||||
"""
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
# Wir umschließen die ABSOLUT GESAMTE Funktion mit einem globalen Sicherheitsnetz
|
|
||||||
try:
|
try:
|
||||||
repo_name = request.GET.get("repo", "boday_tf")
|
repo_name = request.GET.get("repo", "boday_tf")
|
||||||
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
|
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
|
||||||
|
|
@ -29,7 +25,7 @@ class ClusterGraphDataView(View):
|
||||||
|
|
||||||
consul_data = response.json()
|
consul_data = response.json()
|
||||||
|
|
||||||
# WICHTIG: Consul liefert im KV-Store IMMER eine Liste zurück! [ {"LockIndex": 0, "Key": "...", "Value": "..."} ]
|
# KORRIGIERT: Holt das erste Element [0] aus der Consul-Liste, bevor .get() gerufen wird!
|
||||||
if isinstance(consul_data, list) and len(consul_data) > 0:
|
if isinstance(consul_data, list) and len(consul_data) > 0:
|
||||||
raw_value = consul_data[0].get("Value")
|
raw_value = consul_data[0].get("Value")
|
||||||
else:
|
else:
|
||||||
|
|
@ -55,10 +51,12 @@ class ClusterGraphDataView(View):
|
||||||
res_type = res.get("type")
|
res_type = res.get("type")
|
||||||
res_name = res.get("name")
|
res_name = res.get("name")
|
||||||
|
|
||||||
# Attribut-Struktur sicher auflösen
|
|
||||||
instances = res.get("instances", [])
|
instances = res.get("instances", [])
|
||||||
|
# Falls instances eine Liste ist (oft der Fall im State)
|
||||||
if isinstance(instances, list) and len(instances) > 0:
|
if isinstance(instances, list) and len(instances) > 0:
|
||||||
attributes = instances[0].get("attributes", {})
|
attributes = instances[0].get("attributes", {})
|
||||||
|
elif isinstance(instances, dict):
|
||||||
|
attributes = instances.get("attributes", {})
|
||||||
else:
|
else:
|
||||||
attributes = res.get("values", {})
|
attributes = res.get("values", {})
|
||||||
|
|
||||||
|
|
@ -66,11 +64,11 @@ class ClusterGraphDataView(View):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
metadata = attributes.get("metadata", [{}])
|
metadata = attributes.get("metadata", [{}])
|
||||||
actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) else metadata.get("name", res_name)
|
actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) and len(metadata) > 0 else metadata.get("name", res_name) if hasattr(metadata, "get") else res_name
|
||||||
|
|
||||||
if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
|
if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
|
||||||
spec = attributes.get("spec", [{}])
|
spec = attributes.get("spec", [{}])
|
||||||
rules = spec[0].get("rule", []) if isinstance(spec, list) else spec.get("rule", [])
|
rules = spec[0].get("rule", []) if isinstance(spec, list) and len(spec) > 0 else spec.get("rule", []) if hasattr(spec, "get") else []
|
||||||
ingresses.append({"name": actual_name, "rules": rules})
|
ingresses.append({"name": actual_name, "rules": rules})
|
||||||
elif res_type in ["kubernetes_service", "kubernetes_service_v1"]:
|
elif res_type in ["kubernetes_service", "kubernetes_service_v1"]:
|
||||||
services[actual_name] = {"name": actual_name, "type": "Service"}
|
services[actual_name] = {"name": actual_name, "type": "Service"}
|
||||||
|
|
@ -86,12 +84,12 @@ class ClusterGraphDataView(View):
|
||||||
for ing in ingresses:
|
for ing in ingresses:
|
||||||
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
|
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
|
||||||
for rule in ing["rules"]:
|
for rule in ing["rules"]:
|
||||||
http = rule.get("http", [{}])
|
http = rule.get("http", [{}]) if hasattr(rule, "get") else {}
|
||||||
paths = http[0].get("path", []) if isinstance(http, list) else http.get("path", [])
|
paths = http.get("path", []) if isinstance(http, dict) else http[0].get("path", []) if isinstance(http, list) and len(http) > 0 else []
|
||||||
for path in paths:
|
for path in paths:
|
||||||
backend = path.get("backend", [{}])
|
backend = path.get("backend", [{}]) if hasattr(path, "get") else {}
|
||||||
service_block = backend[0].get("service", {}) if isinstance(backend, list) else backend.get("service", {})
|
service_block = backend.get("service", {}) if isinstance(backend, dict) else backend[0].get("service", {}) if isinstance(backend, list) and len(backend) > 0 else {}
|
||||||
backend_svc = service_block.get("name")
|
backend_svc = service_block.get("name") if hasattr(service_block, "get") else None
|
||||||
|
|
||||||
if backend_svc in services:
|
if backend_svc in services:
|
||||||
svc_node = {"name": backend_svc, "type": "Service", "children": []}
|
svc_node = {"name": backend_svc, "type": "Service", "children": []}
|
||||||
|
|
@ -104,11 +102,10 @@ class ClusterGraphDataView(View):
|
||||||
return JsonResponse(root_node)
|
return JsonResponse(root_node)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# DIE RETTUNG: Falls IRGENDWAS crasht, fangen wir es ab und senden den Fehler als HTTP 200 Text!
|
|
||||||
error_msg = f"Fehler: {str(e)} -> Traceback: {traceback.format_exc()}"
|
error_msg = f"Fehler: {str(e)} -> Traceback: {traceback.format_exc()}"
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
"name": "cloudmon // Diagnose-Modus",
|
"name": "cloudmon // Fehler-Detail",
|
||||||
"type": "Root",
|
"type": "Root",
|
||||||
"error_details": error_msg,
|
"children": [{"name": f"Details: {str(e)}", "type": "Error", "children": []}]
|
||||||
"children": []
|
|
||||||
})
|
})
|
||||||
|
EOF
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue