mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 01:41:25 -06:00
Added checks for empty list of hostgroups, improved some logging
This commit is contained in:
parent
de82d5ac71
commit
753633e7d2
@ -135,9 +135,14 @@ class PhysicalDevice:
|
|||||||
self.hostgroups = [hg.generate(f) for f in hg_format]
|
self.hostgroups = [hg.generate(f) for f in hg_format]
|
||||||
else:
|
else:
|
||||||
self.hostgroups.append(hg.generate(hg_format))
|
self.hostgroups.append(hg.generate(hg_format))
|
||||||
self.hostgroups = list(set(self.hostgroups))
|
# Remove dyuplicates and None values
|
||||||
self.logger.debug(f"Host {self.name}: Should be member "
|
self.hostgroups = list(filter(None, list(set(self.hostgroups))))
|
||||||
f"of groups: {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):
|
def set_template(self, prefer_config_context, overrule_custom):
|
||||||
"""Set Template"""
|
"""Set Template"""
|
||||||
@ -180,8 +185,6 @@ class PhysicalDevice:
|
|||||||
self.logger.warning(e)
|
self.logger.warning(e)
|
||||||
raise TemplateError(e)
|
raise TemplateError(e)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_templates_context(self):
|
def get_templates_context(self):
|
||||||
"""Get Zabbix templates from the device context"""
|
"""Get Zabbix templates from the device context"""
|
||||||
if "zabbix" not in self.config_context:
|
if "zabbix" not in self.config_context:
|
||||||
@ -301,7 +304,8 @@ class PhysicalDevice:
|
|||||||
"name": zbx_template["name"],
|
"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)
|
self.logger.debug(e)
|
||||||
# Return error should the template not be found in Zabbix
|
# Return error should the template not be found in Zabbix
|
||||||
if not template_match:
|
if not template_match:
|
||||||
@ -324,7 +328,7 @@ class PhysicalDevice:
|
|||||||
if group["name"] == hg:
|
if group["name"] == hg:
|
||||||
self.group_ids.append({"groupid": group["groupid"]})
|
self.group_ids.append({"groupid": group["groupid"]})
|
||||||
e = (
|
e = (
|
||||||
f"Host {self.name}: matched group "
|
f"Host {self.name}: Matched group "
|
||||||
f"\"{group['name']}\" (ID:{group['groupid']})"
|
f"\"{group['name']}\" (ID:{group['groupid']})"
|
||||||
)
|
)
|
||||||
self.logger.debug(e)
|
self.logger.debug(e)
|
||||||
@ -506,7 +510,6 @@ class PhysicalDevice:
|
|||||||
templateids.append({"templateid": template["templateid"]})
|
templateids.append({"templateid": template["templateid"]})
|
||||||
# Set interface, group and template configuration
|
# Set interface, group and template configuration
|
||||||
interfaces = self.setInterfaceDetails()
|
interfaces = self.setInterfaceDetails()
|
||||||
groups = self.group_ids
|
|
||||||
# Set Zabbix proxy if defined
|
# Set Zabbix proxy if defined
|
||||||
self.setProxy(proxies)
|
self.setProxy(proxies)
|
||||||
# Set basic data for host creation
|
# Set basic data for host creation
|
||||||
@ -515,7 +518,7 @@ class PhysicalDevice:
|
|||||||
"name": self.visible_name,
|
"name": self.visible_name,
|
||||||
"status": self.zabbix_state,
|
"status": self.zabbix_state,
|
||||||
"interfaces": interfaces,
|
"interfaces": interfaces,
|
||||||
"groups": groups,
|
"groups": self.group_ids,
|
||||||
"templates": templateids,
|
"templates": templateids,
|
||||||
"description": description,
|
"description": description,
|
||||||
"inventory_mode": self.inventory_mode,
|
"inventory_mode": self.inventory_mode,
|
||||||
@ -544,7 +547,7 @@ class PhysicalDevice:
|
|||||||
# Set NetBox custom field to hostID value.
|
# Set NetBox custom field to hostID value.
|
||||||
self.nb.custom_fields[config["device_cf"]] = int(self.zabbix_id)
|
self.nb.custom_fields[config["device_cf"]] = int(self.zabbix_id)
|
||||||
self.nb.save()
|
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.logger.info(msg)
|
||||||
self.create_journal_entry("success", msg)
|
self.create_journal_entry("success", msg)
|
||||||
else:
|
else:
|
||||||
@ -944,8 +947,8 @@ class PhysicalDevice:
|
|||||||
tmpls_from_zabbix.pop(pos)
|
tmpls_from_zabbix.pop(pos)
|
||||||
succesfull_templates.append(nb_tmpl)
|
succesfull_templates.append(nb_tmpl)
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"Host {self.name}: template "
|
f"Host {self.name}: Template "
|
||||||
f"{nb_tmpl['name']} is present in Zabbix."
|
f"'{nb_tmpl['name']}' is present in Zabbix."
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
if (
|
if (
|
||||||
|
@ -93,6 +93,7 @@ class Hostgroup:
|
|||||||
format_options["cluster"] = self.nb.cluster.name
|
format_options["cluster"] = self.nb.cluster.name
|
||||||
format_options["cluster_type"] = self.nb.cluster.type.name
|
format_options["cluster_type"] = self.nb.cluster.type.name
|
||||||
self.format_options = format_options
|
self.format_options = format_options
|
||||||
|
self.logger.debug(f"Host {self.name}: Resolved properties for use in hostgroups: {self.format_options}")
|
||||||
|
|
||||||
def set_nesting(
|
def set_nesting(
|
||||||
self, nested_sitegroup_flag, nested_region_flag, nb_groups, nb_regions
|
self, nested_sitegroup_flag, nested_region_flag, nb_groups, nb_regions
|
||||||
@ -135,17 +136,18 @@ class Hostgroup:
|
|||||||
hostgroup_value = self.format_options[hg_item]
|
hostgroup_value = self.format_options[hg_item]
|
||||||
if hostgroup_value:
|
if hostgroup_value:
|
||||||
hg_output.append(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.
|
# Check if the hostgroup is populated with at least one item.
|
||||||
if bool(hg_output):
|
if bool(hg_output):
|
||||||
return "/".join(hg_output)
|
return "/".join(hg_output)
|
||||||
msg = (
|
msg = (
|
||||||
f"Unable to generate hostgroup for host {self.name}."
|
f"Host {self.name}: Generating hostgroup name for '{hg_format}' failed. "
|
||||||
" Not enough valid items. This is most likely"
|
f"This is most likely due to fields that have no value."
|
||||||
" due to the use of custom fields that are empty"
|
|
||||||
" or an invalid hostgroup format."
|
|
||||||
)
|
)
|
||||||
self.logger.error(msg)
|
self.logger.warning(msg)
|
||||||
raise HostgroupError(msg)
|
return None
|
||||||
|
#raise HostgroupError(msg)
|
||||||
|
|
||||||
def list_formatoptions(self):
|
def list_formatoptions(self):
|
||||||
"""
|
"""
|
||||||
|
@ -212,6 +212,8 @@ def main(arguments):
|
|||||||
config["hostgroup_format"], netbox_site_groups, netbox_regions)
|
config["hostgroup_format"], netbox_site_groups, netbox_regions)
|
||||||
# Check if a valid hostgroup has been found for this VM.
|
# Check if a valid hostgroup has been found for this VM.
|
||||||
if not device.hostgroups:
|
if not device.hostgroups:
|
||||||
|
logger.warning(f"Host {device.name}: Host has no valid "
|
||||||
|
f"hostgroups, Skipping this host...")
|
||||||
continue
|
continue
|
||||||
device.set_inventory(nb_device)
|
device.set_inventory(nb_device)
|
||||||
device.set_usermacros()
|
device.set_usermacros()
|
||||||
|
Loading…
Reference in New Issue
Block a user