Skip to content

Feature: Add ZabbixTemplateRule for regex-based template assignment by platform name#117

Open
berlikm wants to merge 9 commits into
OpensourceICTSolutions:developmentfrom
berlikm:feature/templaterule-regex
Open

Feature: Add ZabbixTemplateRule for regex-based template assignment by platform name#117
berlikm wants to merge 9 commits into
OpensourceICTSolutions:developmentfrom
berlikm:feature/templaterule-regex

Conversation

@berlikm

@berlikm berlikm commented Jul 13, 2026

Copy link
Copy Markdown

Summary

Adds a new ZabbixTemplateRule model that matches platform names against regex patterns to automatically assign Zabbix templates. Solves the scaling problem where vCenter/netbox-sync creates dozens of platform variants (e.g. 'Windows Server 2022 (Build 20348.5400)') that all need the same template.

How it works

  1. ZabbixTemplateRule with pattern (regex), zabbixtemplate, priority, enabled
  2. Resolved in get_assigned_zabbixobjects() after direct + inherited templates — direct assignments always take priority
  3. Case-insensitive matching with 2-second ReDoS timeout (signal.alarm-based, falls back to unbounded re.search in sub-threads)
  4. Full CRUD: model, form, filterset, table, view, URLs, navigation menu

Migration

0013_zabbixtemplaterule

Includes tags (TaggableManager) and custom_field_data (JSONField with encoder) in the CreateModel operation — these are inherited from NetBoxModel and must be declared in the migration.

Dependencies

Stacks on PR #115. Merge after #115.

Testing

8 new tests covering: matching, non-matching, multi-rule, direct override, disabled rules, case-insensitive, no-platform, priority ordering, ReDoS timeout. All 1076 existing tests pass.

Part of #121.

@berlikm
berlikm force-pushed the feature/templaterule-regex branch from 2a56e43 to 394153c Compare July 14, 2026 13:02
berlikm added 3 commits July 14, 2026 14:15
Adds Site, SiteGroup, and Region as valid assignment targets for all
ZabbixServerAssignment, ZabbixHostgroupAssignment, ZabbixTemplateAssignment,
ZabbixTagAssignment, ZabbixMacroAssignment, and ZabbixHostInventory objects.

This allows assignments made at the Site/SiteGroup/Region level to be
inherited by all devices and VMs at that site or below, via the existing
inheritance_chain mechanism.

Changes:
- Constants: Site/SiteGroup/Region added to ASSIGNMENT_MODELS, ASSIGNMENT_TYPE_TO_FIELD, PATH_LABELS
- Forms: All 7 assignment forms get Site/SiteGroup/Region DynamicModelChoiceField + FieldSet
- Settings: inheritance_chain extended with Site/SiteGroup/Region resolution paths
- Jobs: SyncHostJob._prepare_assignment() creates detached copies for inherited assignments
- HostSync: _get_sync_target() resolves actual Device/VM for Jinja2 rendering
- HostSync: get_tag_attributes() deduplicates tags by (tag, value) to prevent
  Zabbix 7 rejecting duplicate pairs inherited from multiple sources
- HostInterfaceSync: get_create_params() falls back to the device's primary IP
  when the interface has no IP assigned (e.g. when inherited from SiteGroup)
- HostInterfaceSync: errors are caught per-interface so one failing interface
  does not prevent the remaining interfaces and templates from being synced
- Inheritance: ConfigGroup interface expansion removed; interfaces are now
  resolved naturally via the inheritance chain with primary IP fallback
- SyncBase: _is_inherited_copy guard prevents save()/update_sync_info() on inherited copies
- Views: ZabbixSiteTabView, ZabbixSiteGroupTabView, ZabbixRegionTabView added
- Tests: 9 new tests for inheritance resolution

Tested: all 1076 existing tests pass. Verified end-to-end on dev NetBox.
- Filter inherited/direct ZabbixServerAssignment and ZabbixHostInterface by zabbixserver
- Resolve ConfigGroup interface IP from primary_ip4/primary_ip6
- Add seen-set cycle guard to resolve_path
- Fixes silent dropping of Site/SiteGroup assignments
@berlikm
berlikm force-pushed the feature/templaterule-regex branch from 394153c to a4767a3 Compare July 14, 2026 14:34
berlikm added 3 commits July 14, 2026 14:54
Fixes inherited Site/SiteGroup/Region assignments reading custom fields from
the intermediate object instead of the actual Device/VM.
Adds a new ZabbixTemplateRule model that matches platform names against
regex patterns to automatically assign Zabbix templates. This solves the
scaling problem where vCenter/netbox-sync creates dozens of platform
variants (e.g. 'Windows Server 2022 (Build 20348.5400)') that all need
the same template.

- New model: ZabbixTemplateRule (name, pattern, zabbixtemplate, enabled, priority)
- Resolved in get_assigned_zabbixobjects() after direct + inherited templates
- Direct assignments always take priority over regex rules
- Case-insensitive matching
- Full CRUD: model, form, filterset, table, view, URLs, navigation menu
- Migration 0013: creates ZabbixTemplateRule table
- 8 tests covering: matching, non-matching, multi-rule, direct override,
  disabled rules, case-insensitive, no-platform, priority ordering
- All 15 tests pass (8 new + 7 existing)
Replace the unbounded re.search with a signal.alarm-based timeout that
prevents catastrophic backtracking patterns from hanging the sync job.

The timeout uses signal.SIGALRM which works in the main thread (where
Django views and RQ worker jobs run). If called from a sub-thread
where signals are unavailable, it gracefully falls back to an unbounded
re.search — acceptable because platform names are short (<100 chars)
and patterns are validated at model save time via re.compile().
@berlikm

berlikm commented Jul 14, 2026

Copy link
Copy Markdown
Author

This PR is part of the zero-touch provisioning feature set described in #121. Please see that issue for the full architecture and motivation.

ZabbixTemplateRule inherits from NetBoxModel which includes tags
(TaggableManager) and custom_field_data (JSONField with encoder).
The original migration omitted these fields, causing
makemigrations --check to generate a follow-up migration on fresh
installs.
berlikm added a commit to berlikm/nbxsync that referenced this pull request Jul 15, 2026
# Conflicts:
#	nbxsync/utils/inheritance.py
berlikm added a commit to berlikm/nbxsync that referenced this pull request Jul 15, 2026
# Conflicts:
#	nbxsync/migrations/0015_zabbixtemplaterule.py
berlikm added a commit to berlikm/nbxsync that referenced this pull request Jul 15, 2026
…zabbixhostbinding

0015 was depending on 0012 (original PR OpensourceICTSolutions#117 base), creating a parallel
leaf node alongside 0014_zabbixhostbinding. Point it at 0014 so the
chain is 0013→0014→0015→0016 with no branches.
Add 'or []' to all dict.get() calls on all_objects to handle None.
Mirrors PR OpensourceICTSolutions#115 fix #A.
@berlikm
berlikm force-pushed the feature/templaterule-regex branch from 01d8873 to 7d55072 Compare July 16, 2026 07:33
berlikm added a commit to berlikm/nbxsync that referenced this pull request Jul 16, 2026
PR OpensourceICTSolutions#117 added the ZabbixTemplateRule model, view, form, table, etc. but
forgot the ObjectView template. Clicking on a template rule in the UI
threw TemplateDoesNotExist because NetBox looks for
nbxsync/zabbixtemplaterule.html when rendering the ObjectView.
PR OpensourceICTSolutions#117 added the ZabbixTemplateRule model, view, form, table, etc. but
forgot the ObjectView template. Clicking on a template rule in the UI
threw TemplateDoesNotExist because NetBox looks for
nbxsync/zabbixtemplaterule.html when rendering the ObjectView.
@berlikm

berlikm commented Jul 16, 2026

Copy link
Copy Markdown
Author

Missing template file

This PR is missing the nbxsync/templates/nbxsync/zabbixtemplaterule.html template. When a user clicks on a ZabbixTemplateRule in the UI to view its detail page, NetBox throws:

django.template.exceptions.TemplateDoesNotExist: nbxsync/zabbixtemplaterule.html

The ZabbixTemplateRuleView (ObjectView) at views/zabbixtemplaterule.py:27 expects this template to exist. I've pushed commit 8ef5cdb to this branch with the missing template file — a standard NetBox object detail template showing the rule's pattern, template, hostgroup, tag, priority, and enabled status.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant