Skip to content

Commit

Permalink
fix(clusterstatus): Update cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
lperdereau authored and lcaflc committed Nov 6, 2024
1 parent 9252603 commit 23d3aa7
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 16 deletions.
55 changes: 43 additions & 12 deletions src/pvecontrol/actions/cluster.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
import logging
from humanize import naturalsize

from pvecontrol.config import get_config
from pvecontrol.utils import filter_keys, print_tableoutput
from pvecontrol.node import NodeStatus


def action_clusterstatus(proxmox, args):
logging.debug(proxmox.status)
logging.debug(proxmox.resources)
status = filter_keys(proxmox.status[0], ['name', 'nodes', 'quorate'])
nodes = [ filter_keys(n, ['name', 'ip', 'online']) for n in proxmox.status[1:] ]
# FIXME get cluster maxmem
# FIXME get cluster maxcpu
# FIXME get cluster allocatedmem
# FIXME get cluster allocatedcpu
print_tableoutput([status])
print_tableoutput(nodes)
status = "healthy" if proxmox.is_healthy() else "not healthy"

templates = sum([len(node.templates()) for node in proxmox.nodes])
vms = sum([len(node.vms) for node in proxmox.nodes])
metrics = proxmox.metrics()

metrics_cpu_output = "{:.2f}/{}({:.1f}%), allocated: {}".format(
metrics['cpu']['usage'],
metrics['cpu']['total'],
metrics['cpu']['percent'],
metrics['cpu']['allocated']
)

metrics_memory_output = "{}/{}({:.1f}%), allocated: {}".format(
naturalsize(metrics['memory']['usage'], binary=True, format="%.2f"),
naturalsize(metrics['memory']['total'], binary=True, format="%.2f"),
metrics['memory']['percent'],
naturalsize(metrics['memory']['allocated'], binary=True, format="%.2f"),
)

metrics_disk_output = "{}/{}({:.1f}%)".format(
naturalsize(metrics['disk']['usage'], binary=True, format="%.2f"),
naturalsize(metrics['disk']['total'], binary=True, format="%.2f"),
metrics['disk']['percent']
)

output = f"""
Status: {status}
VMs: {vms - templates}
Templates: {templates}
Metrics:
CPU: {metrics_cpu_output}
Memory: {metrics_memory_output}
Disk: {metrics_disk_output}
Nodes:
Offline: {len([node for node in proxmox.nodes if node.status == NodeStatus.offline])}
Online: {len([node for node in proxmox.nodes if node.status == NodeStatus.online])}
Unknown: {len([node for node in proxmox.nodes if node.status == NodeStatus.unknown])}
"""

print(output)

def action_sanitycheck(proxmox, args):
"""Check status of proxmox Cluster"""
Expand Down
57 changes: 57 additions & 0 deletions src/pvecontrol/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pvecontrol.node import PVENode
from pvecontrol.task import PVETask


class PVECluster:
"""Proxmox VE Cluster"""

Expand Down Expand Up @@ -57,3 +58,59 @@ def find_task(self, upid):
if task.upid == upid:
return task
return False

def is_healthy(self):
return bool([item for item in self.status if item.get('type') == 'cluster'][0]['quorate'])

def get_resources_nodes(self):
return [resource for resource in self.resources if resource["type"] == "node"]

def get_resources_storages(self):
return [resource for resource in self.resources if resource["type"] == "storage"]

def cpu_metrics(self):
nodes = self.get_resources_nodes()
total_cpu = sum([node['maxcpu'] for node in nodes])
total_cpu_usage = sum([node['cpu'] for node in nodes])
total_cpu_allocated = sum([node.allocatedcpu for node in self.nodes])
cpu_percent = total_cpu_usage / total_cpu *100

return {
"total": total_cpu,
"usage": total_cpu_usage,
"allocated": total_cpu_allocated,
"percent": cpu_percent,
}

def memory_metrics(self):
nodes = self.get_resources_nodes()
total_memory = sum([node['maxmem'] for node in nodes])
total_memory_usage = sum([node['mem'] for node in nodes])
total_memory_allocated = sum([node.allocatedmem for node in self.nodes])
memory_percent = total_memory_usage / total_memory *100

return {
"total": total_memory,
"usage": total_memory_usage,
"allocated": total_memory_allocated,
"percent": memory_percent,
}

def disk_metrics(self):
storages = self.get_resources_storages()
total_disk = sum([node['maxdisk'] for node in storages])
total_disk_usage = sum([node['disk'] for node in storages])
disk_percent = total_disk_usage / total_disk *100

return {
"total": total_disk,
"usage": total_disk_usage,
"percent": disk_percent,
}

def metrics(self):
return {
"cpu": self.cpu_metrics(),
"memory": self.memory_metrics(),
"disk": self.disk_metrics()
}
5 changes: 4 additions & 1 deletion src/pvecontrol/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NodeStatus(Enum):
class PVENode:
"""A proxmox VE Node"""
_api = None

def __init__(self, api, node, status, input = {}):
self.node = node
self.status = NodeStatus[status]
Expand Down Expand Up @@ -83,3 +83,6 @@ def _init_allocatedcpu(self):
# if vm.vmid == item:
# return True
# return False

def templates(self):
return [vm for vm in self.vms if vm.template]
10 changes: 7 additions & 3 deletions src/pvecontrol/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(self, api, node, vmid, status, input = {}):
self.maxmem = 0
self.uptime = 0
self.tags = ""
self.template = 0

for k in input:
if k == "name":
self.name = input["name"]
Expand All @@ -37,12 +39,14 @@ def __init__(self, api, node, vmid, status, input = {}):
self.uptime = input["uptime"]
elif k == "tags":
self.tags = input["tags"]

elif k == "template":
self.template = input["template"]

self.config = self._api.nodes(self.node).qemu(vmid).config.get()

def __str__(self):
return("vmid: {}, status: {}, name: {}, lock: {}, cpus: {}, maxdisk: {}, maxmem: {}, uptime: {}, tags: {}"
.format(self.vmid, self.status, self.name, self.lock, self.cpus, self.maxdisk, self.maxmem, self.uptime, self.tags))
return("vmid: {}, status: {}, name: {}, lock: {}, cpus: {}, maxdisk: {}, maxmem: {}, uptime: {}, tags: {}, template: {}"
.format(self.vmid, self.status, self.name, self.lock, self.cpus, self.maxdisk, self.maxmem, self.uptime, self.tags, self.template))

def migrate(self, target, online = False):
options = {}
Expand Down

0 comments on commit 23d3aa7

Please sign in to comment.