feat: spread cluster VMs without a fixed host over the cluster hosts
VMs assigned only to a cluster now count towards every host of that cluster in proportion to its size: vCPUs by threads, memory by host RAM, evenly if no host has CPU & RAM recorded. The cluster host table and the CPU & RAM tab of a host include this share; the cluster totals still count each VM once. Hosts are matched by device ID instead of name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
675574ee00
commit
afa15eb46a
8 files changed
+156
-21
No files matched your search
@@ -469,7 +469,14 @@ machines (vCPUs and memory of the VM):
|
||||
Warnings appear when VMs are allocated more RAM than the hosts have, when the
|
||||
RAM no longer fits after the failure of the largest host, and when the vCPU
|
||||
ratio exceeds `hardware_vcpu_ratio_warning`. VMs with the status *Offline* or
|
||||
*Decommissioning* are not counted. VM memory is read in MB as NetBox stores it.
|
||||
*Decommissioning* are not counted.
|
||||
|
||||
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
|
||||
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),
|
||||
`cpu` (catalogue model name, e.g. `Xeon Gold 6338`), `cpu_count`, `memory_gb`,
|
||||
|
||||
@@ -79,20 +79,39 @@ def percent(part, whole):
|
||||
|
||||
@dataclass
|
||||
class Host:
|
||||
"""One physical host; cores/threads/memory are None when no hardware is recorded."""
|
||||
"""One physical host; cores/threads/memory are None when no hardware is recorded.
|
||||
|
||||
vcpus/vm_memory_gb come from VMs placed on this host, shared_* is its share of the
|
||||
cluster VMs without a fixed host (see build_capacity).
|
||||
"""
|
||||
|
||||
name: str
|
||||
cores: int | None = None
|
||||
threads: int | None = None
|
||||
memory_gb: float | None = None
|
||||
key: object = None
|
||||
vcpus: float = 0
|
||||
vm_memory_gb: float = 0
|
||||
vm_count: int = 0
|
||||
shared_vcpus: float = 0
|
||||
shared_memory_gb: float = 0
|
||||
|
||||
def __post_init__(self):
|
||||
if self.key is None:
|
||||
self.key = self.name
|
||||
|
||||
@property
|
||||
def complete(self):
|
||||
return bool(self.cores and self.memory_gb)
|
||||
|
||||
@property
|
||||
def total_vcpus(self):
|
||||
return self.vcpus + self.shared_vcpus
|
||||
|
||||
@property
|
||||
def total_vm_memory_gb(self):
|
||||
return self.vm_memory_gb + self.shared_memory_gb
|
||||
|
||||
|
||||
@dataclass
|
||||
class Capacity:
|
||||
@@ -101,6 +120,8 @@ class Capacity:
|
||||
vcpus: float = 0
|
||||
vm_memory_gb: float = 0
|
||||
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
|
||||
|
||||
@property
|
||||
def known_hosts(self):
|
||||
@@ -163,15 +184,28 @@ class Capacity:
|
||||
return result
|
||||
|
||||
|
||||
def _weights(hosts, attribute):
|
||||
"""Share per host proportional to its size; equal shares if no host has the value recorded."""
|
||||
sizes = [getattr(host, attribute) or 0 for host in hosts]
|
||||
total = sum(sizes)
|
||||
if not total:
|
||||
return [1 / len(hosts)] * len(hosts)
|
||||
return [size / total for size in sizes]
|
||||
|
||||
|
||||
def build_capacity(hosts, vms):
|
||||
"""Aggregate hosts and VMs.
|
||||
|
||||
hosts: iterable of Host (memory in GB).
|
||||
vms: iterable of (host name or None, vcpus, memory in MB, status).
|
||||
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.
|
||||
"""
|
||||
capacity = Capacity(hosts=list(hosts))
|
||||
by_name = {host.name: host for host in capacity.hosts}
|
||||
for host_name, vcpus, memory_mb, status in vms:
|
||||
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 status in INACTIVE_VM_STATUSES:
|
||||
continue
|
||||
vcpus = float(vcpus or 0)
|
||||
@@ -179,11 +213,31 @@ def build_capacity(hosts, vms):
|
||||
capacity.vm_count += 1
|
||||
capacity.vcpus += vcpus
|
||||
capacity.vm_memory_gb += memory_gb
|
||||
host = by_name.get(host_name)
|
||||
host = by_key.get(host_key)
|
||||
if host is None:
|
||||
capacity.unplaced_vm_count += 1
|
||||
unplaced_vcpus += vcpus
|
||||
unplaced_memory_gb += memory_gb
|
||||
else:
|
||||
host.vm_count += 1
|
||||
host.vcpus += vcpus
|
||||
host.vm_memory_gb += memory_gb
|
||||
|
||||
if capacity.hosts and capacity.unplaced_vm_count:
|
||||
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):
|
||||
host.shared_memory_gb = unplaced_memory_gb * weight
|
||||
return capacity
|
||||
|
||||
|
||||
def host_capacity(cluster_capacity, host):
|
||||
"""A Capacity for one host of a cluster, including its share of the VMs without a fixed host."""
|
||||
return Capacity(
|
||||
hosts=[host],
|
||||
vm_count=host.vm_count,
|
||||
vcpus=host.total_vcpus,
|
||||
vm_memory_gb=host.total_vm_memory_gb,
|
||||
unplaced_vm_count=cluster_capacity.unplaced_vm_count,
|
||||
host_share=True,
|
||||
)
|
||||
@@ -49,7 +49,7 @@ class ClusterCapacityColumn(tables.Column):
|
||||
from .hardware_views import host_entry, vm_rows
|
||||
|
||||
hosts = Device.objects.filter(cluster=record).select_related("netbox_utilities_hardware__cpu")
|
||||
vms = VirtualMachine.objects.filter(cluster=record).select_related("device")
|
||||
vms = VirtualMachine.objects.filter(cluster=record)
|
||||
return build_capacity([host_entry(device) for device in hosts], vm_rows(vms))
|
||||
|
||||
def render(self, record):
|
||||
|
||||
@@ -14,9 +14,10 @@ def host_entry(device):
|
||||
"""hardware.Host for a device, with or without recorded CPU/RAM."""
|
||||
record = getattr(device, "netbox_utilities_hardware", None)
|
||||
if record is None:
|
||||
return hardware.Host(name=device.name)
|
||||
return hardware.Host(name=device.name, key=device.pk)
|
||||
return hardware.Host(
|
||||
name=device.name,
|
||||
key=device.pk,
|
||||
cores=record.total_cores,
|
||||
threads=record.total_threads,
|
||||
memory_gb=record.memory_gb,
|
||||
@@ -24,7 +25,7 @@ def host_entry(device):
|
||||
|
||||
|
||||
def vm_rows(vms):
|
||||
return [(vm.device.name if vm.device_id else None, vm.vcpus, vm.memory, vm.status) for vm in vms]
|
||||
return [(vm.device_id, vm.vcpus, vm.memory, vm.status) for vm in vms]
|
||||
|
||||
|
||||
def capacity_context(capacity):
|
||||
@@ -86,24 +87,38 @@ def device_hardware_context(request, device):
|
||||
.select_related("tenant")
|
||||
.order_by("name")
|
||||
)
|
||||
if vms or device.cluster_id:
|
||||
if device.cluster_id:
|
||||
# Includes this host's share of the cluster VMs that have no fixed host.
|
||||
cluster_capacity, _ = build_cluster_capacity(request, device.cluster_id, extra_host=device)
|
||||
host = next(host for host in cluster_capacity.hosts if host.key == device.pk)
|
||||
capacity = hardware.host_capacity(cluster_capacity, host)
|
||||
elif vms:
|
||||
capacity = hardware.build_capacity([host_entry(device)], vm_rows(vms))
|
||||
host = capacity.hosts[0]
|
||||
if device.cluster_id or vms:
|
||||
context.update(capacity_context(capacity))
|
||||
context["host"] = capacity.hosts[0]
|
||||
context["host"] = host
|
||||
context["vms"] = vms
|
||||
context["vm_list_url"] = f"{reverse('virtualization:virtualmachine_list')}?device_id={device.pk}"
|
||||
return context
|
||||
|
||||
|
||||
def cluster_capacity_context(request, cluster):
|
||||
def build_cluster_capacity(request, cluster_id, extra_host=None):
|
||||
"""Capacity of a cluster's hosts and VMs; returns (capacity, host devices in the same order)."""
|
||||
hosts = list(
|
||||
Device.objects.restrict(request.user, "view")
|
||||
.filter(cluster=cluster)
|
||||
.filter(cluster_id=cluster_id)
|
||||
.select_related("netbox_utilities_hardware__cpu")
|
||||
.order_by("name")
|
||||
)
|
||||
vms = VirtualMachine.objects.restrict(request.user, "view").filter(cluster=cluster).select_related("device")
|
||||
capacity = hardware.build_capacity([host_entry(device) for device in hosts], vm_rows(vms))
|
||||
if extra_host is not None and all(host.pk != extra_host.pk for host in hosts):
|
||||
hosts.append(extra_host)
|
||||
vms = VirtualMachine.objects.restrict(request.user, "view").filter(cluster_id=cluster_id)
|
||||
return hardware.build_capacity([host_entry(device) for device in hosts], vm_rows(vms)), hosts
|
||||
|
||||
|
||||
def cluster_capacity_context(request, cluster):
|
||||
capacity, hosts = build_cluster_capacity(request, cluster.pk)
|
||||
rows = []
|
||||
for device, host in zip(hosts, capacity.hosts, strict=True):
|
||||
record = getattr(device, "netbox_utilities_hardware", None)
|
||||
@@ -113,9 +128,9 @@ def cluster_capacity_context(request, cluster):
|
||||
"record": record,
|
||||
"host": host,
|
||||
"memory": hardware.format_memory(host.memory_gb),
|
||||
"vm_memory": hardware.format_memory(round(host.vm_memory_gb, 1)),
|
||||
"memory_percent": hardware.percent(host.vm_memory_gb, host.memory_gb),
|
||||
"vcpu_per_core": hardware.format_ratio(host.vcpus / host.cores if host.cores else None),
|
||||
"vm_memory": hardware.format_memory(round(host.total_vm_memory_gb, 1)),
|
||||
"memory_percent": hardware.percent(host.total_vm_memory_gb, host.memory_gb),
|
||||
"vcpu_per_core": hardware.format_ratio(host.total_vcpus / host.cores if host.cores else None),
|
||||
}
|
||||
)
|
||||
return {
|
||||
|
||||
@@ -27,7 +27,10 @@
|
||||
</td>
|
||||
<td>{% if row.host.cores %}{{ row.host.cores }} / {{ row.host.threads }}{% else %}—{% endif %}</td>
|
||||
<td>{{ row.memory }}</td>
|
||||
<td>{{ row.host.vm_count }} <span class="text-muted small">({{ row.host.vcpus|floatformat:"-1" }} vCPU, {{ row.vm_memory }})</span></td>
|
||||
<td>
|
||||
{{ row.host.vm_count }}{% if row.host.shared_vcpus or row.host.shared_memory_gb %} <span class="text-muted small">+ anteilig</span>{% endif %}
|
||||
<div class="text-muted small">{{ row.host.total_vcpus|floatformat:"-1" }} vCPU, {{ row.vm_memory }}</div>
|
||||
</td>
|
||||
<td>{{ row.vcpu_per_core }}</td>
|
||||
<td>
|
||||
{% if row.memory_percent is not None %}
|
||||
@@ -42,7 +45,8 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer small text-muted">
|
||||
Gezählt werden alle VMs außer „Offline“ und „Außer Betrieb“. VMs ohne festen Host zählen nur zur Cluster-Summe.
|
||||
Gezählt werden alle VMs außer „Offline“ und „Außer Betrieb“. VMs ohne festen Host werden anteilig nach Größe
|
||||
auf die Hosts verteilt: vCPUs nach Threads, RAM nach Host-RAM. Ist bei keinem Host CPU/RAM erfasst, zu gleichen Teilen.
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
@@ -64,6 +64,12 @@
|
||||
{% else %}
|
||||
<div class="card-body text-muted">Keine VMs mit diesem Gerät als Host.</div>
|
||||
{% endif %}
|
||||
{% if capacity.host_share and capacity.unplaced_vm_count %}
|
||||
<div class="card-footer small text-muted">
|
||||
Die Liste zeigt nur VMs, deren Gerät dieser Host ist. {{ capacity.unplaced_vm_count }} weitere VM{{ capacity.unplaced_vm_count|pluralize:",s" }}
|
||||
des Clusters ohne festen Host {{ capacity.unplaced_vm_count|pluralize:"ist,sind" }} anteilig in der Auslastung enthalten.
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock content %}
|
||||
@@ -14,7 +14,13 @@
|
||||
<th scope="row">VMs</th>
|
||||
<td>
|
||||
{{ capacity.vm_count }} VM{{ capacity.vm_count|pluralize:",s" }} · {{ capacity.vcpus|floatformat:"-1" }} vCPUs · {{ vm_memory }} RAM
|
||||
{% if capacity.unplaced_vm_count %}<div class="small text-muted">davon {{ capacity.unplaced_vm_count }} ohne festen Host</div>{% endif %}
|
||||
{% if capacity.host_share and capacity.unplaced_vm_count %}
|
||||
<div class="small text-muted">
|
||||
inkl. anteiliger Last aus {{ capacity.unplaced_vm_count }} Cluster-VM{{ capacity.unplaced_vm_count|pluralize:",s" }} ohne festen Host
|
||||
</div>
|
||||
{% elif capacity.unplaced_vm_count %}
|
||||
<div class="small text-muted">davon {{ capacity.unplaced_vm_count }} ohne festen Host, anteilig nach Größe auf die Hosts verteilt</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
||||
@@ -116,3 +116,46 @@ class CapacityTest(SimpleTestCase):
|
||||
capacity = hardware.build_capacity(self.hosts()[:1], [])
|
||||
self.assertIsNone(capacity.failover_memory_gb)
|
||||
self.assertEqual(capacity.vcpu_per_core, 0)
|
||||
|
||||
|
||||
class UnplacedVMTest(SimpleTestCase):
|
||||
def test_spread_by_host_size(self):
|
||||
hosts = [
|
||||
hardware.Host("small", cores=16, threads=32, memory_gb=256, key=1),
|
||||
hardware.Host("big", cores=48, threads=96, memory_gb=768, key=2),
|
||||
]
|
||||
vms = [(None, 16, 100 * 1024, "active"), (1, 4, 8 * 1024, "active")]
|
||||
capacity = hardware.build_capacity(hosts, vms)
|
||||
small, big = capacity.hosts
|
||||
self.assertEqual(capacity.unplaced_vm_count, 1)
|
||||
self.assertAlmostEqual(small.shared_vcpus, 4)
|
||||
self.assertAlmostEqual(big.shared_vcpus, 12)
|
||||
self.assertAlmostEqual(small.shared_memory_gb, 25)
|
||||
self.assertAlmostEqual(big.shared_memory_gb, 75)
|
||||
self.assertAlmostEqual(small.total_vcpus, 8)
|
||||
self.assertAlmostEqual(small.total_vm_memory_gb, 33)
|
||||
# The cluster sums count every VM exactly once.
|
||||
self.assertEqual((capacity.vcpus, capacity.vm_memory_gb), (20, 108))
|
||||
|
||||
def test_equal_shares_without_recorded_hardware(self):
|
||||
hosts = [hardware.Host("a", key=1), hardware.Host("b", key=2)]
|
||||
capacity = hardware.build_capacity(hosts, [(None, 8, 16 * 1024, "active")])
|
||||
self.assertEqual([host.shared_vcpus for host in capacity.hosts], [4, 4])
|
||||
self.assertEqual([host.shared_memory_gb for host in capacity.hosts], [8, 8])
|
||||
|
||||
def test_host_capacity_includes_share(self):
|
||||
hosts = [
|
||||
hardware.Host("a", cores=32, threads=64, memory_gb=512, key=1),
|
||||
hardware.Host("b", cores=32, threads=64, memory_gb=512, key=2),
|
||||
]
|
||||
cluster = hardware.build_capacity(hosts, [(None, 32, 512 * 1024, "active"), (1, 8, 64 * 1024, "active")])
|
||||
single = hardware.host_capacity(cluster, cluster.hosts[0])
|
||||
self.assertTrue(single.host_share)
|
||||
self.assertEqual((single.vm_count, single.vcpus, single.vm_memory_gb), (1, 24, 320))
|
||||
self.assertEqual(single.memory_percent, 62)
|
||||
self.assertEqual(single.vcpu_per_core, 0.75)
|
||||
|
||||
def test_hosts_are_matched_by_key_not_name(self):
|
||||
hosts = [hardware.Host("esx", key=1), hardware.Host("esx", key=2)]
|
||||
capacity = hardware.build_capacity(hosts, [(2, 4, 1024, "active")])
|
||||
self.assertEqual([host.vcpus for host in capacity.hosts], [0, 4])
|
||||
Reference in new issue
Block a user