134 lines
4.0 KiB
Python
134 lines
4.0 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")
|
|
table.add_column("ID", justify="right")
|
|
table.add_column("Name")
|
|
table.add_column("Location")
|
|
table.add_column("Vendor")
|
|
table.add_column("CPU", justify="right")
|
|
table.add_column("Memory")
|
|
table.add_column("Storage")
|
|
|
|
nodes = fetch_all("metal_nodes")
|
|
for node in nodes:
|
|
table.add_row(
|
|
str(node[0]), node[1], node[2], node[3],
|
|
str(node[4]), node[5], node[6]
|
|
)
|
|
|
|
console.print(table)
|
|
|
|
def display_virtual_machines():
|
|
console = Console()
|
|
table = Table(title="Virtual Machines")
|
|
table.add_column("ID", justify="right")
|
|
table.add_column("Name")
|
|
table.add_column("Location")
|
|
table.add_column("CPU", justify="right")
|
|
table.add_column("Memory")
|
|
table.add_column("Storage")
|
|
table.add_column("Type")
|
|
|
|
vms = fetch_all("virtual_machines")
|
|
for vm in vms:
|
|
table.add_row(
|
|
str(vm[0]), vm[1], vm[2],
|
|
str(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")
|
|
table.add_column("Node Name")
|
|
table.add_column("CPU", justify="right")
|
|
table.add_column("Memory (MiB)", justify="right")
|
|
table.add_column("Pods Allocatable", justify="right")
|
|
|
|
nodes = v1.list_node()
|
|
for node in nodes.items:
|
|
table.add_row(
|
|
node.metadata.name,
|
|
node.status.capacity.get("cpu"),
|
|
str(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")
|
|
table.add_column("Namespace")
|
|
table.add_column("CPU (Cores)", justify="right")
|
|
table.add_column("Memory (MiB)", justify="right")
|
|
|
|
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__":
|
|
display_metal_nodes()
|
|
display_virtual_machines()
|
|
display_kubernetes_nodes()
|
|
display_namespace_usage()
|