cloudmon/monitor/views.py

90 lines
3.9 KiB
Python
Raw Normal View History

2026-11-01 16:35:19 +00:00
import json
2026-11-02 17:05:54 +00:00
import base64
import requests
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:
repo_name = request.GET.get("repo", "boday_tf")
2026-11-03 05:10:24 +00:00
# ENTERPRISE RESOLUTION: Nutzt wieder das native Docker-DNS im gemeinsamen Netzwerk!
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 18:20:18 +00:00
return JsonResponse({"name": f"Warte auf Consul-Key '{repo_name}'...", "type": "Root", "children": []})
2026-11-02 17:05:54 +00:00
consul_data = response.json()
2026-11-02 18:20:18 +00:00
raw_value = None
if isinstance(consul_data, list) and len(consul_data) > 0:
2026-11-02 18:20:18 +00:00
first_item = consul_data[0]
if isinstance(first_item, dict):
raw_value = first_item.get("Value")
elif isinstance(consul_data, dict):
raw_value = consul_data.get("Value")
2026-11-02 17:47:39 +00:00
2026-11-02 17:05:54 +00:00
if not raw_value:
2026-11-03 05:10:24 +00:00
return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []})
2026-11-02 17:05:54 +00:00
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
resources = state_json.get("resources", [])
2026-11-02 18:20:18 +00:00
2026-11-01 12:39:17 +00:00
ingresses = []
services = {}
deployments = {}
for res in resources:
2026-11-01 12:39:17 +00:00
res_type = res.get("type")
res_name = res.get("name")
instances = res.get("instances", [])
2026-11-02 18:20:18 +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", {})
else:
attributes = res.get("values", {})
if not attributes:
continue
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-02 17:05:54 +00:00
if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
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-02 18:20:18 +00:00
elif res_type in ["kubernetes_service", "kubernetes_service_v1", "helm_release"]:
services[actual_name] = {"name": actual_name, "type": "Helm-Chart" if res_type == "helm_release" else "Service"}
2026-11-01 16:35:19 +00:00
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
deployments[actual_name] = {"name": actual_name, "type": "Deployment"}
2026-11-01 12:39:17 +00:00
root_node = {
2026-11-03 05:10:24 +00:00
"name": f"Consul: {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": []}
root_node["children"].append(ing_node)
2026-11-02 18:20:18 +00:00
if not root_node["children"]:
for s_name, s_obj in services.items():
root_node["children"].append({"name": s_name, "type": s_obj["type"], "children": []})
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: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:20:18 +00:00
"children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}]
2026-11-02 17:47:39 +00:00
})