Files
metalcheck/app/extras/pseudographic.py
Aleksandr Tcitlionok 633c4285d8 add(pseudo): colors
2024-12-05 07:42:41 +00:00

148 lines
4.8 KiB
Python

from rich.console import Console
from rich.table import Table
from database import fetch_all
from kubernetes import client, config
# Helper functions for conversions
def convert_cpu_to_cores(cpu):
"""
Convert CPU usage to cores for human-readable format.
"""
if "n" in cpu: # Nanocores to cores
return round(int(cpu.replace("n", "")) / 1e9, 4)
elif "u" in cpu: # Microcores to cores
return round(int(cpu.replace("u", "")) / 1e6, 4)
elif "m" in cpu: # Millicores to cores
return round(int(cpu.replace("m", "")) / 1000, 4)
return float(cpu) # Already in cores
def convert_memory_to_mib(memory):
"""
Convert memory to MiB (mebibytes).
"""
if "Ki" in memory:
return int(memory.replace("Ki", "")) / 1024
elif "Mi" in memory:
return int(memory.replace("Mi", ""))
elif "Gi" in memory:
return int(memory.replace("Gi", "")) * 1024
return float(memory)
def display_metal_nodes():
console = Console()
table = Table(title="🖥️ Metal Nodes", style="bold green")
table.add_column("ID", justify="right", style="cyan")
table.add_column("Name", style="magenta")
table.add_column("Location", style="blue")
table.add_column("Vendor", style="green")
table.add_column("CPU", justify="right", style="yellow")
table.add_column("Memory (GB)", justify="right", style="cyan")
table.add_column("Storage", style="magenta")
nodes = fetch_all("metal_nodes")
for node in nodes:
table.add_row(
f"{node[0]}",
node[1],
node[2],
node[3],
f"{node[4]}",
node[5],
node[6]
)
console.print(table)
def display_virtual_machines():
console = Console()
table = Table(title="💻 Virtual Machines", style="bold blue")
table.add_column("ID", justify="right", style="cyan")
table.add_column("Name", style="magenta")
table.add_column("Location", style="blue")
table.add_column("CPU", justify="right", style="yellow")
table.add_column("Memory (GB)", justify="right", style="cyan")
table.add_column("Storage", style="magenta")
table.add_column("Type", style="green")
vms = fetch_all("virtual_machines")
for vm in vms:
table.add_row(
f"{vm[0]}",
vm[1],
vm[2],
f"{vm[3]}",
vm[4],
vm[5],
vm[6]
)
console.print(table)
def display_kubernetes_nodes():
console = Console()
config.load_incluster_config()
v1 = client.CoreV1Api()
table = Table(title="📦 Kubernetes Nodes", style="bold yellow")
table.add_column("Node Name", style="blue")
table.add_column("CPU", justify="right", style="yellow")
table.add_column("Memory (MiB)", justify="right", style="cyan")
table.add_column("Pods Allocatable", justify="right", style="green")
nodes = v1.list_node()
for node in nodes.items:
table.add_row(
node.metadata.name,
node.status.capacity.get("cpu"),
f"{round(convert_memory_to_mib(node.status.capacity.get('memory')), 2)}",
node.status.allocatable.get("pods")
)
console.print(table)
def display_namespace_usage():
console = Console()
config.load_incluster_config()
metrics_client = client.CustomObjectsApi()
table = Table(title="📊 Namespace Resource Usage", style="bold magenta")
table.add_column("Namespace", style="blue")
table.add_column("CPU (Cores)", justify="right", style="yellow")
table.add_column("Memory (MiB)", justify="right", style="cyan")
namespace_usage = {}
pod_metrics = metrics_client.list_cluster_custom_object(
group="metrics.k8s.io", version="v1beta1", plural="pods"
)
for pod in pod_metrics["items"]:
namespace = pod["metadata"]["namespace"]
if namespace not in namespace_usage:
namespace_usage[namespace] = {"cpu": 0, "memory": 0}
for container in pod["containers"]:
cpu_usage = container["usage"]["cpu"]
memory_usage = container["usage"]["memory"]
namespace_usage[namespace]["cpu"] += convert_cpu_to_cores(cpu_usage)
namespace_usage[namespace]["memory"] += convert_memory_to_mib(memory_usage)
for namespace, usage in namespace_usage.items():
table.add_row(
namespace,
f"{round(usage['cpu'], 4)}",
f"{round(usage['memory'], 2)}"
)
console.print(table)
if __name__ == "__main__":
console = Console()
console.print("✨ [bold green]Welcome to the Metal Check Dashboard![/bold green] ✨\n")
display_metal_nodes()
display_virtual_machines()
display_kubernetes_nodes()
display_namespace_usage()
console.print("\n🚀 [bold cyan]Dashboard rendering complete![/bold cyan] 🚀")