From 251f2e10412a7ab49ca318f7d3d64714edd57712 Mon Sep 17 00:00:00 2001 From: Louis PERDEREAU Date: Thu, 28 Nov 2024 10:42:31 +0100 Subject: [PATCH] fix: Transpose existing sanity checks to new class --- src/pvecontrol/actions/cluster.py | 5 ----- src/pvecontrol/sanitycheck.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/pvecontrol/actions/cluster.py b/src/pvecontrol/actions/cluster.py index b4d5899..c874a3b 100644 --- a/src/pvecontrol/actions/cluster.py +++ b/src/pvecontrol/actions/cluster.py @@ -49,11 +49,6 @@ def action_clusterstatus(proxmox, args): def action_sanitycheck(proxmox, args): """Check status of proxmox Cluster""" - for node in proxmox.nodes: - if (node.maxcpu * proxmox.config['node']['cpufactor']) <= node.allocatedcpu: - print("Node %s is in cpu overcommit status: %s allocated but %s available"%(node.node, node.allocatedcpu, node.maxcpu)) - if (node.allocatedmem + proxmox.config['node']['memoryminimum']) >= node.maxmem: - print("Node %s is in mem overcommit status: %s allocated but %s available"%(node.node, node.allocatedmem, node.maxmem)) # More checks to implement # VM is started but 'startonboot' not set # VM is running in cpu = host diff --git a/src/pvecontrol/sanitycheck.py b/src/pvecontrol/sanitycheck.py index 9bd4adc..da76a78 100644 --- a/src/pvecontrol/sanitycheck.py +++ b/src/pvecontrol/sanitycheck.py @@ -62,12 +62,14 @@ def display(self, padding_max_size): class SanityCheck(): check_methods = ( + "_validate_nodes_capacity", "_validate_ha_groups", "_validate_ha_vms", ) check_display_order = [ CheckType.HA, + CheckType.Node, ] def __init__(self, proxmox: PVECluster): @@ -97,6 +99,32 @@ def display(self): print(f"{dash_size*'-'} {check.type.value} {dash_size*'-'}\n") check.display(size) + def _check_cpu_overcommit(self, maxcpu, cpufactor, allocated_cpu): + return (maxcpu * cpufactor) <= allocated_cpu + + def _check_mem_overcommit(self, max_mem, min_mem, allocated_mem): + return (allocated_mem + min_mem) >= max_mem + + # Check nodes capacity + def _validate_nodes_capacity(self): + node_config = self._proxmox.config['node'] + check = Check(CheckType.Node, "Check Node capacity") + for node in self._proxmox.nodes: + if self._check_cpu_overcommit(node.maxcpu, node_config['cpufactor'], node.allocatedcpu): + msg = "Node %s is in cpu overcommit status: %s allocated but %s available"%(node.node, node.allocatedcpu, node.maxcpu) + check.add_messages(CheckMessage(CheckCode.CRIT, msg)) + else: + msg = f"Node '{node.node}' isn't in cpu overcommit" + check.add_messages(CheckMessage(CheckCode.OK, msg)) + + if self._check_mem_overcommit(node.allocatedmem, node_config['memoryminimum'], node.maxmem): + msg = f"Node '{node.node}' is in mem overcommit status: {node.allocatedmem} allocated but {node.maxmem} available" + check.add_messages(CheckMessage(CheckCode.CRIT, msg)) + else: + msg = f"Node '{node.node}' isn't in cpu overcommit" + check.add_messages(CheckMessage(CheckCode.OK, msg)) + return check + # Check HA groups def _validate_ha_groups(self): check = Check(CheckType.HA, "Check HA groups")