diff --git a/README.md b/README.md index 25bcb8b..b915aa1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/netbox_utilities/hardware_integration.py b/netbox_utilities/hardware_integration.py index 3f38bd4..c251ecc 100644 --- a/netbox_utilities/hardware_integration.py +++ b/netbox_utilities/hardware_integration.py @@ -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( diff --git a/netbox_utilities/hardware_views.py b/netbox_utilities/hardware_views.py index ca69e7a..9509921 100644 --- a/netbox_utilities/hardware_views.py +++ b/netbox_utilities/hardware_views.py @@ -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) diff --git a/netbox_utilities/template_content.py b/netbox_utilities/template_content.py index 100b592..b28ba97 100644 --- a/netbox_utilities/template_content.py +++ b/netbox_utilities/template_content.py @@ -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"] diff --git a/netbox_utilities/templates/netbox_utilities/device_hardware.html b/netbox_utilities/templates/netbox_utilities/device_hardware.html new file mode 100644 index 0000000..3f47aa6 --- /dev/null +++ b/netbox_utilities/templates/netbox_utilities/device_hardware.html @@ -0,0 +1,69 @@ +{% extends 'dcim/device/base.html' %} + +{% block content %} +{% if not supported and not record %} +
+ CPU & RAM werden für Geräte mit den konfigurierten Rollen (z. B. Server) und für Cluster-Hosts gepflegt. +
+{% endif %} +
+
+
+

+ CPU & RAM + {% if record and can_change %} + + Bearbeiten + + {% elif not record and can_add %} + Erfassen + {% endif %} +

+ {% if record %} + {% include 'netbox_utilities/inc/hardware_record.html' %} + {% else %} +
Noch keine CPU und kein RAM erfasst.
+ {% endif %} +
+
+
+ {% if capacity %} +
+

+ Virtualisierung + {% if object.cluster %}Cluster {{ object.cluster }}{% endif %} +

+ {% include 'netbox_utilities/inc/hardware_capacity.html' %} +
+ {% endif %} +
+
+{% if capacity %} +
+

+ VMs auf diesem Host {{ vms|length }} + In der VM-Liste öffnen +

+ {% if vms %} +
+ + + + {% for vm in vms %} + + + + + + + + {% endfor %} + +
NameStatusvCPUsRAMMandant
{{ vm }}{{ vm.get_status_display }}{{ vm.vcpus|floatformat:"-1"|default:"—" }}{% if vm.memory %}{{ vm.memory }} MB{% else %}—{% endif %}{% if vm.tenant %}{{ vm.tenant }}{% else %}—{% endif %}
+
+ {% else %} +
Keine VMs mit diesem Gerät als Host.
+ {% endif %} +
+{% endif %} +{% endblock content %} diff --git a/netbox_utilities/templates/netbox_utilities/device_hardware_panel.html b/netbox_utilities/templates/netbox_utilities/device_hardware_panel.html deleted file mode 100644 index 3745fa2..0000000 --- a/netbox_utilities/templates/netbox_utilities/device_hardware_panel.html +++ /dev/null @@ -1,24 +0,0 @@ -
-

- CPU & RAM - {% if record and can_change %} - - Bearbeiten - - {% elif not record and can_add %} - Erfassen - {% endif %} -

- {% if record %} - {% include 'netbox_utilities/inc/hardware_record.html' %} - {% else %} -
Noch keine CPU und kein RAM erfasst.
- {% endif %} - {% if capacity %} -

- Virtualisierung - VMs anzeigen -

- {% include 'netbox_utilities/inc/hardware_capacity.html' %} - {% endif %} -