diff --git a/README.md b/README.md index 1aa2a85..54bb674 100644 --- a/README.md +++ b/README.md @@ -475,7 +475,9 @@ VMs that are assigned only to a cluster, without a fixed host (*Device*), are spread over the cluster hosts in proportion to their size: vCPUs by the number of threads, memory by the host RAM. A host with twice the threads carries twice the vCPUs of these VMs. If no host of the cluster has CPU & RAM recorded, the -load is split evenly. The cluster table and the CPU & RAM tab of each host +load is split evenly. As soon as a single VM of the cluster (in any status) is +assigned to one of its hosts, the placement counts as managed by hand: nothing +is spread, and the VMs without a host only count towards the cluster totals. The cluster table and the CPU & RAM tab of each host include this share and mark it as such; the cluster totals count every VM once. VM memory is read in MB as NetBox stores it. **CSV import.** **Hardware > CPU & RAM > Import** accepts `device` (name), diff --git a/netbox_utilities/hardware.py b/netbox_utilities/hardware.py index 1f7a1aa..92a0633 100644 --- a/netbox_utilities/hardware.py +++ b/netbox_utilities/hardware.py @@ -122,6 +122,10 @@ class Capacity: unplaced_vm_count: int = 0 # True for one host of a cluster: vcpus/vm_memory_gb include its share of the unplaced VMs. host_share: bool = False + # Some VM is pinned to a host, so VMs without a host are not spread automatically. + manual_placement: bool = False + # VMs without a host were spread over the hosts. + distributed: bool = False @property def known_hosts(self): @@ -200,12 +204,16 @@ def build_capacity(hosts, vms): vms: iterable of (host key or None, vcpus, memory in MB, status). VMs without a fixed host (only a cluster) are spread over the hosts in proportion to - their size: vCPUs by threads, memory by RAM. + their size: vCPUs by threads, memory by RAM. As soon as any VM of the cluster (in any + status) is pinned to one of its hosts, the placement is managed by hand and nothing is + spread; the unplaced VMs then only count towards the cluster totals. """ capacity = Capacity(hosts=list(hosts)) by_key = {host.key: host for host in capacity.hosts} unplaced_vcpus = unplaced_memory_gb = 0 for host_key, vcpus, memory_mb, status in vms: + if host_key in by_key: + capacity.manual_placement = True if status in INACTIVE_VM_STATUSES: continue vcpus = float(vcpus or 0) @@ -223,7 +231,8 @@ def build_capacity(hosts, vms): host.vcpus += vcpus host.vm_memory_gb += memory_gb - if capacity.hosts and capacity.unplaced_vm_count: + if capacity.hosts and capacity.unplaced_vm_count and not capacity.manual_placement: + capacity.distributed = True for host, weight in zip(capacity.hosts, _weights(capacity.hosts, "threads"), strict=True): host.shared_vcpus = unplaced_vcpus * weight for host, weight in zip(capacity.hosts, _weights(capacity.hosts, "memory_gb"), strict=True): @@ -240,4 +249,6 @@ def host_capacity(cluster_capacity, host): vm_memory_gb=host.total_vm_memory_gb, unplaced_vm_count=cluster_capacity.unplaced_vm_count, host_share=True, + manual_placement=cluster_capacity.manual_placement, + distributed=cluster_capacity.distributed, ) diff --git a/netbox_utilities/templates/netbox_utilities/cluster_capacity_panel.html b/netbox_utilities/templates/netbox_utilities/cluster_capacity_panel.html index ae82350..730f138 100644 --- a/netbox_utilities/templates/netbox_utilities/cluster_capacity_panel.html +++ b/netbox_utilities/templates/netbox_utilities/cluster_capacity_panel.html @@ -45,8 +45,15 @@
{% endif %} diff --git a/netbox_utilities/templates/netbox_utilities/device_hardware.html b/netbox_utilities/templates/netbox_utilities/device_hardware.html index 320577f..7f38867 100644 --- a/netbox_utilities/templates/netbox_utilities/device_hardware.html +++ b/netbox_utilities/templates/netbox_utilities/device_hardware.html @@ -64,7 +64,7 @@ {% else %}