mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 01:41:25 -06:00
Fixed #10
This commit is contained in:
parent
ea680aa1f5
commit
55e28a7095
@ -47,13 +47,7 @@ def main(arguments):
|
|||||||
zabbix_pass = environ.get("ZABBIX_PASS")
|
zabbix_pass = environ.get("ZABBIX_PASS")
|
||||||
netbox_host = environ.get("NETBOX_HOST")
|
netbox_host = environ.get("NETBOX_HOST")
|
||||||
netbox_token = environ.get("NETBOX_TOKEN")
|
netbox_token = environ.get("NETBOX_TOKEN")
|
||||||
netbox_key = environ.get("NETBOX_KEY")
|
|
||||||
|
|
||||||
if(arguments.secret and not netbox_key):
|
|
||||||
e = ("You need a private user key to"
|
|
||||||
" use the Netbox secrets functionality.")
|
|
||||||
logger.warning(e)
|
|
||||||
EnvironmentVarError(e)
|
|
||||||
# Set Zabbix API
|
# Set Zabbix API
|
||||||
try:
|
try:
|
||||||
zabbix = ZabbixAPI(zabbix_host)
|
zabbix = ZabbixAPI(zabbix_host)
|
||||||
@ -62,8 +56,7 @@ def main(arguments):
|
|||||||
e = f"Zabbix returned the following error: {str(e)}."
|
e = f"Zabbix returned the following error: {str(e)}."
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
# Set Netbox API
|
# Set Netbox API
|
||||||
netbox = api(netbox_host, token=netbox_token,
|
netbox = api(netbox_host, token=netbox_token, threading=True)
|
||||||
private_key=netbox_key, threading=True)
|
|
||||||
# Get all Zabbix and Netbox data
|
# Get all Zabbix and Netbox data
|
||||||
netbox_devices = netbox.dcim.devices.all()
|
netbox_devices = netbox.dcim.devices.all()
|
||||||
zabbix_groups = zabbix.hostgroup.get(output=['name'])
|
zabbix_groups = zabbix.hostgroup.get(output=['name'])
|
||||||
@ -98,9 +91,6 @@ def main(arguments):
|
|||||||
else:
|
else:
|
||||||
logger.debug(f"{device.name} is not linked to a tenant. "
|
logger.debug(f"{device.name} is not linked to a tenant. "
|
||||||
f"Using HG format '{device.hostgroup}'.")
|
f"Using HG format '{device.hostgroup}'.")
|
||||||
# -s flag: collect secrets from this device
|
|
||||||
if(arguments.secret):
|
|
||||||
device.getNetboxSecrets(netbox)
|
|
||||||
# Checks if device is in cleanup state
|
# Checks if device is in cleanup state
|
||||||
if(device.status != "Active"):
|
if(device.status != "Active"):
|
||||||
if(device.zabbix_id):
|
if(device.zabbix_id):
|
||||||
@ -180,7 +170,6 @@ class NetworkDevice():
|
|||||||
self.zabbix = zabbix
|
self.zabbix = zabbix
|
||||||
self.tenant = nb.tenant
|
self.tenant = nb.tenant
|
||||||
self.hostgroup = None
|
self.hostgroup = None
|
||||||
self.secrets = None
|
|
||||||
self.zbxproxy = "0"
|
self.zbxproxy = "0"
|
||||||
self.hg_format = [self.nb.site.name,
|
self.hg_format = [self.nb.site.name,
|
||||||
self.nb.device_type.manufacturer.name,
|
self.nb.device_type.manufacturer.name,
|
||||||
@ -262,19 +251,6 @@ class NetworkDevice():
|
|||||||
logger.debug(f"Device {self.name} is non-primary cluster member.")
|
logger.debug(f"Device {self.name} is non-primary cluster member.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def getNetboxSecrets(self, nbapi):
|
|
||||||
"""
|
|
||||||
Get secrets from this Netbox host
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self.secrets = nbapi.secrets.secrets.filter(device=self.nb.name)
|
|
||||||
if(self.secrets):
|
|
||||||
logger.debug(f"Got {len(self.secrets)} secret(s)"
|
|
||||||
f" for host {self.name}.")
|
|
||||||
except NetboxRequestError as e:
|
|
||||||
e = f"Device {self.name}: unable to get Netbox secrets, error: {e}"
|
|
||||||
logger.warning(e)
|
|
||||||
|
|
||||||
def getZabbixTemplate(self, templates):
|
def getZabbixTemplate(self, templates):
|
||||||
"""
|
"""
|
||||||
Returns Zabbix template ID
|
Returns Zabbix template ID
|
||||||
@ -352,8 +328,7 @@ class NetworkDevice():
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Initiate interface class
|
# Initiate interface class
|
||||||
interface = ZabbixInterface(self.nb.config_context,
|
interface = ZabbixInterface(self.nb.config_context, self.ip)
|
||||||
self.ip, self.secrets)
|
|
||||||
# Check if Netbox has device context.
|
# Check if Netbox has device context.
|
||||||
# If not fall back to old config.
|
# If not fall back to old config.
|
||||||
if(interface.get_context()):
|
if(interface.get_context()):
|
||||||
@ -589,9 +564,8 @@ class NetworkDevice():
|
|||||||
|
|
||||||
|
|
||||||
class ZabbixInterface():
|
class ZabbixInterface():
|
||||||
def __init__(self, context, ip, secrets=None):
|
def __init__(self, context, ip):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.secrets = secrets
|
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.skelet = {"main": "1", "useip": "1", "dns": "", "ip": self.ip}
|
self.skelet = {"main": "1", "useip": "1", "dns": "", "ip": self.ip}
|
||||||
self.interface = self.skelet
|
self.interface = self.skelet
|
||||||
@ -615,15 +589,6 @@ class ZabbixInterface():
|
|||||||
# 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"]):
|
||||||
snmp = self.context["zabbix"]["snmp"]
|
snmp = self.context["zabbix"]["snmp"]
|
||||||
# Check if matching SNMP Netbox secret is found for host.
|
|
||||||
supported_secrets = ["community", "authpassphrase",
|
|
||||||
"privpassphrase"]
|
|
||||||
# Check if Netbox host has secrets
|
|
||||||
if(self.secrets):
|
|
||||||
for secret in self.secrets:
|
|
||||||
# If secret is supported, add to SNMP details
|
|
||||||
if(secret.name in supported_secrets):
|
|
||||||
snmp[secret.name] = secret.plaintext
|
|
||||||
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(snmp.get("bulk")):
|
||||||
@ -692,8 +657,6 @@ if(__name__ == "__main__"):
|
|||||||
parser.add_argument("-t", "--tenant", action="store_true",
|
parser.add_argument("-t", "--tenant", action="store_true",
|
||||||
help=("Add Tenant name to the Zabbix "
|
help=("Add Tenant name to the Zabbix "
|
||||||
"hostgroup name scheme."))
|
"hostgroup name scheme."))
|
||||||
parser.add_argument("-s", "--secret", action="store_true",
|
|
||||||
help=("Use Netbox secrets for SNMP interfaces."))
|
|
||||||
parser.add_argument("-p", "--proxy_power", action="store_true",
|
parser.add_argument("-p", "--proxy_power", action="store_true",
|
||||||
help=("USE WITH CAUTION. If there is a proxy "
|
help=("USE WITH CAUTION. If there is a proxy "
|
||||||
"configured in Zabbix but not in Netbox, sync "
|
"configured in Zabbix but not in Netbox, sync "
|
||||||
|
Loading…
Reference in New Issue
Block a user