From 23997f9423ce6f36a9bffc137fe4841c4e275ddf Mon Sep 17 00:00:00 2001 From: TheNetworkGuy Date: Wed, 12 Jun 2024 19:02:44 +0200 Subject: [PATCH] Properly styled code as suggested in #61. Also implemented #63 --- modules/device.py | 36 ++++++++++++++++++------------------ modules/interface.py | 20 +++++++++++++++++++- netbox_zabbix_sync.py | 8 ++++---- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/modules/device.py b/modules/device.py index 929b74d..5b16419 100644 --- a/modules/device.py +++ b/modules/device.py @@ -72,7 +72,7 @@ class NetworkDevice(): if device_cf in self.nb.custom_fields: self.zabbix_id = self.nb.custom_fields[device_cf] else: - e = f"Custom field {device_cf} not found for {self.name}." + e = f"Device {self.name}: Custom field {device_cf} not present" self.logger.warning(e) raise SyncInventoryError(e) @@ -273,7 +273,7 @@ class NetworkDevice(): """ # Check if there are templates defined if not self.zbx_template_names: - e = f"No templates found for device {self.name}" + e = f"Device {self.name}: No templates found" self.logger.info(e) raise SyncInventoryError() # Set variable to empty list @@ -290,8 +290,7 @@ class NetworkDevice(): template_match = True self.zbx_templates.append({"templateid": zbx_template['templateid'], "name": zbx_template['name']}) - e = (f"Found template {zbx_template['name']}" - f" for host {self.name}.") + e = f"Device {self.name}: found template {zbx_template['name']}" self.logger.debug(e) # Return error should the template not be found in Zabbix if not template_match: @@ -310,7 +309,7 @@ class NetworkDevice(): for group in groups: if group['name'] == self.hostgroup: self.group_id = group['groupid'] - e = f"Found group {group['name']} for host {self.name}." + e = f"Device {self.name}: matched group {group['name']}" self.logger.debug(e) return True return False @@ -325,7 +324,7 @@ class NetworkDevice(): self.zabbix.host.delete(self.zabbix_id) self.nb.custom_fields[device_cf] = None self.nb.save() - e = f"Deleted host {self.name} from Zabbix." + e = f"Device {self.name}: Deleted host from Zabbix." self.logger.info(e) self.create_journal_entry("warning", "Deleted host from Zabbix") except APIRequestError as e: @@ -395,11 +394,11 @@ class NetworkDevice(): continue # If the proxy name matches if proxy["name"] == proxy_name: + self.logger.debug(f"Device {self.name}: using {proxy['type']}" + f" {proxy_name}") self.zbxproxy = proxy return True - else: - self.logger.warning(f"Device {self.name}: unable to find proxy {proxy_name}") - break + self.logger.warning(f"Device {self.name}: unable to find proxy {proxy_name}") return False def createInZabbix(self, groups, templates, proxies, @@ -450,17 +449,17 @@ class NetworkDevice(): host = self.zabbix.host.create(**create_data) self.zabbix_id = host["hostids"][0] except APIRequestError as e: - e = f"Couldn't add {self.name}, Zabbix returned {str(e)}." + e = f"Device {self.name}: Couldn't create. Zabbix returned {str(e)}." self.logger.error(e) - raise SyncExternalError(e) from e + raise SyncExternalError(e) from None # Set Netbox custom field to hostID value. self.nb.custom_fields[device_cf] = int(self.zabbix_id) self.nb.save() - msg = f"Created host {self.name} in Zabbix." + msg = f"Device {self.name}: Created host in Zabbix." self.logger.info(msg) self.create_journal_entry("success", msg) else: - e = f"Unable to add {self.name} to Zabbix: host already present." + e = f"Device {self.name}: Unable to add to Zabbix. Host already present." self.logger.warning(e) def createZabbixHostgroup(self): @@ -474,7 +473,7 @@ class NetworkDevice(): data = {'groupid': groupid["groupids"][0], 'name': self.hostgroup} return data except APIRequestError as e: - e = f"Couldn't add hostgroup, Zabbix returned {str(e)}." + e = f"Couldn't add hostgroup {self.hostgroup}, Zabbix returned {str(e)}." self.logger.error(e) raise SyncExternalError(e) from e @@ -486,7 +485,8 @@ class NetworkDevice(): try: self.zabbix.host.update(hostid=self.zabbix_id, **kwargs) except APIRequestError as e: - e = f"Zabbix returned the following error: {str(e)}." + e = (f"Device {self.name}: Unable to update. " + f"Zabbix returned the following error: {str(e)}.") self.logger.error(e) raise SyncExternalError(e) from None self.logger.info(f"Updated host {self.name} with data {kwargs}.") @@ -524,7 +524,7 @@ class NetworkDevice(): self.logger.error(e) raise SyncInventoryError(e) if len(host) == 0: - e = (f"No Zabbix host found for {self.name}. " + e = (f"Device {self.name}: No Zabbix host found. " f"This is likely the result of a deleted Zabbix host " f"without zeroing the ID field in Netbox.") self.logger.error(e) @@ -680,7 +680,7 @@ class NetworkDevice(): try: # API call to Zabbix self.zabbix.hostinterface.update(updates) - e = f"Solved {self.name} interface conflict." + e = f"Device {self.name}: solved interface conflict." self.logger.info(e) self.create_journal_entry("info", e) except APIRequestError as e: @@ -715,7 +715,7 @@ class NetworkDevice(): } try: self.nb_journals.create(journal) - self.logger.debug(f"Created journal entry in NB for host {self.name}") + self.logger.debug(f"Device {self.name}: Created journal entry in Netbox") return True except JournalError(e) as e: self.logger.warning("Unable to create journal entry for " diff --git a/modules/interface.py b/modules/interface.py index 17abc5f..54586db 100644 --- a/modules/interface.py +++ b/modules/interface.py @@ -13,12 +13,30 @@ class ZabbixInterface(): self.skelet = {"main": "1", "useip": "1", "dns": "", "ip": self.ip} self.interface = self.skelet + def _set_default_port(self): + """Sets default TCP / UDP port for different interface types""" + interface_mapping = { + 1: 10050, + 2: 161, + 3: 623, + 4: 12345 + } + # Check if interface type is listed in mapper. + if self.interface['type'] not in interface_mapping: + return False + # Set default port to interface + self.interface["port"] = str(interface_mapping[self.interface['type']]) + return True + def get_context(self): """ check if Netbox custom context has been defined. """ if "zabbix" in self.context: zabbix = self.context["zabbix"] - if("interface_type" in zabbix and "interface_port" in zabbix): + if "interface_type" in zabbix: self.interface["type"] = zabbix["interface_type"] + if not "interface_port" in zabbix: + self._set_default_port() + return True self.interface["port"] = zabbix["interface_port"] return True return False diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index 30ce4e3..fd9a9ac 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -140,13 +140,13 @@ def main(arguments): if device.isCluster() and clustering: # Check if device is primary or secondary if device.promoteMasterDevice(): - e = (f"Device {device.name} is " + e = (f"Device {device.name}: is " f"part of cluster and primary.") logger.info(e) else: # Device is secondary in cluster. # Don't continue with this device. - e = (f"Device {device.name} is part of cluster " + e = (f"Device {device.name}: is part of cluster " f"but not primary. Skipping this host...") logger.info(e) continue @@ -156,11 +156,11 @@ def main(arguments): # Delete device from Zabbix # and remove hostID from Netbox. device.cleanup() - logger.info(f"Cleaned up host {device.name}.") + logger.info(f"Device {device.name}: cleanup complete") continue # Device has been added to Netbox # but is not in Activate state - logger.info(f"Skipping host {device.name} since its " + logger.info(f"Device {device.name}: skipping since this device is " f"not in the active state.") continue # Check if the device is in the disabled state