diff --git a/README.md b/README.md index 477f41c..9616983 100644 --- a/README.md +++ b/README.md @@ -414,9 +414,9 @@ Tags can be synced from the following sources: Syncing tags will override any tags that were set manually on the host, making NetBox the single source-of-truth for managing tags. -To enable syncing, turn on tag_sync in the config file. +To enable syncing, turn on `tag_sync` in the config file. By default, this script will modify tag names and tag values to lowercase. -You can change this behaviour by setting tag_lower to False. +You can change this behaviour by setting `tag_lower` to `False`. ```python tag_sync = True @@ -429,7 +429,8 @@ As NetBox doesn't follow the tag/value pattern for tags, we will need a tag name set to register the netbox tags. By default the tag name is "NetBox", but you can change this to whatever you want. -The value for the tag can be set to 'name', 'display', or 'slug', which refers to the property of the NetBox tag object that will be used as the value in Zabbix. +The value for the tag can be set to 'name', 'display', or 'slug', which refers to the +property of the NetBox tag object that will be used as the value in Zabbix. ```python tag_name = 'NetBox' @@ -512,7 +513,7 @@ Through this method, it is possible to define the following types of usermacros: 2. Secret 3. Vault -The default macro type is text if no `type` and `value` have been set. +The default macro type is text, if no `type` and `value` have been set. It is also possible to create usermacros with [context](https://www.zabbix.com/documentation/7.0/en/manual/config/macros/user_macros_context). @@ -632,7 +633,8 @@ python3 netbox_zabbix_sync.py ### Zabbix proxy -You can set the proxy for a device using the 'proxy' key in config context. +#### Config Context +You can set the proxy for a device using the `proxy` key in config context. ```json { @@ -673,6 +675,33 @@ In the example above the host will use the group on Zabbix 7. On Zabbix 6 and below the host will use the proxy. Zabbix 7 will use the proxy value when omitting the proxy_group value. +#### Custom Field + +Alternatively, you can use a custom field for assigning a device or VM to +a Zabbix proxy or proxy group. The custom fields can be assigned to both +Devices and VMs. + +You can also assign these custom fields to a site to allow all devices/VMs +in that site to be confured with the same proxy or proxy group. +In order for this to work, `extended_site_properties` needs to be enabled in +the configuation as well. + +To use the custom fields for proxy configuration, configure one or both +of the following settings in the configuration file with the actual names of your +custom fields: + +```python +proxy_cf = "zabbix_proxy" +proxy_group_cf = "zabbix_proxy_group" +``` + +As with config context proxy configuration, proxy group will take precedence over +proxy when configured. +Proxy settings configured on the device or VM will take precedence over any site configuration. + +If the custom fields have no value but the proxy or proxy group is configured in config context, +that setting will be used. + ### Set interface parameters within NetBox When adding a new device, you can set the interface type with custom context. By diff --git a/modules/config.py b/modules/config.py index 43bd775..b1afcfb 100644 --- a/modules/config.py +++ b/modules/config.py @@ -16,6 +16,8 @@ DEFAULT_CONFIG = { "templates_config_context_overrule": False, "template_cf": "zabbix_template", "device_cf": "zabbix_hostid", + "proxy_cf": False, + "proxy_group_cf" : False, "clustering": False, "create_hostgroups": True, "create_journal": False, diff --git a/modules/device.py b/modules/device.py index cf0e2a2..ad99e9d 100644 --- a/modules/device.py +++ b/modules/device.py @@ -458,9 +458,14 @@ class PhysicalDevice: """ Sets proxy or proxy group if this value has been defined in config context + or custom fields. input: List of all proxies and proxy groups in standardized format """ + proxy_name = None + + #!!! FIX this check to still work if only CF is configurd. + # check if the key Zabbix is defined in the config context if "zabbix" not in self.nb.config_context: return False @@ -477,10 +482,21 @@ class PhysicalDevice: # Only insert groups in front of list for Zabbix7 proxy_types.insert(0, "proxy_group") for proxy_type in proxy_types: - # Check if the key exists in NetBox CC - if proxy_type in self.nb.config_context["zabbix"]: + # Check if we should use custom fields for proxy config + field_config = "proxy_cf" if proxy_type=="proxy" else "proxy_group_cf" + if config[field_config]: + if config[field_config] in self.nb.custom_fields: + if self.nb.custom_fields[config[field_config]]: + proxy_name = self.nb.custom_fields[config[field_config]] + elif config[field_config] in self.nb.site.custom_fields: + if self.nb.site.custom_fields[config[field_config]]: + proxy_name = self.nb.site.custom_fields[config[field_config]] + + # Otherwise check if the proxy is configured in NetBox CC + if not proxy_name and proxy_type in self.nb.config_context["zabbix"]: proxy_name = self.nb.config_context["zabbix"][proxy_type] # go through all proxies + if proxy_name: for proxy in proxy_list: # If the proxy does not match the type, ignore and continue if not proxy["type"] == proxy_type: