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

+9 -8
View File
@@ -219,8 +219,8 @@ sudo systemctl restart netbox netbox-rq
| `disk_device_roles` | `["Server", "NAS", "SAN", "MSA"]` | Device role names (case-insensitive) that get the **Disks** tab |
| `disk_age_warning_years` | `5` | Warn about disks older than this (manufacture date, otherwise install date) |
| `disk_warranty_warning_days` | `90` | Warn this many days before the warranty ends |
| `hardware_enabled` | `True` | **CPU & RAM** panels, catalogue, columns and cluster capacity |
| `hardware_device_roles` | `["Server"]` | Device roles (case-insensitive) that show the **CPU & RAM** panel; cluster hosts always show it |
| `hardware_enabled` | `True` | **CPU & RAM** tab, catalogue, columns and cluster capacity |
| `hardware_device_roles` | `["Server"]` | Device roles (case-insensitive) that show the **CPU & RAM** tab; cluster hosts always show it |
| `hardware_vcpu_ratio_warning` | `4` | Warn when a host or cluster has more vCPUs per physical core |
## Usage
@@ -410,10 +410,11 @@ RAID groups `netbox_utilities.add_raidgroup` (otherwise they are skipped).
### CPU & RAM
Servers and virtualization hosts get a **CPU & RAM** panel on the device page.
Servers and virtualization hosts get a **CPU & RAM** tab next to **Disks**.
It is shown for devices whose role is listed in `hardware_device_roles`, for
every device assigned to a cluster, and for every device that already has CPU &
RAM recorded. **Add** opens a form with:
RAM recorded; the badge shows the total number of cores. **Add** opens a form
with:
- **CPU model**: a searchable select over the catalogue. Typing `6338`,
`Gold 6338` or `epyc 7543` is enough; each entry shows cores, threads and
@@ -423,7 +424,7 @@ RAM recorded. **Add** opens a form with:
- **RAM**: number and size of the modules, total RAM (calculated from modules ×
size when left empty), type (DDR3/DDR4/DDR5) and speed in MT/s.
The panel then shows e.g. *2 × Intel Xeon Gold 6338 · 64 cores / 128 threads ·
The tab then shows e.g. *2 × Intel Xeon Gold 6338 · 64 cores / 128 threads ·
1 TB (16 × 64 GB DDR4-3200)*. The device list offers the optional columns
**CPU**, **Cores / Threads** and **RAM** under **Configure Table**.
@@ -457,8 +458,8 @@ machines (vCPUs and memory of the VM):
percent and the RAM that remains if the largest host fails (N+1). A table
lists every host with its CPU, VMs, ratio and RAM usage; hosts without CPU &
RAM are flagged with a link to record them.
- **Host (device) page:** the CPU & RAM panel adds the VMs running on that
host (VMs whose *Device* is set) with the same figures.
- **Host (device):** the CPU & RAM tab adds the capacity of that host and a
list of the VMs running on it (VMs whose *Device* is set).
- **VM page:** a **Host hardware** panel shows the CPU of the host, or of the
cluster hosts if the VM has no fixed host, and warns if the VM has more
vCPUs than the host has threads.
@@ -487,7 +488,7 @@ esx02,EPYC 9354,2,24,32,ddr5,4800
`?cores__gte=32`, `?cluster_id=4` and `?cpu_id=17`. GraphQL offers
`netbox_utilities_cpu_model(_list)` and `netbox_utilities_device_hardware(_list)`.
**Permissions.** The panels require `netbox_utilities.view_devicehardware`;
**Permissions.** The tab and panels require `netbox_utilities.view_devicehardware`;
recording and editing use `netbox_utilities.add_devicehardware` and
`netbox_utilities.change_devicehardware`. The catalogue uses the
`netbox_utilities.*_cpumodel` permissions.
+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>