Files
Netbox-Utilities/netbox_utilities/disk_integration.py
T
MrBlakeandClaude Opus 5.5 adbd697c0a feat: add disk management tab for servers, NAS, SAN and MSA devices
Devices whose role is one of disk_device_roles (default Server, NAS, SAN,
MSA) get a "Datenträger" tab with slot layout, disks, RAID groups,
warranty and age warnings, replacement history, CSV import, REST API and
GraphQL.

- First-visit wizard for slot count, rows, first slot number, numbering
  and form factor; layouts can be saved as device type templates and
  applied with one click.
- Slot grid drawn like the device front, coloured by status, with
  warning icons; free slots open a pre-filled disk form.
- Bulk fill of selected slots with shared attributes and one serial per
  line (barcode scanner friendly), with live count and duplicate checks.
- Sizes in vendor notation (960 GB, 4 TB, 1,92 TB, 2 TiB); the tenant is
  always taken from the device and follows tenant changes.
- RAID groups (RAID 0/1/5/6/10/50/60, RAID-Z1/Z2/Z3, JBOD) with hot
  spares, usable capacity, tolerated failures and degraded state.
- "Ersetzen" marks a disk as removed and opens the successor form;
  removed disks stay as per-slot history.
- Warnings for failed disks, expired or expiring warranties and disks
  older than disk_age_warning_years.
- Global disk list with filters, export, global search by serial,
  optional device list column, CSV import, REST API and GraphQL.
- New settings: disks_enabled, disk_device_roles,
  disk_age_warning_years, disk_warranty_warning_days.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 12:01:56 +02:00

64 lines
2.0 KiB
Python

import django_tables2 as tables
from django.db.models.signals import post_save
COLUMN_NAME = "netbox_utilities_disks"
def sync_disk_tenants(sender, instance, raw=False, **kwargs):
"""Keep the tenant of all disks in line with their device."""
if raw or not instance.pk:
return
from .models import Disk
Disk.objects.filter(device_id=instance.pk).exclude(tenant_id=instance.tenant_id).update(
tenant_id=instance.tenant_id
)
class DeviceDisksColumn(tables.Column):
"""Shows used/total slots for devices with a disk layout, linked to the tab."""
@staticmethod
def occupancy(record):
from . import disks
from .models import Disk, DiskBayLayout
layout = DiskBayLayout.objects.filter(device=record).only("slot_count").first()
if layout is None:
return None
used = Disk.objects.filter(device=record, status__in=disks.SLOT_OCCUPYING_STATUSES).count()
return f"{used}/{layout.slot_count}"
def render(self, record):
from django.urls import reverse
from django.utils.html import format_html
occupancy = self.occupancy(record)
if occupancy is None:
return "—"
return format_html('<a href="{}">{}</a>', reverse("dcim:device_disks", kwargs={"pk": record.pk}), occupancy)
def value(self, record):
return self.occupancy(record) or ""
def install_disk_support():
from dcim.models import Device
from dcim.tables import DeviceTable
from utilities.tables import register_table_column
# Registers the "Datenträger" device tab before NetBox builds the dcim URLs.
from . import disk_views # noqa: F401
post_save.connect(
sync_disk_tenants,
sender=Device,
dispatch_uid="netbox_utilities.sync_disk_tenants",
weak=False,
)
register_table_column(
DeviceDisksColumn(accessor=tables.A("pk"), verbose_name="Datenträger", orderable=False, empty_values=()),
COLUMN_NAME,
DeviceTable,
)