2026-11-02 18:16:15 +00:00
|
|
|
cat << 'EOF' > /home/bast/build/cloudmon/monitor/views.py
|
2026-11-01 16:35:19 +00:00
|
|
|
import json
|
2026-11-02 17:05:54 +00:00
|
|
|
import base64
|
|
|
|
|
import requests
|
2026-11-02 17:52:07 +00:00
|
|
|
import traceback
|
2026-11-01 12:39:17 +00:00
|
|
|
from django.http import JsonResponse
|
|
|
|
|
from django.views.generic import TemplateView, View
|
|
|
|
|
|
|
|
|
|
class DashboardView(TemplateView):
|
|
|
|
|
template_name = "monitor/dashboard.html"
|
|
|
|
|
|
2026-11-02 17:05:54 +00:00
|
|
|
class ClusterGraphDataView(View):
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2026-11-01 12:39:17 +00:00
|
|
|
try:
|
2026-11-02 17:52:07 +00:00
|
|
|
repo_name = request.GET.get("repo", "boday_tf")
|
|
|
|
|
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
|
|
|
|
|
|
2026-11-02 17:05:54 +00:00
|
|
|
response = requests.get(consul_url, timeout=5)
|
|
|
|
|
if response.status_code == 404:
|
2026-11-02 17:52:07 +00:00
|
|
|
return JsonResponse({
|
|
|
|
|
"name": f"Warte auf Consul-Key '{repo_name}'...",
|
|
|
|
|
"type": "Root",
|
|
|
|
|
"children": []
|
|
|
|
|
})
|
2026-11-02 17:05:54 +00:00
|
|
|
|
2026-11-02 17:52:07 +00:00
|
|
|
consul_data = response.json()
|
|
|
|
|
|
2026-11-02 18:16:15 +00:00
|
|
|
# KORRIGIERT: Holt das erste Element [0] aus der Consul-Liste, bevor .get() gerufen wird!
|
2026-11-02 17:52:07 +00:00
|
|
|
if isinstance(consul_data, list) and len(consul_data) > 0:
|
|
|
|
|
raw_value = consul_data[0].get("Value")
|
2026-11-02 17:47:39 +00:00
|
|
|
else:
|
2026-11-02 17:52:07 +00:00
|
|
|
raw_value = consul_data.get("Value") if hasattr(consul_data, "get") else None
|
2026-11-02 17:47:39 +00:00
|
|
|
|
2026-11-02 17:05:54 +00:00
|
|
|
if not raw_value:
|
2026-11-02 17:52:07 +00:00
|
|
|
return JsonResponse({
|
|
|
|
|
"name": f"Key '{repo_name}' geladen, aber Value ist leer.",
|
|
|
|
|
"type": "Root",
|
|
|
|
|
"children": []
|
|
|
|
|
})
|
2026-11-02 17:05:54 +00:00
|
|
|
|
2026-11-02 17:47:39 +00:00
|
|
|
# Base64 decodieren
|
2026-11-02 17:05:54 +00:00
|
|
|
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
|
|
|
|
|
|
2026-11-02 17:47:39 +00:00
|
|
|
# Flache Ressourcen parsen
|
2026-11-02 17:05:54 +00:00
|
|
|
resources = state_json.get("resources", [])
|
2026-11-01 12:39:17 +00:00
|
|
|
ingresses = []
|
|
|
|
|
services = {}
|
|
|
|
|
deployments = {}
|
|
|
|
|
|
2026-11-01 23:16:57 +00:00
|
|
|
for res in resources:
|
2026-11-01 12:39:17 +00:00
|
|
|
res_type = res.get("type")
|
|
|
|
|
res_name = res.get("name")
|
2026-11-02 17:52:07 +00:00
|
|
|
|
2026-11-01 23:16:57 +00:00
|
|
|
instances = res.get("instances", [])
|
2026-11-02 18:16:15 +00:00
|
|
|
# Falls instances eine Liste ist (oft der Fall im State)
|
2026-11-02 17:52:07 +00:00
|
|
|
if isinstance(instances, list) and len(instances) > 0:
|
|
|
|
|
attributes = instances[0].get("attributes", {})
|
2026-11-02 18:16:15 +00:00
|
|
|
elif isinstance(instances, dict):
|
|
|
|
|
attributes = instances.get("attributes", {})
|
2026-11-02 17:52:07 +00:00
|
|
|
else:
|
|
|
|
|
attributes = res.get("values", {})
|
|
|
|
|
|
|
|
|
|
if not attributes:
|
|
|
|
|
continue
|
2026-11-01 23:16:57 +00:00
|
|
|
|
|
|
|
|
metadata = attributes.get("metadata", [{}])
|
2026-11-02 18:16:15 +00:00
|
|
|
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
|
2026-11-01 23:16:57 +00:00
|
|
|
|
2026-11-02 17:05:54 +00:00
|
|
|
if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
|
2026-11-01 23:16:57 +00:00
|
|
|
spec = attributes.get("spec", [{}])
|
2026-11-02 18:16:15 +00:00
|
|
|
rules = spec[0].get("rule", []) if isinstance(spec, list) and len(spec) > 0 else spec.get("rule", []) if hasattr(spec, "get") else []
|
2026-11-02 17:05:54 +00:00
|
|
|
ingresses.append({"name": actual_name, "rules": rules})
|
2026-11-01 16:35:19 +00:00
|
|
|
elif res_type in ["kubernetes_service", "kubernetes_service_v1"]:
|
2026-11-01 23:16:57 +00:00
|
|
|
services[actual_name] = {"name": actual_name, "type": "Service"}
|
2026-11-01 16:35:19 +00:00
|
|
|
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
|
2026-11-01 23:16:57 +00:00
|
|
|
deployments[actual_name] = {"name": actual_name, "type": "Deployment"}
|
2026-11-01 12:39:17 +00:00
|
|
|
|
|
|
|
|
root_node = {
|
2026-11-02 17:05:54 +00:00
|
|
|
"name": f"Consul Storage: {repo_name}",
|
2026-11-01 12:39:17 +00:00
|
|
|
"type": "Root",
|
|
|
|
|
"children": []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ing in ingresses:
|
|
|
|
|
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
|
|
|
|
|
for rule in ing["rules"]:
|
2026-11-02 18:16:15 +00:00
|
|
|
http = rule.get("http", [{}]) if hasattr(rule, "get") else {}
|
|
|
|
|
paths = http.get("path", []) if isinstance(http, dict) else http[0].get("path", []) if isinstance(http, list) and len(http) > 0 else []
|
2026-11-01 16:35:19 +00:00
|
|
|
for path in paths:
|
2026-11-02 18:16:15 +00:00
|
|
|
backend = path.get("backend", [{}]) if hasattr(path, "get") else {}
|
|
|
|
|
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") if hasattr(service_block, "get") else None
|
2026-11-01 12:39:17 +00:00
|
|
|
|
|
|
|
|
if backend_svc in services:
|
|
|
|
|
svc_node = {"name": backend_svc, "type": "Service", "children": []}
|
|
|
|
|
for dep_name in deployments.keys():
|
|
|
|
|
if dep_name in backend_svc or backend_svc in dep_name:
|
2026-11-01 16:35:19 +00:00
|
|
|
svc_node["children"].append({"name": dep_name, "type": "Deployment", "children": []})
|
2026-11-01 12:39:17 +00:00
|
|
|
ing_node["children"].append(svc_node)
|
|
|
|
|
root_node["children"].append(ing_node)
|
|
|
|
|
|
2026-11-02 17:05:54 +00:00
|
|
|
return JsonResponse(root_node)
|
2026-11-01 12:39:17 +00:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2026-11-02 17:52:07 +00:00
|
|
|
error_msg = f"Fehler: {str(e)} -> Traceback: {traceback.format_exc()}"
|
2026-11-02 17:47:39 +00:00
|
|
|
return JsonResponse({
|
2026-11-02 18:16:15 +00:00
|
|
|
"name": "cloudmon // Fehler-Detail",
|
2026-11-02 17:47:39 +00:00
|
|
|
"type": "Root",
|
2026-11-02 18:16:15 +00:00
|
|
|
"children": [{"name": f"Details: {str(e)}", "type": "Error", "children": []}]
|
2026-11-02 17:47:39 +00:00
|
|
|
})
|
2026-11-02 18:16:15 +00:00
|
|
|
EOF
|