Skip to content

feat: Integrate maintenance events with NetBox event system#20

Merged
1 commit merged into
diegogodoy06:mainfrom
petersonbasso:main
Jun 11, 2026
Merged

feat: Integrate maintenance events with NetBox event system#20
1 commit merged into
diegogodoy06:mainfrom
petersonbasso:main

Conversation

@petersonbasso

Copy link
Copy Markdown
Contributor

This pull request integrates the maintenance planner and execution tracking with NetBox's native event system. With this integration, users can configure Event Rules, Webhooks, custom Scripts and Notifications triggered by the lifecycle of maintenance plans and executions.

Key Features & Changes

  1. Custom Event Types Registration
    • Registered three new event types (maintenance_due, maintenance_scheduled, and maintenance_completed) into the central NetBox registry (registry['event_types']) during the plugin initialization in netbox_maintenance_device/__init__.py.
  2. Unified Event Dispatcher
    • Implemented events.py with a robust fire_event helper. It automatically handles enqueuing events in the current request queue (to respect database transaction commits) or immediately flushing events when executed outside the HTTP request cycle (e.g., from management commands or background system jobs).
  3. Event Triggers on Execution Changes
    • Added hooks in MaintenanceExecution.save() to automatically fire events:
      • maintenance_scheduled when an execution is set to the Scheduled state.
      • maintenance_completed when an execution is completed.
  4. Overdue Detections & Cron Integration
    • Added a last_notified_date field to the MaintenancePlan model via migrations to track due date alerts.
    • Created a daily system job CheckMaintenanceJob (using @system_job) and a Django management command check_maintenance to periodically scan active plans, identify overdue schedules, and fire the maintenance_due event while avoiding duplicate notifications.
  5. UI & Filter Forms
    • Implemented MaintenancePlanFilterForm and MaintenanceExecutionFilterForm in forms.py to support UI search and filtering (by device, virtual machine, tags, active status, etc.).

- Register custom event types (maintenance_due, maintenance_scheduled, maintenance_completed) in NetBox registry
- Implement core events module to dispatch events safely within/outside HTTP request contexts
- Fire events automatically when a MaintenanceExecution is scheduled or completed
- Add CheckMaintenanceJob and check_maintenance command to periodically detect overdue plans and fire events
- Track last notified date on MaintenancePlan to prevent duplicate events
- Implement filter forms for MaintenancePlan and MaintenanceExecution
@diegogodoy06 diegogodoy06 self-assigned this May 22, 2026
@diegogodoy06 diegogodoy06 added the enhancement New feature or request label May 22, 2026
@diegogodoy06 diegogodoy06 self-requested a review May 25, 2026 10:58
@diegogodoy06

Copy link
Copy Markdown
Owner

Tested locally on a clean NetBox v4.6.2 instance (Docker, PostgreSQL 16) with the plugin installed from this branch — thanks @petersonbasso, the event system integration works well. Verification performed:

  • ✅ All plugin migrations apply cleanly from scratch (00010004); manage.py check passes; makemigrations --check reports no model drift
  • ✅ Custom event types (maintenance_due, maintenance_scheduled, maintenance_completed) registered in the NetBox registry
  • ✅ End-to-end: overdue plan + Event Rule (notification group) → check_maintenance fires maintenance_due and the notification is delivered; re-running does not duplicate (last_notified_date suppression works)
  • MaintenanceExecution.save() hooks fire maintenance_scheduled / maintenance_completed both outside a request (immediate flush) and inside a request context (queued via EventContext, coexisting with NetBox's native change events in the same queue)
  • ✅ Full NetBox boot with the plugin enabled; UI and API routes respond normally

Two compatibility issues were found during testing; they are fixed in a follow-up commit that will be included in the merge:

  1. Migration dependency: ("extras", "0138_customfieldchoiceset_choice_colors") only ships with NetBox 4.6.0, so migrate would fail with NodeNotFoundError on NetBox 4.4/4.5 (still supported by this plugin). Changed to ("extras", "__latest__"), matching the plugin's other migrations.
  2. Missing data key in the event payload: extras.events.process_event_rules reads event['data'] unconditionally. On the immediate-flush path (management command / system job — i.e. exactly the maintenance_due flow) every matching event rule raised KeyError: 'data' (swallowed by the logging wrapper), so due notifications never reached webhooks/scripts/notification groups. Fixed by eagerly including 'data': serialize_for_event(instance) in the payload; this also covers the plain-dict queue fallback on NetBox ≤ 4.5.

Merging manually with the fixes included.

@diegogodoy06 diegogodoy06 closed this pull request by merging all changes into diegogodoy06:main in 2430107 Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants