Added multiple template support

This commit is contained in:
Gabor SOMOGYVARI 2023-04-18 07:42:48 +02:00
parent 9b58f523c8
commit fe693d4d7c
2 changed files with 31 additions and 19 deletions

View File

@ -42,11 +42,12 @@ Use the following custom fields in Netbox:
* Object: dcim > device * Object: dcim > device
``` ```
``` ```
* Type: Text * Type: Multiple selection
* Name: zabbix_template * Name: zabbix_template
* Required: False * Required: False
* Default: null * Default: null
* Object: dcim > device_type * Object: dcim > device_type
* Choices: comma separated list of template names
``` ```
You can make the hostID field hidden or read-only to prevent human intervention. You can make the hostID field hidden or read-only to prevent human intervention.

View File

@ -216,7 +216,7 @@ class NetworkDevice():
# Gather device Zabbix template # Gather device Zabbix template
device_type_cf = self.nb.device_type.custom_fields device_type_cf = self.nb.device_type.custom_fields
if(template_cf in device_type_cf): if(template_cf in device_type_cf):
self.template_name = device_type_cf[template_cf] self.template_names = device_type_cf[template_cf]
else: else:
e = (f"Custom field {template_cf} not " e = (f"Custom field {template_cf} not "
f"found for {self.nb.device_type.manufacturer.name}" f"found for {self.nb.device_type.manufacturer.name}"
@ -315,20 +315,29 @@ class NetworkDevice():
INPUT: list of templates INPUT: list of templates
OUTPUT: True OUTPUT: True
""" """
if(not self.template_name): if(not self.template_names):
e = (f"Device template '{self.nb.device_type.display}' " e = (f"Device template '{self.nb.device_type.display}' "
"has no Zabbix template defined.") "has no Zabbix templates defined.")
logger.info(e) logger.info(e)
raise SyncInventoryError() raise SyncInventoryError()
for template in templates: self.template_ids = list()
if(template['name'] == self.template_name): for nb_template_name in self.template_names:
self.template_id = template['templateid'] found = False
e = (f"Found template ID {str(template['templateid'])} " for template in templates:
f"for host {self.name}.") if(template['name'] == nb_template_name):
logger.debug(e) self.template_ids.append({"templateid": template['templateid']})
return True e = (f"Found template ID {str(template['templateid'])} "
else: f"for host {self.name}.")
e = (f"Unable to find template {self.template_name} " logger.debug(e)
found = True
continue
if not found:
e = (f"Unable to find template {nb_template_name} "
f"for host {self.name} in Zabbix.")
logger.warning(e)
raise SyncInventoryError(e)
if len(self.template_ids) == 0:
e = (f"Unable to find templates {self.template_names} "
f"for host {self.name} in Zabbix.") f"for host {self.name} in Zabbix.")
logger.warning(e) logger.warning(e)
raise SyncInventoryError(e) raise SyncInventoryError(e)
@ -433,7 +442,7 @@ class NetworkDevice():
# Set interface, group and template configuration # Set interface, group and template configuration
interfaces = self.setInterfaceDetails() interfaces = self.setInterfaceDetails()
groups = [{"groupid": self.group_id}] groups = [{"groupid": self.group_id}]
templates = [{"templateid": self.template_id}] templates = self.template_ids
# Set Zabbix proxy if defined # Set Zabbix proxy if defined
self.setProxy(proxys) self.setProxy(proxys)
# Add host to Zabbix # Add host to Zabbix
@ -523,13 +532,15 @@ class NetworkDevice():
f"Received value: {host['host']}") f"Received value: {host['host']}")
self.updateZabbixHost(host=self.name) self.updateZabbixHost(host=self.name)
for template in host["parentTemplates"]: # Making a sorted set of both side's template ids to be able to compare them
if(template["templateid"] == self.template_id): zbx_template_ids = sorted({x['templateid'] for x in host['parentTemplates']})
logger.debug(f"Device {self.name}: template in-sync.") nb_template_ids = sorted({x['templateid'] for x in self.template_ids})
break
if zbx_template_ids == nb_template_ids:
logger.debug(f"Device {self.name}: template in-sync.")
else: else:
logger.warning(f"Device {self.name}: template OUT of sync.") logger.warning(f"Device {self.name}: template OUT of sync.")
self.updateZabbixHost(templates=self.template_id) self.updateZabbixHost(templates=self.template_ids)
for group in host["groups"]: for group in host["groups"]:
if(group["groupid"] == self.group_id): if(group["groupid"] == self.group_id):