feat: CPU & RAM as a device tab next to Disks

The CPU & RAM panel on the device page becomes its own tab with the
total core count as badge. Besides CPU and RAM it shows the capacity of
a virtualization host and the list of VMs running on it. Saving the
form returns to the tab.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-29 11:00:30 +01:00
1 parent 8624ddb62f
commit 675574ee00
6 files changed
+124 -52

No files matched your search

+3
View File
@@ -70,6 +70,9 @@ def install_hardware_support():
from utilities.tables import register_table_column
from virtualization.tables import ClusterTable
# Registers the "CPU & RAM" device tab before NetBox builds the dcim URLs.
from . import hardware_views # noqa: F401
common = {"accessor": tables.A("pk"), "orderable": False, "empty_values": ()}
register_table_column(DeviceCPUColumn(verbose_name="CPU", **common), "netbox_utilities_cpu", DeviceTable)
register_table_column(
+43 -5
View File
@@ -2,11 +2,12 @@ from dcim.models import Device
from django.db.models import Count
from django.urls import reverse
from netbox.views import generic
from utilities.views import register_model_view
from utilities.views import ViewTab, register_model_view
from virtualization.models import VirtualMachine
from . import hardware, hardware_filtersets, hardware_forms, hardware_tables
from .models import CPUModel, DeviceHardware
from .runtime import hardware_enabled
def host_entry(device):
@@ -38,21 +39,58 @@ def capacity_context(capacity):
}
def hardware_tab_url(device):
return reverse("dcim:device_hardware", kwargs={"pk": device.pk})
def hardware_tab_badge(device):
"""Total cores of the device; "—" without CPU; None hides the tab for other devices."""
if not hardware_enabled():
return None
record = DeviceHardware.objects.filter(device=device).select_related("cpu").first()
if record is None:
return "—" if hardware.supports_hardware(device) else None
return str(record.total_cores) if record.cpu_id else "—"
@register_model_view(Device, "hardware", path="hardware")
class DeviceHardwareTabView(generic.ObjectView):
queryset = Device.objects.all()
template_name = "netbox_utilities/device_hardware.html"
tab = ViewTab(
label="CPU & RAM",
badge=hardware_tab_badge,
permission="netbox_utilities.view_devicehardware",
weight=2060,
hide_if_empty=True,
)
def get_extra_context(self, request, instance):
return device_hardware_context(request, instance)
def device_hardware_context(request, device):
"""Panel on the device page: CPU/RAM and, for virtualization hosts, the VMs running on it."""
"""The "CPU & RAM" tab: CPU/RAM and, for virtualization hosts, the VMs running on it."""
record = DeviceHardware.objects.filter(device=device).select_related("cpu").first()
context = {
"record": record,
"supported": hardware.supports_hardware(device),
"can_add": request.user.has_perm("netbox_utilities.add_devicehardware"),
"can_change": request.user.has_perm("netbox_utilities.change_devicehardware"),
"add_url": f"{reverse('plugins:netbox_utilities:devicehardware_add')}?device={device.pk}"
f"&return_url={device.get_absolute_url()}",
f"&return_url={hardware_tab_url(device)}",
}
vms = list(VirtualMachine.objects.restrict(request.user, "view").filter(device=device))
vms = list(
VirtualMachine.objects.restrict(request.user, "view")
.filter(device=device)
.select_related("tenant")
.order_by("name")
)
if vms or device.cluster_id:
capacity = hardware.build_capacity([host_entry(device)], vm_rows(vms))
context.update(capacity_context(capacity))
context["host"] = capacity.hosts[0]
context["vms"] = vms
context["vm_list_url"] = f"{reverse('virtualization:virtualmachine_list')}?device_id={device.pk}"
return context
@@ -190,7 +228,7 @@ class DeviceHardwareEditView(generic.ObjectEditView):
def get_return_url(self, request, obj=None):
if obj is not None and obj.pk and obj.device_id and "return_url" not in request.GET:
return obj.device.get_absolute_url()
return hardware_tab_url(obj.device)
return super().get_return_url(request, obj)
-15
View File
@@ -111,9 +111,6 @@ class DeviceUtilitiesContent(PluginTemplateExtension):
return self.render("netbox_utilities/device_bulk_module_button.html")
def right_page(self):
return self._hardware_panel() + self._rack_width_panel()
def _rack_width_panel(self):
placement = DeviceRackPlacement.objects.filter(device=self.context["object"]).first()
if placement is None:
return ""
@@ -125,18 +122,6 @@ class DeviceUtilitiesContent(PluginTemplateExtension):
},
)
def _hardware_panel(self):
request, device = self.context["request"], self.context["object"]
if not hardware_enabled() or not request.user.has_perm("netbox_utilities.view_devicehardware"):
return ""
from . import hardware
from .hardware_views import device_hardware_context
context = device_hardware_context(request, device)
if context["record"] is None and not hardware.supports_hardware(device):
return ""
return self.render("netbox_utilities/device_hardware_panel.html", context)
class ClusterHardwareContent(PluginTemplateExtension):
models = ["virtualization.cluster"]
@@ -0,0 +1,69 @@
{% extends 'dcim/device/base.html' %}
{% block content %}
{% if not supported and not record %}
<div class="alert alert-info">
CPU &amp; RAM werden für Geräte mit den konfigurierten Rollen (z. B. Server) und für Cluster-Hosts gepflegt.
</div>
{% endif %}
<div class="row mb-3">
<div class="col col-12 col-xl-6">
<div class="card">
<h2 class="card-header d-flex align-items-center">
<span class="me-auto"><i class="mdi mdi-chip me-1" aria-hidden="true"></i>CPU &amp; RAM</span>
{% if record and can_change %}
<a href="{% url 'plugins:netbox_utilities:devicehardware_edit' pk=record.pk %}?return_url={{ request.path|urlencode }}" class="btn btn-sm btn-outline-secondary">
<i class="mdi mdi-pencil" aria-hidden="true"></i> Bearbeiten
</a>
{% elif not record and can_add %}
<a href="{{ add_url }}" class="btn btn-sm btn-primary"><i class="mdi mdi-plus-thick" aria-hidden="true"></i> Erfassen</a>
{% endif %}
</h2>
{% if record %}
{% include 'netbox_utilities/inc/hardware_record.html' %}
{% else %}
<div class="card-body text-muted">Noch keine CPU und kein RAM erfasst.</div>
{% endif %}
</div>
</div>
<div class="col col-12 col-xl-6">
{% if capacity %}
<div class="card">
<h2 class="card-header d-flex align-items-center">
<span class="me-auto"><i class="mdi mdi-server-network me-1" aria-hidden="true"></i>Virtualisierung</span>
{% if object.cluster %}<a href="{{ object.cluster.get_absolute_url }}" class="btn btn-sm btn-outline-secondary">Cluster {{ object.cluster }}</a>{% endif %}
</h2>
{% include 'netbox_utilities/inc/hardware_capacity.html' %}
</div>
{% endif %}
</div>
</div>
{% if capacity %}
<div class="card">
<h2 class="card-header d-flex align-items-center">
<span class="me-auto">VMs auf diesem Host <span class="badge text-bg-secondary">{{ vms|length }}</span></span>
<a href="{{ vm_list_url }}" class="btn btn-sm btn-outline-secondary">In der VM-Liste öffnen</a>
</h2>
{% if vms %}
<div class="table-responsive">
<table class="table table-hover table-sm align-middle mb-0">
<thead><tr><th>Name</th><th>Status</th><th>vCPUs</th><th>RAM</th><th>Mandant</th></tr></thead>
<tbody>
{% for vm in vms %}
<tr>
<td><a href="{{ vm.get_absolute_url }}">{{ vm }}</a></td>
<td><span class="badge text-bg-{{ vm.get_status_color }}">{{ vm.get_status_display }}</span></td>
<td>{{ vm.vcpus|floatformat:"-1"|default:"—" }}</td>
<td>{% if vm.memory %}{{ vm.memory }} MB{% else %}—{% endif %}</td>
<td>{% if vm.tenant %}<a href="{{ vm.tenant.get_absolute_url }}">{{ vm.tenant }}</a>{% else %}—{% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="card-body text-muted">Keine VMs mit diesem Gerät als Host.</div>
{% endif %}
</div>
{% endif %}
{% endblock content %}
@@ -1,24 +0,0 @@
<div class="card">
<h2 class="card-header d-flex align-items-center">
<span class="me-auto"><i class="mdi mdi-chip me-1" aria-hidden="true"></i>CPU &amp; RAM</span>
{% if record and can_change %}
<a href="{% url 'plugins:netbox_utilities:devicehardware_edit' pk=record.pk %}?return_url={{ request.path|urlencode }}" class="btn btn-sm btn-outline-secondary">
<i class="mdi mdi-pencil" aria-hidden="true"></i> Bearbeiten
</a>
{% elif not record and can_add %}
<a href="{{ add_url }}" class="btn btn-sm btn-primary"><i class="mdi mdi-plus-thick" aria-hidden="true"></i> Erfassen</a>
{% endif %}
</h2>
{% if record %}
{% include 'netbox_utilities/inc/hardware_record.html' %}
{% else %}
<div class="card-body text-muted">Noch keine CPU und kein RAM erfasst.</div>
{% endif %}
{% if capacity %}
<h3 class="card-header border-top h5 d-flex align-items-center">
<span class="me-auto">Virtualisierung</span>
<a href="{{ vm_list_url }}" class="btn btn-sm btn-outline-secondary">VMs anzeigen</a>
</h3>
{% include 'netbox_utilities/inc/hardware_capacity.html' %}
{% endif %}
</div>