diff --git a/modules/device.py b/modules/device.py index ee54891..76a41a7 100644 --- a/modules/device.py +++ b/modules/device.py @@ -146,11 +146,11 @@ class PhysicalDevice(): """ Get Zabbix templates from the device context """ if "zabbix" not in self.config_context: e = (f"Host {self.name}: Key 'zabbix' not found in config " - "context for template") + "context for template lookup") raise TemplateError(e) if "templates" not in self.config_context["zabbix"]: e = (f"Host {self.name}: Key 'templates' not found in config " - "context 'zabbix' for template host") + "context 'zabbix' for template lookup") raise TemplateError(e) return self.config_context["zabbix"]["templates"] diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index bcf4558..cfe95fb 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -6,6 +6,8 @@ import logging import argparse from os import environ, path, sys from pynetbox import api +from pynetbox.core.query import RequestError as NBRequestError +from requests.exceptions import ConnectionError as RequestsConnectionError from zabbix_utils import ZabbixAPI, APIRequestError, ProcessingError from modules.device import PhysicalDevice from modules.virtual_machine import VirtualMachine @@ -82,7 +84,15 @@ def main(arguments): allowed_objects = ["location", "role", "manufacturer", "region", "site", "site_group", "tenant", "tenant_group"] # Create API call to get all custom fields which are on the device objects - device_cfs = netbox.extras.custom_fields.filter(type="text", content_type_id=23) + try: + device_cfs = list(netbox.extras.custom_fields.filter(type="text", content_type_id=23)) + except RequestsConnectionError: + logger.error(f"Unable to connect to Netbox with URL {netbox_host}." + " Please check the URL and status of Netbox.") + sys.exit(1) + except NBRequestError as e: + logger.error(f"Netbox error: {e}") + sys.exit(1) for cf in device_cfs: allowed_objects.append(cf.name) for hg_object in hg_objects: