mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 01:41:25 -06:00
Merge branch 'main' of https://github.com/TheNetworkGuy/netbox-zabbix-sync
This commit is contained in:
commit
58d894832e
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# pylint: disable=invalid-name, logging-not-lazy, too-many-locals, logging-fstring-interpolation
|
# pylint: disable=invalid-name, logging-not-lazy, too-many-locals, logging-fstring-interpolation, too-many-lines
|
||||||
|
|
||||||
|
|
||||||
"""Netbox to Zabbix sync script."""
|
"""Netbox to Zabbix sync script."""
|
||||||
@ -256,6 +256,7 @@ class NetworkDevice():
|
|||||||
self.nb = nb
|
self.nb = nb
|
||||||
self.id = nb.id
|
self.id = nb.id
|
||||||
self.name = nb.name
|
self.name = nb.name
|
||||||
|
self.visible_name = None
|
||||||
self.status = nb.status.label
|
self.status = nb.status.label
|
||||||
self.zabbix = zabbix
|
self.zabbix = zabbix
|
||||||
self.zabbix_id = None
|
self.zabbix_id = None
|
||||||
@ -295,6 +296,19 @@ class NetworkDevice():
|
|||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
raise SyncInventoryError(e)
|
raise SyncInventoryError(e)
|
||||||
|
|
||||||
|
# Validate hostname format.
|
||||||
|
odd_character_list = ["ä", "ö", "ü", "Ä", "Ö", "Ü", "ß"]
|
||||||
|
self.use_visible_name = False
|
||||||
|
if any(letter in self.name for letter in odd_character_list):
|
||||||
|
self.name = f"NETBOX_ID{self.id}"
|
||||||
|
self.visible_name = self.nb.name
|
||||||
|
self.use_visible_name = True
|
||||||
|
logger.info(f"Device {self.visible_name} contains special characters. "
|
||||||
|
f"Using {self.name} as name for the Netbox object "
|
||||||
|
f"and using {self.visible_name} as visible name in Zabbix.")
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
def set_hostgroup(self, hg_format, nb_site_groups, nb_regions):
|
def set_hostgroup(self, hg_format, nb_site_groups, nb_regions):
|
||||||
"""Set the hostgroup for this device"""
|
"""Set the hostgroup for this device"""
|
||||||
# Get all variables from the NB data
|
# Get all variables from the NB data
|
||||||
@ -545,7 +559,12 @@ class NetworkDevice():
|
|||||||
"""
|
"""
|
||||||
Checks if hostname exists in Zabbix.
|
Checks if hostname exists in Zabbix.
|
||||||
"""
|
"""
|
||||||
host = self.zabbix.host.get(filter={'name': self.name}, output=[])
|
# Validate the hostname or visible name field
|
||||||
|
if not self.use_visible_name:
|
||||||
|
zbx_filter = {'host': self.name}
|
||||||
|
else:
|
||||||
|
zbx_filter = {'name': self.visible_name}
|
||||||
|
host = self.zabbix.host.get(filter=zbx_filter, output=[])
|
||||||
return bool(host)
|
return bool(host)
|
||||||
|
|
||||||
def setInterfaceDetails(self):
|
def setInterfaceDetails(self):
|
||||||
@ -610,6 +629,7 @@ class NetworkDevice():
|
|||||||
try:
|
try:
|
||||||
if version.parse(self.zabbix.api_version()) < version.parse("7.0.0"):
|
if version.parse(self.zabbix.api_version()) < version.parse("7.0.0"):
|
||||||
host = self.zabbix.host.create(host=self.name,
|
host = self.zabbix.host.create(host=self.name,
|
||||||
|
name=self.visible_name,
|
||||||
status=self.zabbix_state,
|
status=self.zabbix_state,
|
||||||
interfaces=interfaces,
|
interfaces=interfaces,
|
||||||
groups=groups,
|
groups=groups,
|
||||||
@ -620,6 +640,7 @@ class NetworkDevice():
|
|||||||
inventory=self.inventory)
|
inventory=self.inventory)
|
||||||
else:
|
else:
|
||||||
host = self.zabbix.host.create(host=self.name,
|
host = self.zabbix.host.create(host=self.name,
|
||||||
|
name=self.visible_name,
|
||||||
status=self.zabbix_state,
|
status=self.zabbix_state,
|
||||||
interfaces=interfaces,
|
interfaces=interfaces,
|
||||||
groups=groups,
|
groups=groups,
|
||||||
@ -705,6 +726,14 @@ class NetworkDevice():
|
|||||||
logger.warning(f"Device {self.name}: hostname OUT of sync. "
|
logger.warning(f"Device {self.name}: hostname OUT of sync. "
|
||||||
f"Received value: {host['host']}")
|
f"Received value: {host['host']}")
|
||||||
self.updateZabbixHost(host=self.name)
|
self.updateZabbixHost(host=self.name)
|
||||||
|
# Execute check depending on wether the name is special or not
|
||||||
|
if self.use_visible_name:
|
||||||
|
if host["name"] == self.visible_name:
|
||||||
|
logger.debug(f"Device {self.name}: visible name in-sync.")
|
||||||
|
else:
|
||||||
|
logger.warning(f"Device {self.name}: visible name OUT of sync."
|
||||||
|
f" Received value: {host['name']}")
|
||||||
|
self.updateZabbixHost(name=self.visible_name)
|
||||||
|
|
||||||
# Check if the templates are in-sync
|
# Check if the templates are in-sync
|
||||||
if not self.zbx_template_comparer(host["parentTemplates"]):
|
if not self.zbx_template_comparer(host["parentTemplates"]):
|
||||||
|
Loading…
Reference in New Issue
Block a user