diff --git a/modules/device.py b/modules/device.py index ee8b666..26bac0e 100644 --- a/modules/device.py +++ b/modules/device.py @@ -135,9 +135,14 @@ class PhysicalDevice: self.hostgroups = [hg.generate(f) for f in hg_format] else: self.hostgroups.append(hg.generate(hg_format)) - self.hostgroups = list(set(self.hostgroups)) - self.logger.debug(f"Host {self.name}: Should be member " - f"of groups: {self.hostgroups}") + # Remove dyuplicates 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""" @@ -180,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: @@ -301,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: @@ -324,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) @@ -506,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 @@ -515,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, @@ -544,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: @@ -944,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 ( diff --git a/modules/hostgroups.py b/modules/hostgroups.py index 8bbec86..cad31e1 100644 --- a/modules/hostgroups.py +++ b/modules/hostgroups.py @@ -93,6 +93,7 @@ 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 in hostgroups: {self.format_options}") def set_nesting( self, nested_sitegroup_flag, nested_region_flag, nb_groups, nb_regions @@ -135,17 +136,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): """ diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index d9ff71b..1515041 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -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()