From bfadd88542ea0b285e8c216a9b1bdba4d140c9fa Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Fri, 13 Jun 2025 10:49:40 +0200 Subject: [PATCH] perform hostgroup creation before consistency check --- netbox_zabbix_sync.py | 57 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index 5418b50..3e50aff 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -85,14 +85,10 @@ def main(arguments): # Set NetBox API netbox = api(netbox_host, token=netbox_token, threading=True) # Create API call to get all custom fields which are on the device objects - device_cfs = [] try: - device_cfs = list( - netbox.extras.custom_fields.filter(type="text", content_types="dcim.device") - ) - vm_cfs = list( - netbox.extras.custom_fields.filter(type="text", content_types="virtualization.virtualmachine") - ) + # Get NetBox version + nb_version = netbox.version + logger.debug(f"NetBox version is {nb_version}.") except RequestsConnectionError: logger.error( f"Unable to connect to NetBox with URL {netbox_host}." @@ -102,10 +98,18 @@ def main(arguments): except NBRequestError as e: logger.error(f"NetBox error: {e}") sys.exit(1) - logger.debug(device_cfs) # Check if the provided Hostgroup layout is valid + device_cfs = [] + vm_cfs = [] + device_cfs = list( + netbox.extras.custom_fields.filter(type="text", content_types="dcim.device") + ) verify_hg_format(hostgroup_format, device_cfs=device_cfs, hg_type="dev", logger=logger) - verify_hg_format(vm_hostgroup_format, vm_cfs=vm_cfs, hg_type="vm", logger=logger) + if sync_vms: + vm_cfs = list( + netbox.extras.custom_fields.filter(type="text", content_types="virtualization.virtualmachine") + ) + verify_hg_format(vm_hostgroup_format, vm_cfs=vm_cfs, hg_type="vm", logger=logger) # Set Zabbix API try: ssl_ctx = ssl.create_default_context() @@ -152,9 +156,6 @@ def main(arguments): # Prepare list of all proxy and proxy_groups zabbix_proxy_list = proxy_prepper(zabbix_proxies, zabbix_proxygroups) - # Get NetBox API version - nb_version = netbox.version - # Go through all NetBox devices for nb_vm in netbox_vms: try: @@ -191,6 +192,14 @@ def main(arguments): # Check if the VM is in the disabled state if vm.status in zabbix_device_disable: vm.zabbix_state = 1 + # Add hostgroup if config is set + if create_hostgroups: + # Create new hostgroup. Potentially multiple groups if nested + hostgroups = vm.createZabbixHostgroup(zabbix_groups) + # go through all newly created hostgroups + for group in hostgroups: + # Add new hostgroups to zabbix group list + zabbix_groups.append(group) # Check if VM is already in Zabbix if vm.zabbix_id: vm.ConsistencyCheck( @@ -201,14 +210,6 @@ def main(arguments): create_hostgroups, ) continue - # Add hostgroup if config is set - if create_hostgroups: - # Create new hostgroup. Potentially multiple groups if nested - hostgroups = vm.createZabbixHostgroup(zabbix_groups) - # go through all newly created hostgroups - for group in hostgroups: - # Add new hostgroups to zabbix group list - zabbix_groups.append(group) # Add VM to Zabbix vm.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list) except SyncError: @@ -268,6 +269,14 @@ def main(arguments): # Check if the device is in the disabled state if device.status in zabbix_device_disable: device.zabbix_state = 1 + # Add hostgroup is config is set + if create_hostgroups: + # Create new hostgroup. Potentially multiple groups if nested + hostgroups = device.createZabbixHostgroup(zabbix_groups) + # go through all newly created hostgroups + for group in hostgroups: + # Add new hostgroups to zabbix group list + zabbix_groups.append(group) # Check if device is already in Zabbix if device.zabbix_id: device.ConsistencyCheck( @@ -278,14 +287,6 @@ def main(arguments): create_hostgroups, ) continue - # Add hostgroup is config is set - if create_hostgroups: - # Create new hostgroup. Potentially multiple groups if nested - hostgroups = device.createZabbixHostgroup(zabbix_groups) - # go through all newly created hostgroups - for group in hostgroups: - # Add new hostgroups to zabbix group list - zabbix_groups.append(group) # Add device to Zabbix device.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list) except SyncError: