1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-09 11:46:49 +00:00

feat(computes): the host overview shows its VMs, capacity and load, also when it cannot be reached

A host the panel could not reach made its overview the 500 page,
without the host's sections, and the Computes list gave such a host an
Overview button that led nowhere. The page now keeps the host's name,
sections, "Edit connection" and "Remove from panel" (the existing
confirmation), and says "Not connected" with the reason and a "Try
again". Details that cannot be read are said in an alert; the VMs,
capacity and load still show, as they are read on their own.

A reachable host's page leads with what it runs and what it gave:
- its VMs by state (running, shut off, paused, other), linked to
  Instances;
- the vCPUs, memory and storage pools given, as on the Computes list,
  which now shares those parts (computes/capacity_*.html) and their
  preparation (views.shown_capacity) with it;
- CPU and memory in use, side by side, with the current reading in
  words, a note on what the charts hold and when they were last
  updated, or since when not; memory in GB like the rest of the page;
- Hardware and Software and connection: logical CPUs and clock from
  virNodeGetInfo, but physical cores, sockets and NUMA nodes counted
  from the CPUs of the capabilities' NUMA cells, as virNodeGetInfo
  makes a topology up for hosts it cannot describe (asymmetric NUMA);
  the hypervisor's own name and version, libvirt, emulator, the guest
  architectures as text instead of menus of dead links. A host without
  sysinfo shows no processor instead of failing.
Empty notes no longer show "None"; labels stack over their values on a
phone instead of running into them.

Pools that cannot be read no longer drop a host's vCPUs and memory from
its capacity: the storage part says so. get_capacity also counts the
VMs by state.
This commit is contained in:
catborise 2026-10-27 14:26:03 +03:00
parent 86c708ed68
commit 1b99d2eac5
15 changed files with 487 additions and 201 deletions

View file

@ -1,11 +1,26 @@
import time
from libvirt import VIR_DOMAIN_SHUTOFF, VIR_NODE_CPU_STATS_ALL_CPUS
from libvirt import (
libvirtError,
VIR_DOMAIN_BLOCKED,
VIR_DOMAIN_PAUSED,
VIR_DOMAIN_RUNNING,
VIR_DOMAIN_SHUTOFF,
VIR_NODE_CPU_STATS_ALL_CPUS,
)
from vrtManager.connection import wvmConnect
from vrtManager.util import get_xml_path
# a VM's state as the host's VM counts group it; others count as "other"
VM_STATES = {
VIR_DOMAIN_RUNNING: "running",
VIR_DOMAIN_BLOCKED: "running", # running, waiting on a resource
VIR_DOMAIN_PAUSED: "paused",
VIR_DOMAIN_SHUTOFF: "shutoff",
}
# the times of virNodeGetCPUStats that add up to the elapsed CPU time; guest is
# already part of user and utilization is a percentage
CPU_TIMES = ("kernel", "user", "idle", "iowait")
@ -78,23 +93,47 @@ class wvmHostDetails(wvmConnect):
info.append(self.host_info[0]) # architecture
info.append(self.host_info[1] * 1048576) # memory
info.append(self.host_info[2]) # cpu core count
info.append(
get_xml_path(self.wvm.getSysinfo(0), func=cpu_version)
) # cpu version
try:
info.append(get_xml_path(self.wvm.getSysinfo(0), func=cpu_version)) # cpu version
except libvirtError: # not every driver or host gives its sysinfo
info.append(None)
info.append(self.wvm.getURI()) # uri
return info
def get_capacity(self):
"""
What the host has given its VMs: vCPUs and memory (bytes) of all VMs
and of the running ones, against its CPUs and memory. A shut-off VM
takes its share again when it starts.
and of the running ones (paused ones too: they keep theirs), against
its CPUs and memory. A shut-off VM takes its share again when it
starts. Also its VMs by state: running, paused, shut off and other
(crashed, suspended, shutting down).
"""
vcpus = {"all": 0, "running": 0}
assigned = {"all": 0, "running": 0}
vms = {"running": 0, "paused": 0, "shutoff": 0, "other": 0}
for vm in self.get_host_instances(raw_mem_size=True).values():
for share in ("all",) if vm["status"] == VIR_DOMAIN_SHUTOFF else ("all", "running"):
vcpus[share] += int(vm["vcpu"])
assigned[share] += vm["memory"]
vms[VM_STATES.get(vm["status"], "other")] += 1
return {"cpus": self.host_info[2], "memory": self.host_info[1] * 1048576,
"vcpus": vcpus, "assigned": assigned}
"vcpus": vcpus, "assigned": assigned, "vms": vms}
def get_cpu_topology(self):
"""
The host's logical CPUs and MHz, and from the CPUs of its NUMA cells
in its capabilities, the NUMA nodes, sockets and physical cores (None
when it does not say). Not virNodeGetInfo's topology: it makes one up
for hosts it cannot describe, such as asymmetric NUMA.
"""
def topology(doc):
cpus = doc.xpath("/capabilities/host/topology/cells/cell/cpus/cpu[@socket_id]")
return {
"nodes": len(doc.xpath("/capabilities/host/topology/cells/cell")) or None,
"sockets": len({cpu.get("socket_id") for cpu in cpus}) or None,
# a core is one within its socket and die
"cores": len({(cpu.get("socket_id"), cpu.get("die_id"), cpu.get("core_id")) for cpu in cpus}) or None,
}
return {**get_xml_path(self.get_cap_xml(), func=topology), "cpus": self.host_info[2], "mhz": self.host_info[3]}

View file

@ -158,10 +158,35 @@ class CapacityTestCase(unittest.TestCase):
"cpus": 4, "memory": 2 * gib,
"vcpus": {"all": 14, "running": 6},
"assigned": {"all": 14 * gib, "running": 6 * gib},
"vms": {"running": 1, "paused": 1, "shutoff": 1, "other": 0},
})
conn.get_host_instances.assert_called_once_with(raw_mem_size=True)
class CpuTopologyTestCase(unittest.TestCase):
"""The topology comes from the capabilities' NUMA cells: getInfo makes
one up (one node, one socket, every CPU a core) for hosts it cannot
describe."""
def topology(self, cells):
conn = connection(wvmHostDetails)
conn.__dict__["host_info"] = ["x86_64", 65536, 16, 2400, 1, 1, 16, 1] # getInfo's made-up one
conn.get_cap_xml = lambda: f"<capabilities><host><topology><cells num='2'>{cells}</cells></topology></host></capabilities>"
return conn.get_cpu_topology()
def test_sockets_and_cores_are_counted_from_the_cpus_of_the_numa_cells(self):
# two nodes, two sockets, four cores of two threads each
cells = "".join(
f"<cell id='{node}'><cpus num='4'>"
+ "".join(f"<cpu id='{node * 4 + i}' socket_id='{node}' die_id='0' core_id='{i // 2}' siblings=''/>" for i in range(4))
+ "</cpus></cell>" for node in range(2))
self.assertEqual(self.topology(cells), {"nodes": 2, "sockets": 2, "cores": 4, "cpus": 16, "mhz": 2400})
def test_a_host_that_does_not_say_has_no_sockets_or_cores(self):
cells = "<cell id='0'><cpus num='2'><cpu id='0'/><cpu id='1'/></cpus></cell>"
self.assertEqual(self.topology(cells), {"nodes": 1, "sockets": None, "cores": None, "cpus": 16, "mhz": 2400})
class StoragesInfoTestCase(unittest.TestCase):
def test_each_pool_tells_its_free_space(self):
conn = connection(wvmStorages)