Fixed #32 and fixed SNMP Bulk bug

This commit is contained in:
TheNetworkGuy 2023-04-11 22:17:30 +02:00
parent fd17b18c9e
commit df77eca944

View File

@ -9,7 +9,8 @@ from pyzabbix import ZabbixAPI, ZabbixAPIException
try: try:
from config import * from config import *
except ModuleNotFoundError: except ModuleNotFoundError:
print(f"Configuration file config.py not found in main directory. Please create the file or rename the config.py.example file to config.py.") print(f"Configuration file config.py not found in main directory."
"Please create the file or rename the config.py.example file to config.py.")
sys.exit(0) sys.exit(0)
# Set logging # Set logging
@ -183,6 +184,7 @@ class NetworkDevice():
self.status = nb.status.label self.status = nb.status.label
self.zabbix = zabbix self.zabbix = zabbix
self.tenant = nb.tenant self.tenant = nb.tenant
self.config_context = nb.config_context
self.hostgroup = "" self.hostgroup = ""
self.zbxproxy = "0" self.zbxproxy = "0"
self.zabbix_state = 0 self.zabbix_state = 0
@ -203,18 +205,7 @@ class NetworkDevice():
logger.warning(e) logger.warning(e)
raise SyncInventoryError(e) raise SyncInventoryError(e)
# Check if device_type has custom field # Check if device has custom field for ZBX ID
device_type_cf = self.nb.device_type.custom_fields
if(template_cf in device_type_cf):
self.template_name = device_type_cf[template_cf]
else:
e = (f"Custom field {template_cf} not "
f"found for {self.nb.device_type.manufacturer.name}"
f" - {self.nb.device_type.display}.")
logger.warning(e)
raise SyncInventoryError(e)
# Check if device has custom field
if(device_cf in self.nb.custom_fields): if(device_cf in self.nb.custom_fields):
self.zabbix_id = self.nb.custom_fields[device_cf] self.zabbix_id = self.nb.custom_fields[device_cf]
else: else:
@ -222,6 +213,24 @@ class NetworkDevice():
logger.warning(e) logger.warning(e)
raise SyncInventoryError(e) raise SyncInventoryError(e)
# Gather device Zabbix templates
device_type_cf = self.nb.device_type.custom_fields
if(templates_config_context):
try:
self.zbx_templates = self.config_context["zabbix"]["templates"]
except KeyError:
e = "Config context for Zabbix template was not found for host {self.name}"
else:
if(template_cf in device_type_cf):
self.template_name = device_type_cf[template_cf]
self.zbx_templates = [device_type_cf[template_cf]]
else:
e = (f"Custom field {template_cf} not "
f"found for {self.nb.device_type.manufacturer.name}"
f" - {self.nb.device_type.display}.")
logger.warning(e)
raise SyncInventoryError(e)
def set_hostgroup(self, format): def set_hostgroup(self, format):
"""Set the hostgroup for this device""" """Set the hostgroup for this device"""
# Get all variables from the NB data # Get all variables from the NB data
@ -309,7 +318,7 @@ class NetworkDevice():
def getZabbixTemplate(self, templates): def getZabbixTemplate(self, templates):
""" """
Returns Zabbix template ID Returns Zabbix template IDs
INPUT: list of templates INPUT: list of templates
OUTPUT: True OUTPUT: True
""" """
@ -682,7 +691,7 @@ class ZabbixInterface():
snmp = self.context["zabbix"]["snmp"] snmp = self.context["zabbix"]["snmp"]
self.interface["details"] = {} self.interface["details"] = {}
# Checks if bulk config has been defined # Checks if bulk config has been defined
if(snmp.get("bulk")): if("bulk" in snmp):
self.interface["details"]["bulk"] = str(snmp.pop("bulk")) self.interface["details"]["bulk"] = str(snmp.pop("bulk"))
else: else:
# Fallback to bulk enabled if not specified # Fallback to bulk enabled if not specified
@ -693,15 +702,15 @@ class ZabbixInterface():
else: else:
e = "SNMP version option is not defined." e = "SNMP version option is not defined."
raise InterfaceConfigError(e) raise InterfaceConfigError(e)
# If version 2 is used, get community string # If version 1 or 2 is used, get community string
if(self.interface["details"]["version"] == '2'): if(self.interface["details"]["version"] in ['1','2']):
if("community" in snmp): if("community" in snmp):
# Set SNMP community to confix context value
community = snmp["community"] community = snmp["community"]
self.interface["details"]["community"] = str(community)
else: else:
e = ("No SNMP community string " # Set SNMP community to default
"defined in custom context.") community = "{$SNMP_COMMUNITY}"
raise InterfaceConfigError(e) self.interface["details"]["community"] = str(community)
# If version 3 has been used, get all # If version 3 has been used, get all
# SNMPv3 Netbox related configs # SNMPv3 Netbox related configs
elif(self.interface["details"]["version"] == '3'): elif(self.interface["details"]["version"] == '3'):