From 5d4ff9c5edf6662b85c1b3701cb119a01cf11ddc Mon Sep 17 00:00:00 2001 From: TheNetworkGuy Date: Fri, 15 Nov 2024 14:03:42 +0100 Subject: [PATCH] Fixed #79 --- modules/device.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/device.py b/modules/device.py index d96bce5..d1a19a0 100644 --- a/modules/device.py +++ b/modules/device.py @@ -296,10 +296,16 @@ class PhysicalDevice(): """ if self.zabbix_id: try: - self.zabbix.host.delete(self.zabbix_id) - self.nb.custom_fields[device_cf] = None - self.nb.save() - e = f"Host {self.name}: Deleted host from Zabbix." + # Check if the Zabbix host exists in Zabbix + zbx_host = bool(self.zabbix.host.get(filter={'hostid': self.zabbix_id}, + output=[])) + e = (f"Host {self.name}: was already deleted from Zabbix." + " Removed link in Netbox.") + if zbx_host: + # Delete host should it exists + self.zabbix.host.delete(self.zabbix_id) + e = f"Host {self.name}: Deleted host from Zabbix." + self.zeroize_cf() self.logger.info(e) self.create_journal_entry("warning", "Deleted host from Zabbix") except APIRequestError as e: @@ -307,6 +313,12 @@ class PhysicalDevice(): self.logger.error(message) raise SyncExternalError(message) from e + def zeroize_cf(self): + """Sets the hostID custom field in Netbox to zero, + effectively destroying the link""" + self.nb.custom_fields[device_cf] = None + self.nb.save() + def _zabbixHostnameExists(self): """ Checks if hostname exists in Zabbix.