mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
feat(instances): the Guest Info tab shows how full the guest's file systems are
A disk filling up inside a guest was seen only from the guest. The Guest Info tab now lists, from the guest agent, each mounted file system with its type and used / total size, a bar turning amber from 85 %. File systems the agent gives no size of (pseudo ones, Windows' System Reserved) are left out; an agent without the command still shows the rest of the tab.
This commit is contained in:
parent
981ff7b294
commit
46931bdbdb
10 changed files with 544 additions and 401 deletions
|
|
@ -18,4 +18,16 @@
|
|||
<dt class="col-3">{% trans "Timezone / Offset" %}</dt>
|
||||
<dd class="col-9" id="ostimezone"></dd>
|
||||
</dl>
|
||||
{# instance_page.js fills and shows it when the agent gives the file systems #}
|
||||
<table class="table table-sm align-middle mx-3" id="osfilesystems" hidden>
|
||||
<caption class="caption-top">{% trans "File systems" %}</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">{% trans "Mount point" %}</th>
|
||||
<th scope="col">{% trans "Type" %}</th>
|
||||
<th scope="col">{% trans "Used" %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
|
|
|||
42
instances/tests/test_guest_info.py
Normal file
42
instances/tests/test_guest_info.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
"""The Guest Info tab's reading: the guest's OS and its file systems, their
|
||||
sizes in words for the page."""
|
||||
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.template.loader import render_to_string
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from lxml import html
|
||||
|
||||
from computes.models import Compute
|
||||
from instances.models import Instance
|
||||
|
||||
|
||||
class GuestInfoTestCase(TestCase):
|
||||
def setUp(self):
|
||||
compute = Compute.objects.create(name="gi", hostname="192.0.2.41", login="root", password="", type=1)
|
||||
self.instance = Instance.objects.create(compute=compute, name="gi-vm", uuid="00000000-0000-4000-8000-0000000000a1")
|
||||
self.client.force_login(get_user_model().objects.create_superuser("gi-admin", "gi@example.com", "pw"))
|
||||
patcher = patch.object(Instance, "proxy", new_callable=PropertyMock)
|
||||
self.proxy = patcher.start().return_value
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_each_file_system_is_told_in_words(self):
|
||||
self.proxy.osinfo.return_value = {"id": "rocky", "filesystems": [
|
||||
{"mountpoint": "/", "type": "xfs", "used": 9 * 2**30, "total": 10 * 2**30}]}
|
||||
data = self.client.get(reverse("instances:osinfo", args=[self.instance.pk])).json()
|
||||
self.assertEqual(data["filesystems"], [{"mountpoint": "/", "type": "xfs", "used": 9 * 2**30, "total": 10 * 2**30,
|
||||
"used_text": "9.0\xa0GB", "total_text": "10.0\xa0GB", "percent": 90}])
|
||||
|
||||
def test_a_file_system_used_past_its_total_is_full(self):
|
||||
# a BSD guest's total leaves out the space reserved for root, which used counts
|
||||
self.proxy.osinfo.return_value = {"filesystems": [{"mountpoint": "/", "type": "ufs", "used": 104, "total": 100}]}
|
||||
data = self.client.get(reverse("instances:osinfo", args=[self.instance.pk])).json()
|
||||
self.assertEqual(data["filesystems"][0]["percent"], 100)
|
||||
|
||||
def test_the_tab_has_a_hidden_table_for_them(self):
|
||||
# filled and shown by instance_page.js when the agent gives some
|
||||
table = html.fromstring(render_to_string("instances/info_tab.html")).xpath("//table[@id='osfilesystems']")[0]
|
||||
self.assertIsNotNone(table.get("hidden"))
|
||||
self.assertEqual(table.xpath(".//th/text()"), ["Mount point", "Type", "Used"])
|
||||
|
|
@ -549,7 +549,11 @@ def stats(request, pk):
|
|||
def osinfo(request, pk):
|
||||
instance = get_instance(request.user, pk)
|
||||
try:
|
||||
return JsonResponse(instance.proxy.osinfo())
|
||||
info = instance.proxy.osinfo()
|
||||
for fs in info.get("filesystems", []): # sizes in the page's words
|
||||
fs.update(used_text=filesizeformat(fs["used"]), total_text=filesizeformat(fs["total"]),
|
||||
percent=min(100, fs["used"] * 100 // fs["total"])) # used may pass a BSD guest's total
|
||||
return JsonResponse(info)
|
||||
except libvirtError as err:
|
||||
return JsonResponse({"error": error_text(request, err)}, status=400)
|
||||
|
||||
|
|
|
|||
Binary file not shown.
File diff suppressed because it is too large
Load diff
Binary file not shown.
File diff suppressed because it is too large
Load diff
|
|
@ -33,11 +33,40 @@
|
|||
$('#oskernelversion').text(data['kernel-version'] || '');
|
||||
$('#osversion').text(data.version || '');
|
||||
$('#ostimezone').text(data.zone ? data.zone + " / " + data.offset : '');
|
||||
show_filesystems(data.filesystems || []);
|
||||
}).fail(function (xhr) {
|
||||
var message = xhr.responseJSON && xhr.responseJSON.error;
|
||||
error.text(message || silent).removeClass('d-none');
|
||||
show_filesystems([]);
|
||||
});
|
||||
}
|
||||
// each file system: its mount point, type, and how full it is (a bar, amber from 85 %)
|
||||
function show_filesystems(filesystems) {
|
||||
var table = document.getElementById('osfilesystems');
|
||||
var body = table.tBodies[0];
|
||||
body.replaceChildren();
|
||||
filesystems.forEach(function (fs) {
|
||||
var row = body.insertRow();
|
||||
row.insertCell().textContent = fs.mountpoint;
|
||||
row.insertCell().textContent = fs.type;
|
||||
var used = row.insertCell();
|
||||
used.textContent = fs.used_text + " / " + fs.total_text;
|
||||
var bar = document.createElement('div');
|
||||
bar.className = 'progress mt-1';
|
||||
bar.style.setProperty('--bs-progress-height', '0.3rem');
|
||||
bar.setAttribute('role', 'progressbar');
|
||||
bar.setAttribute('aria-valuenow', fs.percent);
|
||||
bar.setAttribute('aria-valuemin', 0);
|
||||
bar.setAttribute('aria-valuemax', 100);
|
||||
bar.setAttribute('aria-label', fs.mountpoint);
|
||||
var fill = document.createElement('div');
|
||||
fill.className = 'progress-bar' + (fs.percent >= 85 ? ' bg-warning' : '');
|
||||
fill.style.width = fs.percent + '%';
|
||||
bar.appendChild(fill);
|
||||
used.appendChild(bar);
|
||||
});
|
||||
table.hidden = filesystems.length === 0;
|
||||
}
|
||||
function get_osinfo() {
|
||||
show_tab('#settings');
|
||||
show_tab('#osinfo');
|
||||
|
|
|
|||
|
|
@ -784,8 +784,21 @@ class wvmInstance(wvmConnect):
|
|||
for command in ("guest-get-timezone", "guest-get-host-name"):
|
||||
with contextlib.suppress(libvirtError):
|
||||
info.update(agent_command(self.instance, command))
|
||||
with contextlib.suppress(libvirtError):
|
||||
info["filesystems"] = self.guest_filesystems()
|
||||
return info
|
||||
|
||||
def guest_filesystems(self):
|
||||
"""The guest's mounted file systems the agent gives both sizes of (not
|
||||
pseudo ones), by mount point: [{"mountpoint", "type", "used", "total"}], bytes."""
|
||||
filesystems = []
|
||||
for fs in agent_command(self.instance, "guest-get-fsinfo") or []:
|
||||
used, total = (fs or {}).get("used-bytes"), (fs or {}).get("total-bytes")
|
||||
if isinstance(used, int) and isinstance(total, int) and total > 0:
|
||||
filesystems.append({"mountpoint": str(fs.get("mountpoint", "")), "type": str(fs.get("type", "")),
|
||||
"used": used, "total": total})
|
||||
return sorted(filesystems, key=lambda fs: fs["mountpoint"])
|
||||
|
||||
def start(self, flags=0):
|
||||
self.instance.createWithFlags(flags)
|
||||
|
||||
|
|
|
|||
|
|
@ -68,13 +68,14 @@ class InterfaceAddressesTestCase(unittest.TestCase):
|
|||
class OsInfoTestCase(unittest.TestCase):
|
||||
def test_the_os_details_are_asked_with_a_timeout(self):
|
||||
answers = {"guest-get-osinfo": {"kernel-release": "6.12"}, "guest-get-timezone": {"zone": "UTC"},
|
||||
"guest-get-host-name": {"host-name": "web"}}
|
||||
"guest-get-host-name": {"host-name": "web"}, "guest-get-fsinfo": []}
|
||||
inst = wvmInstance.__new__(wvmInstance)
|
||||
inst.instance = MagicMock()
|
||||
with patch.object(module, "qemuAgentCommand",
|
||||
side_effect=lambda dom, cmd, timeout, flags: json.dumps(
|
||||
{"return": answers[json.loads(cmd)["execute"]]})) as agent:
|
||||
self.assertEqual(inst.osinfo(), {"kernel-release": "6.12", "zone": "UTC", "host-name": "web"})
|
||||
self.assertEqual(inst.osinfo(), {"kernel-release": "6.12", "zone": "UTC", "host-name": "web",
|
||||
"filesystems": []})
|
||||
self.assertEqual({c.args[2] for c in agent.call_args_list}, {10})
|
||||
|
||||
def test_a_command_the_agent_lacks_leaves_out_only_its_details(self):
|
||||
|
|
@ -82,14 +83,30 @@ class OsInfoTestCase(unittest.TestCase):
|
|||
inst.instance = MagicMock()
|
||||
|
||||
def agent(dom, cmd, timeout, flags):
|
||||
if json.loads(cmd)["execute"] == "guest-get-timezone":
|
||||
raise libvirtError("The command guest-get-timezone has not been found")
|
||||
if json.loads(cmd)["execute"] in ("guest-get-timezone", "guest-get-fsinfo"):
|
||||
raise libvirtError("The command has not been found")
|
||||
return json.dumps({"return": {"guest-get-osinfo": {"id": "alma"}, "guest-get-host-name": {"host-name": "web"}}[
|
||||
json.loads(cmd)["execute"]]})
|
||||
|
||||
with patch.object(module, "qemuAgentCommand", side_effect=agent):
|
||||
self.assertEqual(inst.osinfo(), {"id": "alma", "host-name": "web"})
|
||||
|
||||
def test_the_file_systems_with_a_size_by_mount_point(self):
|
||||
fsinfo = [
|
||||
{"name": "vda2", "mountpoint": "/boot", "type": "xfs", "used-bytes": 2**28, "total-bytes": 2**30},
|
||||
{"name": "vda1", "mountpoint": "/var/lib/containers/overlay", "type": "overlay"}, # no size: left out
|
||||
{"name": "vdb1", "mountpoint": "/data", "type": "xfs", "total-bytes": 2**30}, # no used size: left out
|
||||
None,
|
||||
{"name": "vda1", "mountpoint": "/", "type": "xfs", "used-bytes": 3 * 2**30, "total-bytes": 10 * 2**30},
|
||||
]
|
||||
inst = wvmInstance.__new__(wvmInstance)
|
||||
inst.instance = MagicMock()
|
||||
with patch.object(module, "qemuAgentCommand", return_value=json.dumps({"return": fsinfo})):
|
||||
self.assertEqual(inst.guest_filesystems(), [
|
||||
{"mountpoint": "/", "type": "xfs", "used": 3 * 2**30, "total": 10 * 2**30},
|
||||
{"mountpoint": "/boot", "type": "xfs", "used": 2**28, "total": 2**30},
|
||||
])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue