Added #39. Updated code, documentation and config example

This commit is contained in:
TheNetworkGuy 2023-12-27 15:33:45 +01:00
parent 6389146342
commit 528efd0ff8
3 changed files with 59 additions and 29 deletions

View File

@ -77,6 +77,11 @@ After that make sure that for each host there is at least one template defined i
} }
``` ```
You can also opt for the default device type custom field behaviour but with the added benefit of overwriting the template should a device in Netbox have a device specific context defined. In this case the device specific context template(s) will take priority over the device type custom field template.
```
templates_config_context_overrule = True
```
## Permissions ## Permissions
### Netbox ### Netbox

View File

@ -2,6 +2,9 @@
# Set to true to enable the template source information # Set to true to enable the template source information
# coming from config context instead of a custom field. # coming from config context instead of a custom field.
templates_config_context = False templates_config_context = False
# Set to true to give config context templates a
# higher priority then custom field templates
templates_config_context_overrule = False
# Set template and device Netbox "custom field" names # Set template and device Netbox "custom field" names
# Template_cf is not used when templates_config_context is enabled # Template_cf is not used when templates_config_context is enabled

View File

@ -84,7 +84,7 @@ def main(arguments):
device = NetworkDevice(nb_device, zabbix, netbox_journals, device = NetworkDevice(nb_device, zabbix, netbox_journals,
arguments.journal) arguments.journal)
device.set_hostgroup(arguments.layout) device.set_hostgroup(arguments.layout)
device.set_template(templates_config_context) device.set_template(templates_config_context, templates_config_context_overrule)
# Checks if device is part of cluster. # Checks if device is part of cluster.
# Requires the cluster argument. # Requires the cluster argument.
if(device.isCluster() and arguments.cluster): if(device.isCluster() and arguments.cluster):
@ -170,6 +170,8 @@ class ProxyConfigError(SyncError):
class HostgroupError(SyncError): class HostgroupError(SyncError):
pass pass
class TemplateError(SyncError):
pass
class NetworkDevice(): class NetworkDevice():
@ -256,34 +258,55 @@ class NetworkDevice():
logger.error(e) logger.error(e)
raise SyncInventoryError(e) raise SyncInventoryError(e)
def set_template(self, templates_config_context): def set_template(self, prefer_config_context, overrule_custom):
if templates_config_context: self.zbx_template_names = None
# Template lookup using config context # Gather templates ONLY from the device specific context
if("zabbix" not in self.config_context): if prefer_config_context:
e = ("Key 'zabbix' not found in config " try:
f"context for template host {self.name}") self.zbx_template_names = self.get_templates_context()
except TemplateError as e:
logger.warning(e) logger.warning(e)
raise SyncInventoryError(e) return True
if("templates" not in self.config_context["zabbix"]): # Gather templates from the custom field but overrule
e = ("Key 'zabbix' not found in config " # them should there be any device specific templates
f"context for template host {self.name}") if overrule_custom:
logger.warning(e) try:
raise SyncInventoryError(e) self.zbx_template_names = self.get_templates_context()
self.zbx_template_names = self.config_context["zabbix"]["templates"] except TemplateError:
pass
if not self.zbx_template_names:
self.zbx_template_names = self.get_templates_cf()
return True
# Gather templates ONLY from the custom field
self.zbx_template_names = self.get_templates_cf()
return True
def get_templates_cf(self):
# Get Zabbix templates from the device type
device_type_cfs = self.nb.device_type.custom_fields
# Check if the ZBX Template CF is present
if(template_cf in device_type_cfs):
# Set value to template
return [device_type_cfs[template_cf]]
else: else:
# Get device type custom fields # Custom field not found, return error
device_type_cfs = self.nb.device_type.custom_fields e = (f"Custom field {template_cf} not "
# Check if the ZBX Template CF is present f"found for {self.nb.device_type.manufacturer.name}"
if(template_cf in device_type_cfs): f" - {self.nb.device_type.display}.")
# Set value to template
self.zbx_template_names = [device_type_cfs[template_cf]] raise TemplateError(e)
else:
# Custom field not found, return error def get_templates_context(self):
e = (f"Custom field {template_cf} not " # Get Zabbix templates from the device context
f"found for {self.nb.device_type.manufacturer.name}" if("zabbix" not in self.config_context):
f" - {self.nb.device_type.display}.") e = ("Key 'zabbix' not found in config "
logger.warning(e) f"context for template host {self.name}")
raise SyncInventoryError(e) raise TemplateError(e)
if("templates" not in self.config_context["zabbix"]):
e = ("Key 'zabbix' not found in config "
f"context for template host {self.name}")
raise TemplateError(e)
return self.config_context["zabbix"]["templates"]
def isCluster(self): def isCluster(self):
""" """
@ -337,8 +360,7 @@ class NetworkDevice():
""" """
# Check if there are templates defined # Check if there are templates defined
if(not self.zbx_template_names): if(not self.zbx_template_names):
e = (f"Device template '{self.nb.device_type.display}' " e = (f"No templates found for device {self.name}")
"has no Zabbix templates defined.")
logger.info(e) logger.info(e)
raise SyncInventoryError() raise SyncInventoryError()
# Set variable to empty list # Set variable to empty list