From 0b47afac9cde617e4f90a3d118d7d7c94862d891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hinz?= Date: Thu, 30 Apr 2026 16:00:00 +0200 Subject: [PATCH 1/3] custom hostname and displayname for zabbix --- nbxsync/__init__.py | 2 ++ nbxsync/settings.py | 4 ++++ nbxsync/utils/sync/hostsync.py | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/nbxsync/__init__.py b/nbxsync/__init__.py index 7311d8d..7d1fc86 100644 --- a/nbxsync/__init__.py +++ b/nbxsync/__init__.py @@ -117,6 +117,8 @@ class nbxSync(PluginConfig): 'attach_objtag': False, 'objtag_type': 'nb_type', 'objtag_id': 'nb_id', + 'custom_field_hostname': '', + 'custom_field_display_name': '', } queues = [] validated_config = None diff --git a/nbxsync/settings.py b/nbxsync/settings.py index 7e657b6..ebcd0c2 100644 --- a/nbxsync/settings.py +++ b/nbxsync/settings.py @@ -112,6 +112,10 @@ class PluginSettingsModel(BaseModel): objtag_id: str = Field(default='nb_id') + custom_field_hostname: str = Field(default='') + custom_field_display_name: str = Field(default='') + + # Helper function def get_plugin_settings() -> PluginSettingsModel: plugin_config = apps.get_app_config('nbxsync') diff --git a/nbxsync/utils/sync/hostsync.py b/nbxsync/utils/sync/hostsync.py index 1882144..23d0688 100644 --- a/nbxsync/utils/sync/hostsync.py +++ b/nbxsync/utils/sync/hostsync.py @@ -20,13 +20,34 @@ class HostSync(ZabbixSyncBase): def api_object(self): return self.api.host - def get_name_value(self): + def get_base_name(self): # If the object has the "name" attribute, only return that (Device). If not (cornercase?), return the display string if hasattr(self.obj.assigned_object, 'name'): return self.obj.assigned_object.name return str(self.obj.assigned_object) + def get_name_value(self): + base_name = self.get_base_name() + cf_name = getattr(self.pluginsettings, 'custom_field_hostname', '') + if cf_name and hasattr(self.obj.assigned_object, 'custom_field_data'): + cf_value = self.obj.assigned_object.custom_field_data.get(cf_name) + if cf_value: + return str(cf_value) + return base_name + + def get_display_name(self): + base_name = self.get_base_name() + cf_name = getattr(self.pluginsettings, 'custom_field_display_name', '') + if cf_name and hasattr(self.obj.assigned_object, 'custom_field_data'): + cf_value = self.obj.assigned_object.custom_field_data.get(cf_name) + if cf_value: + return str(cf_value) + return base_name + + def find_by_name(self): + return self.api_object().get(filter={'host': self.sanitize_string(input_str=str(self.get_name_value()))}) + def get_create_params(self): status = self.obj.assigned_object.status object_type = self.obj.assigned_object._meta.model_name # "device" or "virtualmachine" @@ -41,7 +62,7 @@ def get_create_params(self): return { 'host': self.sanitize_string(input_str=str(self.get_name_value())), - 'name': self.get_name_value(), + 'name': self.get_display_name(), 'groups': self.get_groups(), 'status': host_status, 'description': self.obj.assigned_object.description or '', From 5208a1cd358289e6837fae9f07a54c3ce3be6ee8 Mon Sep 17 00:00:00 2001 From: bhinz Date: Thu, 30 Apr 2026 16:35:03 +0200 Subject: [PATCH 2/3] Update installation.md added configuration options --- docs/installation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/installation.md b/docs/installation.md index 1b31938..9bd258f 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -110,6 +110,8 @@ PLUGINS_CONFIG = { 'attach_objtag': True, 'objtag_type': 'nb_type', 'objtag_id': 'nb_id', + 'custom_field_hostname':'', + 'custom_field_display_name':'', } } ``` From f25e15dbea82c07d4df9b1d730015689cfd8d32e Mon Sep 17 00:00:00 2001 From: bhinz Date: Thu, 30 Apr 2026 16:40:36 +0200 Subject: [PATCH 3/3] custom_fields added --- docs/configuration.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 2316c26..8dc66c9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -78,7 +78,9 @@ The plugin is configuration to do exactly what you want, by means of the plugin 'no_alerting_tag_value': '1', 'attach_objtag': True, 'objtag_type': 'nb_type', - 'objtag_id': 'nb_id' + 'objtag_id': 'nb_id', + 'custom_field_hostname':'', + 'custom_field_display_name':'' } ``` @@ -208,6 +210,9 @@ These tags allow you to navigate from a Zabbix host back to the corresponding N | `objtag_type` | `nb_type` | Tag name for the NetBox object type | | `objtag_id` | `nb_id` | Tag name for the NetBox object ID | +### custom_field_hostname and custom_field_display_name +You can use these fields to map the connection between NetBox and the Zabbix hostname and display name. The device name is used as the default. + ## Enabling and Disabling Synchronization Two separate `sync_enabled` flags control whether synchronization to Zabbix is active. @@ -216,4 +221,4 @@ Two separate `sync_enabled` flags control whether synchronization to Zabbix is a **On `ZabbixServerAssignment`**: disabling this stops synchronization for that specific device/VM assignment only. Other assignments to the same or different Zabbix servers are unaffected. -Both flags must be `True` for a sync to proceed. The "Sync Status" column on the Zabbix Server Assignments list shows a green check only when both the assignment and its server are enabled. \ No newline at end of file +Both flags must be `True` for a sync to proceed. The "Sync Status" column on the Zabbix Server Assignments list shows a green check only when both the assignment and its server are enabled.