cloudmon/monitor/views.py

102 lines
4.3 KiB
Python
Raw Permalink 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:49 +00:00
# Nutzt das stabile Docker-Host-Gateway
consul_url = f"http://172.17.0{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
2026-11-03 05:10:49 +00:00
# KORRIGIERT: Holt explizit das ERSTE Element aus der Consul-Liste über den Index Null!
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
2026-11-03 05:10:49 +00:00
# Base64 decodieren
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-03 05:10:49 +00:00
root_node = {
"name": f"Consul Storage: {repo_name}",
"type": "Root",
"children": []
}
2026-11-01 12:39:17 +00:00
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", {})
else:
continue
2026-11-03 05:10:49 +00:00
if res_type == "helm_release":
metadata_list = attributes.get("metadata", [{}])
metadata = metadata_list[0] if metadata_list else {}
chart_version = metadata.get("version", "1.0.0")
namespace = metadata.get("namespace", "default")
status = attributes.get("status", "deployed")
raw_values_string = metadata.get("values", "{}")
try:
values_dict = json.loads(raw_values_string)
image_tag = values_dict.get("image", {}).get("tag", "latest")
except:
image_tag = "latest"
2026-11-03 05:10:49 +00:00
root_node["children"].append({
"name": attributes.get("name", res_name),
"type": "Helm-Chart",
"meta_status": status,
"meta_namespace": namespace,
"meta_version": chart_version,
"meta_tag": image_tag,
"children": []
})
2026-11-01 12:39:17 +00:00
2026-11-03 05:10:49 +00:00
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
metadata = attributes.get("metadata", [{}])
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
root_node["children"].append({
"name": actual_name,
"type": "Deployment",
"meta_status": "Active",
"meta_namespace": "default",
"meta_version": "v1 (Native K8s)",
"meta_tag": "Managed via Deployment spec",
"children": []
})
2026-11-02 18:20:18 +00:00
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-03 05:10:49 +00:00
"name": "cloudmon // Fehler",
2026-11-02 17:47:39 +00:00
"type": "Root",
2026-11-03 05:10:49 +00:00
"children": [{"name": f"Parsing Fehler: {str(e)}", "type": "Error"}]
2026-11-02 17:47:39 +00:00
})