mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-12-11 02:49:36 -06:00
* Added support for object and select custom fields in host groups and proxy config.
* Corrected error when `full_proxy_sync` was not set and a host no longer uses a proxy.
This commit is contained in:
parent
123b243f56
commit
422d343c1f
@ -21,7 +21,7 @@ from modules.exceptions import (
|
||||
from modules.hostgroups import Hostgroup
|
||||
from modules.interface import ZabbixInterface
|
||||
from modules.tags import ZabbixTags
|
||||
from modules.tools import field_mapper, remove_duplicates, sanatize_log_output
|
||||
from modules.tools import field_mapper, cf_to_string, remove_duplicates, sanatize_log_output
|
||||
from modules.usermacros import ZabbixUsermacros
|
||||
from modules.config import load_config
|
||||
|
||||
@ -480,17 +480,17 @@ class PhysicalDevice:
|
||||
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]]
|
||||
proxy_name = cf_to_string(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]]
|
||||
proxy_name = cf_to_string(self.nb.site.custom_fields[config[field_config]])
|
||||
|
||||
# Otherwise check if the proxy is configured in NetBox CC
|
||||
if (not proxy_name and "zabbix" in self.nb.config_context and
|
||||
proxy_type in self.nb.config_context["zabbix"]):
|
||||
proxy_name = self.nb.config_context["zabbix"][proxy_type]
|
||||
# go through all proxies
|
||||
|
||||
|
||||
# If a proxy name was found, loop through all proxies to find a match
|
||||
if proxy_name:
|
||||
for proxy in proxy_list:
|
||||
# If the proxy does not match the type, ignore and continue
|
||||
@ -804,7 +804,7 @@ class PhysicalDevice:
|
||||
# Display error message
|
||||
self.logger.warning(
|
||||
"Host %s: Is configured with proxy in Zabbix but not in NetBox."
|
||||
"The -p flag was ommited: no changes have been made.",
|
||||
"full_proxy_sync is not set: no changes have been made.",
|
||||
self.name,
|
||||
)
|
||||
if not proxy_set:
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
from logging import getLogger
|
||||
|
||||
from modules.exceptions import HostgroupError
|
||||
from modules.tools import build_path
|
||||
from modules.tools import build_path, cf_to_string
|
||||
|
||||
|
||||
class Hostgroup:
|
||||
@ -134,7 +134,7 @@ class Hostgroup:
|
||||
raise HostgroupError(msg)
|
||||
# CF data is populated
|
||||
if cf_data["cf"]:
|
||||
hg_output.append(cf_data["cf"])
|
||||
hg_output.append(cf_to_string(cf_data["cf"]))
|
||||
continue
|
||||
# Check if there is a value associated to the variable.
|
||||
# For instance, if a device has no location, do not use it with hostgroup calculation
|
||||
|
||||
@ -50,6 +50,19 @@ def proxy_prepper(proxy_list, proxy_group_list):
|
||||
return output
|
||||
|
||||
|
||||
def cf_to_string(cf, key="name", logger=None):
|
||||
"""
|
||||
Converts a dict custom fields to string
|
||||
"""
|
||||
if isinstance(cf, dict):
|
||||
if key:
|
||||
return cf[key]
|
||||
else:
|
||||
logger.error("Conversion of custom field failed, '%s' not found in cf dict.", key)
|
||||
return None
|
||||
return cf
|
||||
|
||||
|
||||
def field_mapper(host, mapper, nbdevice, logger):
|
||||
"""
|
||||
Maps NetBox field data to Zabbix properties.
|
||||
@ -204,4 +217,4 @@ def sanatize_log_output(data):
|
||||
continue
|
||||
# A macro is not used, so we sanitize the value.
|
||||
sanitized_data["details"][key] = "********"
|
||||
return sanitized_data
|
||||
return sanitized_data
|
||||
@ -82,7 +82,7 @@ def main(arguments):
|
||||
device_cfs = []
|
||||
vm_cfs = []
|
||||
device_cfs = list(
|
||||
netbox.extras.custom_fields.filter(type="text", content_types="dcim.device")
|
||||
netbox.extras.custom_fields.filter(type=["text","object","select"], content_types="dcim.device")
|
||||
)
|
||||
verify_hg_format(
|
||||
config["hostgroup_format"], device_cfs=device_cfs, hg_type="dev", logger=logger
|
||||
@ -90,7 +90,7 @@ def main(arguments):
|
||||
if config["sync_vms"]:
|
||||
vm_cfs = list(
|
||||
netbox.extras.custom_fields.filter(
|
||||
type="text", content_types="virtualization.virtualmachine"
|
||||
type=["text","object","select"], content_types="virtualization.virtualmachine"
|
||||
)
|
||||
)
|
||||
verify_hg_format(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user