mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-17 04:32:54 -06:00
Added multiple template support
This commit is contained in:
parent
9b58f523c8
commit
fe693d4d7c
@ -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.
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
self.template_ids = list()
|
||||||
|
for nb_template_name in self.template_names:
|
||||||
|
found = False
|
||||||
for template in templates:
|
for template in templates:
|
||||||
if(template['name'] == self.template_name):
|
if(template['name'] == nb_template_name):
|
||||||
self.template_id = template['templateid']
|
self.template_ids.append({"templateid": template['templateid']})
|
||||||
e = (f"Found template ID {str(template['templateid'])} "
|
e = (f"Found template ID {str(template['templateid'])} "
|
||||||
f"for host {self.name}.")
|
f"for host {self.name}.")
|
||||||
logger.debug(e)
|
logger.debug(e)
|
||||||
return True
|
found = True
|
||||||
else:
|
continue
|
||||||
e = (f"Unable to find template {self.template_name} "
|
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']})
|
||||||
|
nb_template_ids = sorted({x['templateid'] for x in self.template_ids})
|
||||||
|
|
||||||
|
if zbx_template_ids == nb_template_ids:
|
||||||
logger.debug(f"Device {self.name}: template in-sync.")
|
logger.debug(f"Device {self.name}: template in-sync.")
|
||||||
break
|
|
||||||
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):
|
||||||
|
Loading…
Reference in New Issue
Block a user