Added basic error message when Netbox details are invalid. Fixed logging message for when template context keys are not present.

This commit is contained in:
TheNetworkGuy 2024-11-06 15:57:11 +01:00
parent ffc2aa1947
commit 56c19d97de
2 changed files with 13 additions and 3 deletions

View File

@ -146,11 +146,11 @@ class PhysicalDevice():
""" Get Zabbix templates from the device context """ """ Get Zabbix templates from the device context """
if "zabbix" not in self.config_context: if "zabbix" not in self.config_context:
e = (f"Host {self.name}: Key 'zabbix' not found in config " e = (f"Host {self.name}: Key 'zabbix' not found in config "
"context for template") "context for template lookup")
raise TemplateError(e) raise TemplateError(e)
if "templates" not in self.config_context["zabbix"]: if "templates" not in self.config_context["zabbix"]:
e = (f"Host {self.name}: Key 'templates' not found in config " e = (f"Host {self.name}: Key 'templates' not found in config "
"context 'zabbix' for template host") "context 'zabbix' for template lookup")
raise TemplateError(e) raise TemplateError(e)
return self.config_context["zabbix"]["templates"] return self.config_context["zabbix"]["templates"]

View File

@ -6,6 +6,8 @@ import logging
import argparse import argparse
from os import environ, path, sys from os import environ, path, sys
from pynetbox import api 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 zabbix_utils import ZabbixAPI, APIRequestError, ProcessingError
from modules.device import PhysicalDevice from modules.device import PhysicalDevice
from modules.virtual_machine import VirtualMachine from modules.virtual_machine import VirtualMachine
@ -82,7 +84,15 @@ def main(arguments):
allowed_objects = ["location", "role", "manufacturer", "region", allowed_objects = ["location", "role", "manufacturer", "region",
"site", "site_group", "tenant", "tenant_group"] "site", "site_group", "tenant", "tenant_group"]
# Create API call to get all custom fields which are on the device objects # 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: for cf in device_cfs:
allowed_objects.append(cf.name) allowed_objects.append(cf.name)
for hg_object in hg_objects: for hg_object in hg_objects: