mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 09:51:26 -06:00
Update in Zabbix proxy logic
- Fixed var typo (`proxys` vs `proxies`) - Workaround for Zabbix 7.0.x API changes in `proxy.get()`: `host` was replaced with `name`.
This commit is contained in:
parent
e24351aea2
commit
8aba95525b
@ -2,6 +2,7 @@
|
|||||||
"""Netbox to Zabbix sync script."""
|
"""Netbox to Zabbix sync script."""
|
||||||
|
|
||||||
from os import environ, path, sys
|
from os import environ, path, sys
|
||||||
|
from packaging import version
|
||||||
import logging
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
from pynetbox import api
|
from pynetbox import api
|
||||||
@ -83,12 +84,23 @@ def main(arguments):
|
|||||||
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)
|
||||||
|
# Set API parameter mapping based on API version
|
||||||
|
if version.parse(zabbix.api_version()) < version.parse("7.0.0"):
|
||||||
|
proxy_name = "host"
|
||||||
|
else:
|
||||||
|
proxy_name = "name"
|
||||||
# Get all Zabbix and Netbox data
|
# Get all Zabbix and Netbox data
|
||||||
netbox_devices = netbox.dcim.devices.filter(**nb_device_filter)
|
netbox_devices = netbox.dcim.devices.filter(**nb_device_filter)
|
||||||
netbox_journals = netbox.extras.journal_entries
|
netbox_journals = netbox.extras.journal_entries
|
||||||
zabbix_groups = zabbix.hostgroup.get(output=['groupid', 'name'])
|
zabbix_groups = zabbix.hostgroup.get(output=['groupid', 'name'])
|
||||||
zabbix_templates = zabbix.template.get(output=['templateid', 'name'])
|
zabbix_templates = zabbix.template.get(output=['templateid', 'name'])
|
||||||
zabbix_proxys = zabbix.proxy.get(output=['proxyid', 'host'])
|
zabbix_proxies = zabbix.proxy.get(output=['proxyid', proxy_name])
|
||||||
|
|
||||||
|
# Sanitize data
|
||||||
|
if proxy_name == "host":
|
||||||
|
for proxy in zabbix_proxies:
|
||||||
|
proxy['name'] = proxy.pop('host')
|
||||||
|
|
||||||
# Go through all Netbox devices
|
# Go through all Netbox devices
|
||||||
for nb_device in netbox_devices:
|
for nb_device in netbox_devices:
|
||||||
try:
|
try:
|
||||||
@ -141,11 +153,11 @@ def main(arguments):
|
|||||||
# Device is already present in Zabbix
|
# Device is already present in Zabbix
|
||||||
if(device.zabbix_id):
|
if(device.zabbix_id):
|
||||||
device.ConsistencyCheck(zabbix_groups, zabbix_templates,
|
device.ConsistencyCheck(zabbix_groups, zabbix_templates,
|
||||||
zabbix_proxys, full_proxy_sync)
|
zabbix_proxies, full_proxy_sync)
|
||||||
# Add device to Zabbix
|
# Add device to Zabbix
|
||||||
else:
|
else:
|
||||||
device.createInZabbix(zabbix_groups, zabbix_templates,
|
device.createInZabbix(zabbix_groups, zabbix_templates,
|
||||||
zabbix_proxys)
|
zabbix_proxies)
|
||||||
except SyncError:
|
except SyncError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -475,7 +487,7 @@ class NetworkDevice():
|
|||||||
proxy = self.nb.config_context["zabbix"]["proxy"]
|
proxy = self.nb.config_context["zabbix"]["proxy"]
|
||||||
# Try matching proxy
|
# Try matching proxy
|
||||||
for px in proxy_list:
|
for px in proxy_list:
|
||||||
if(px["host"] == proxy):
|
if(px["name"] == proxy):
|
||||||
self.zbxproxy = px["proxyid"]
|
self.zbxproxy = px["proxyid"]
|
||||||
logger.debug(f"Found proxy {proxy}"
|
logger.debug(f"Found proxy {proxy}"
|
||||||
f" for {self.name}.")
|
f" for {self.name}.")
|
||||||
@ -485,7 +497,7 @@ class NetworkDevice():
|
|||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def createInZabbix(self, groups, templates, proxys,
|
def createInZabbix(self, groups, templates, proxies,
|
||||||
description="Host added by Netbox sync script."):
|
description="Host added by Netbox sync script."):
|
||||||
"""
|
"""
|
||||||
Creates Zabbix host object with parameters from Netbox object.
|
Creates Zabbix host object with parameters from Netbox object.
|
||||||
@ -500,7 +512,7 @@ class NetworkDevice():
|
|||||||
interfaces = self.setInterfaceDetails()
|
interfaces = self.setInterfaceDetails()
|
||||||
groups = [{"groupid": self.group_id}]
|
groups = [{"groupid": self.group_id}]
|
||||||
# Set Zabbix proxy if defined
|
# Set Zabbix proxy if defined
|
||||||
self.setProxy(proxys)
|
self.setProxy(proxies)
|
||||||
# Add host to Zabbix
|
# Add host to Zabbix
|
||||||
try:
|
try:
|
||||||
host = self.zabbix.host.create(host=self.name,
|
host = self.zabbix.host.create(host=self.name,
|
||||||
@ -554,13 +566,13 @@ class NetworkDevice():
|
|||||||
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", f"Updated host in Zabbix with latest NB data.")
|
||||||
|
|
||||||
def ConsistencyCheck(self, groups, templates, proxys, proxy_power):
|
def ConsistencyCheck(self, groups, templates, proxies, proxy_power):
|
||||||
"""
|
"""
|
||||||
Checks if Zabbix object is still valid with Netbox parameters.
|
Checks if Zabbix object is still valid with Netbox parameters.
|
||||||
"""
|
"""
|
||||||
self.getZabbixGroup(groups)
|
self.getZabbixGroup(groups)
|
||||||
self.zbxTemplatePrepper(templates)
|
self.zbxTemplatePrepper(templates)
|
||||||
self.setProxy(proxys)
|
self.setProxy(proxies)
|
||||||
host = self.zabbix.host.get(filter={'hostid': self.zabbix_id},
|
host = self.zabbix.host.get(filter={'hostid': self.zabbix_id},
|
||||||
selectInterfaces=['type', 'ip',
|
selectInterfaces=['type', 'ip',
|
||||||
'port', 'details',
|
'port', 'details',
|
||||||
|
Loading…
Reference in New Issue
Block a user