Skip to content

Commit

Permalink
Merge pull request #58 from TheNetworkGuy/hostname_processing
Browse files Browse the repository at this point in the history
Added functionallity to allow device names with specials characters
  • Loading branch information
TheNetworkGuy authored May 22, 2024
2 parents 78b9d5a + 72fde13 commit 4519253
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions netbox_zabbix_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/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."""
Expand Down Expand Up @@ -255,6 +255,7 @@ def __init__(self, nb, zabbix, nb_journal_class, nb_version, journal=None):
self.nb = nb
self.id = nb.id
self.name = nb.name
self.visible_name = None
self.status = nb.status.label
self.zabbix = zabbix
self.zabbix_id = None
Expand Down Expand Up @@ -294,6 +295,19 @@ def _setBasics(self):
logger.warning(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):
"""Set the hostgroup for this device"""
# Get all variables from the NB data
Expand Down Expand Up @@ -544,7 +558,12 @@ def _zabbixHostnameExists(self):
"""
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)

def setInterfaceDetails(self):
Expand Down Expand Up @@ -609,6 +628,7 @@ def createInZabbix(self, groups, templates, proxies,
try:
if version.parse(self.zabbix.api_version()) < version.parse("7.0.0"):
host = self.zabbix.host.create(host=self.name,
name=self.visible_name,
status=self.zabbix_state,
interfaces=interfaces,
groups=groups,
Expand All @@ -619,6 +639,7 @@ def createInZabbix(self, groups, templates, proxies,
inventory=self.inventory)
else:
host = self.zabbix.host.create(host=self.name,
name=self.visible_name,
status=self.zabbix_state,
interfaces=interfaces,
groups=groups,
Expand Down Expand Up @@ -704,6 +725,14 @@ def ConsistencyCheck(self, groups, templates, proxies, proxy_power):
logger.warning(f"Device {self.name}: hostname OUT of sync. "
f"Received value: {host['host']}")
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
if not self.zbx_template_comparer(host["parentTemplates"]):
Expand Down

0 comments on commit 4519253

Please sign in to comment.