Merge pull request #134 from retigra/issue-130

🐛 Fixes for issue #130
This commit is contained in:
Raymond Kuiper 2025-06-24 16:02:36 +02:00 committed by GitHub
commit 099ebcace5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 15 deletions

View File

@ -135,6 +135,14 @@ class PhysicalDevice:
self.hostgroups = [hg.generate(f) for f in hg_format]
else:
self.hostgroups.append(hg.generate(hg_format))
# Remove duplicates and None values
self.hostgroups = list(filter(None, list(set(self.hostgroups))))
if self.hostgroups:
self.logger.debug(f"Host {self.name}: Should be member "
f"of groups: {self.hostgroups}")
return True
return False
def set_template(self, prefer_config_context, overrule_custom):
"""Set Template"""
@ -177,8 +185,6 @@ class PhysicalDevice:
self.logger.warning(e)
raise TemplateError(e)
def get_templates_context(self):
"""Get Zabbix templates from the device context"""
if "zabbix" not in self.config_context:
@ -298,7 +304,8 @@ class PhysicalDevice:
"name": zbx_template["name"],
}
)
e = f"Host {self.name}: found template {zbx_template['name']}"
e = (f"Host {self.name}: Found template '{zbx_template['name']}' "
f"(ID:{zbx_template['templateid']})")
self.logger.debug(e)
# Return error should the template not be found in Zabbix
if not template_match:
@ -321,7 +328,7 @@ class PhysicalDevice:
if group["name"] == hg:
self.group_ids.append({"groupid": group["groupid"]})
e = (
f"Host {self.name}: matched group "
f"Host {self.name}: Matched group "
f"\"{group['name']}\" (ID:{group['groupid']})"
)
self.logger.debug(e)
@ -503,7 +510,6 @@ class PhysicalDevice:
templateids.append({"templateid": template["templateid"]})
# Set interface, group and template configuration
interfaces = self.setInterfaceDetails()
groups = self.group_ids
# Set Zabbix proxy if defined
self.setProxy(proxies)
# Set basic data for host creation
@ -512,7 +518,7 @@ class PhysicalDevice:
"name": self.visible_name,
"status": self.zabbix_state,
"interfaces": interfaces,
"groups": groups,
"groups": self.group_ids,
"templates": templateids,
"description": description,
"inventory_mode": self.inventory_mode,
@ -541,7 +547,7 @@ class PhysicalDevice:
# Set NetBox custom field to hostID value.
self.nb.custom_fields[config["device_cf"]] = int(self.zabbix_id)
self.nb.save()
msg = f"Host {self.name}: Created host in Zabbix."
msg = f"Host {self.name}: Created host in Zabbix. (ID:{self.zabbix_id})"
self.logger.info(msg)
self.create_journal_entry("success", msg)
else:
@ -941,8 +947,8 @@ class PhysicalDevice:
tmpls_from_zabbix.pop(pos)
succesfull_templates.append(nb_tmpl)
self.logger.debug(
f"Host {self.name}: template "
f"{nb_tmpl['name']} is present in Zabbix."
f"Host {self.name}: Template "
f"'{nb_tmpl['name']}' is present in Zabbix."
)
break
if (

View File

@ -11,6 +11,7 @@ class Hostgroup:
Takes type (vm or dev) and NB object"""
# pylint: disable=too-many-arguments, disable=too-many-positional-arguments
# pylint: disable=logging-fstring-interpolation
def __init__(
self,
obj_type,
@ -93,6 +94,8 @@ class Hostgroup:
format_options["cluster"] = self.nb.cluster.name
format_options["cluster_type"] = self.nb.cluster.type.name
self.format_options = format_options
self.logger.debug(f"Host {self.name}: Resolved properties for use "
f"in hostgroups: {self.format_options}")
def set_nesting(
self, nested_sitegroup_flag, nested_region_flag, nb_groups, nb_regions
@ -135,17 +138,18 @@ class Hostgroup:
hostgroup_value = self.format_options[hg_item]
if hostgroup_value:
hg_output.append(hostgroup_value)
else:
self.logger.info(f"Host {self.name}: Used field '{hg_item}' has no value.")
# Check if the hostgroup is populated with at least one item.
if bool(hg_output):
return "/".join(hg_output)
msg = (
f"Unable to generate hostgroup for host {self.name}."
" Not enough valid items. This is most likely"
" due to the use of custom fields that are empty"
" or an invalid hostgroup format."
f"Host {self.name}: Generating hostgroup name for '{hg_format}' failed. "
f"This is most likely due to fields that have no value."
)
self.logger.error(msg)
raise HostgroupError(msg)
self.logger.warning(msg)
return None
#raise HostgroupError(msg)
def list_formatoptions(self):
"""

View File

@ -212,6 +212,8 @@ def main(arguments):
config["hostgroup_format"], netbox_site_groups, netbox_regions)
# Check if a valid hostgroup has been found for this VM.
if not device.hostgroups:
logger.warning(f"Host {device.name}: Host has no valid "
f"hostgroups, Skipping this host...")
continue
device.set_inventory(nb_device)
device.set_usermacros()