mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-16 04:02:56 -06:00
Futher cleanup
This commit is contained in:
parent
2fcd21a723
commit
c684ac4a9d
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name, logging-not-lazy, too-many-locals, logging-fstring-interpolation
|
||||||
|
|
||||||
|
|
||||||
"""Netbox to Zabbix sync script."""
|
"""Netbox to Zabbix sync script."""
|
||||||
|
|
||||||
@ -46,6 +47,7 @@ logger.setLevel(logging.WARNING)
|
|||||||
|
|
||||||
def main(arguments):
|
def main(arguments):
|
||||||
"""Run the sync process."""
|
"""Run the sync process."""
|
||||||
|
# pylint: disable=too-many-branches, too-many-statements
|
||||||
# set environment variables
|
# set environment variables
|
||||||
if arguments.verbose:
|
if arguments.verbose:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
@ -208,6 +210,7 @@ class TemplateError(SyncError):
|
|||||||
""" Class TemplateError """
|
""" Class TemplateError """
|
||||||
|
|
||||||
class NetworkDevice():
|
class NetworkDevice():
|
||||||
|
# pylint: disable=too-many-instance-attributes
|
||||||
"""
|
"""
|
||||||
Represents Network device.
|
Represents Network device.
|
||||||
INPUT: (Netbox device class, ZabbixAPI class, journal flag, NB journal class)
|
INPUT: (Netbox device class, ZabbixAPI class, journal flag, NB journal class)
|
||||||
@ -332,8 +335,8 @@ class NetworkDevice():
|
|||||||
# Custom field not found, return error
|
# Custom field not found, return error
|
||||||
e = (f"Custom field {template_cf} not "
|
e = (f"Custom field {template_cf} not "
|
||||||
f"found for {self.nb.device_type.manufacturer.name}"
|
f"found for {self.nb.device_type.manufacturer.name}"
|
||||||
f" - {self.nb.device_type.display}.")
|
f" - {self.nb.device_type.display}.")
|
||||||
raise TemplateError(e)
|
raise TemplateError(e) from 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 """
|
||||||
@ -544,7 +547,7 @@ class NetworkDevice():
|
|||||||
except ZabbixAPIException as e:
|
except ZabbixAPIException as e:
|
||||||
e = f"Couldn't add {self.name}, Zabbix returned {str(e)}."
|
e = f"Couldn't add {self.name}, Zabbix returned {str(e)}."
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
raise SyncExternalError(e)
|
raise SyncExternalError(e) from e
|
||||||
# Set Netbox custom field to hostID value.
|
# Set Netbox custom field to hostID value.
|
||||||
self.nb.custom_fields[device_cf] = int(self.zabbix_id)
|
self.nb.custom_fields[device_cf] = int(self.zabbix_id)
|
||||||
self.nb.save()
|
self.nb.save()
|
||||||
@ -580,11 +583,12 @@ class NetworkDevice():
|
|||||||
except ZabbixAPIException as e:
|
except ZabbixAPIException as e:
|
||||||
e = f"Zabbix returned the following error: {str(e)}."
|
e = f"Zabbix returned the following error: {str(e)}."
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
raise SyncExternalError(e)
|
raise SyncExternalError(e) from e
|
||||||
logger.info(f"Updated host {self.name} with data {kwargs}.")
|
logger.info(f"Updated host {self.name} with data {kwargs}.")
|
||||||
self.create_journal_entry("info", f"Updated host in Zabbix with latest NB data.")
|
self.create_journal_entry("info", "Updated host in Zabbix with latest NB data.")
|
||||||
|
|
||||||
def ConsistencyCheck(self, groups, templates, proxies, proxy_power):
|
def ConsistencyCheck(self, groups, templates, proxies, proxy_power):
|
||||||
|
# pylint: disable=too-many-branches, too-many-statements
|
||||||
"""
|
"""
|
||||||
Checks if Zabbix object is still valid with Netbox parameters.
|
Checks if Zabbix object is still valid with Netbox parameters.
|
||||||
"""
|
"""
|
||||||
@ -672,6 +676,7 @@ class NetworkDevice():
|
|||||||
" -p flag was ommited: no "
|
" -p flag was ommited: no "
|
||||||
"changes have been made.")
|
"changes have been made.")
|
||||||
# If only 1 interface has been found
|
# If only 1 interface has been found
|
||||||
|
# pylint: disable=too-many-nested-blocks
|
||||||
if len(host['interfaces']) == 1:
|
if len(host['interfaces']) == 1:
|
||||||
updates = {}
|
updates = {}
|
||||||
# Go through each key / item and check if it matches Zabbix
|
# Go through each key / item and check if it matches Zabbix
|
||||||
@ -812,6 +817,7 @@ class ZabbixInterface():
|
|||||||
|
|
||||||
def set_snmp(self):
|
def set_snmp(self):
|
||||||
""" Check if interface is type SNMP """
|
""" Check if interface is type SNMP """
|
||||||
|
# pylint: disable=too-many-branches
|
||||||
if self.interface["type"] == 2:
|
if self.interface["type"] == 2:
|
||||||
# Checks if SNMP settings are defined in Netbox
|
# Checks if SNMP settings are defined in Netbox
|
||||||
if "snmp" in self.context["zabbix"]:
|
if "snmp" in self.context["zabbix"]:
|
||||||
|
Loading…
Reference in New Issue
Block a user