mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 01:41:25 -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."""
|
||||
|
||||
from os import environ, path, sys
|
||||
from packaging import version
|
||||
import logging
|
||||
import argparse
|
||||
from pynetbox import api
|
||||
@ -83,12 +84,23 @@ def main(arguments):
|
||||
except ZabbixAPIException as e:
|
||||
e = f"Zabbix returned the following error: {str(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
|
||||
netbox_devices = netbox.dcim.devices.filter(**nb_device_filter)
|
||||
netbox_journals = netbox.extras.journal_entries
|
||||
zabbix_groups = zabbix.hostgroup.get(output=['groupid', '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
|
||||
for nb_device in netbox_devices:
|
||||
try:
|
||||
@ -141,11 +153,11 @@ def main(arguments):
|
||||
# Device is already present in Zabbix
|
||||
if(device.zabbix_id):
|
||||
device.ConsistencyCheck(zabbix_groups, zabbix_templates,
|
||||
zabbix_proxys, full_proxy_sync)
|
||||
zabbix_proxies, full_proxy_sync)
|
||||
# Add device to Zabbix
|
||||
else:
|
||||
device.createInZabbix(zabbix_groups, zabbix_templates,
|
||||
zabbix_proxys)
|
||||
zabbix_proxies)
|
||||
except SyncError:
|
||||
pass
|
||||
|
||||
@ -475,7 +487,7 @@ class NetworkDevice():
|
||||
proxy = self.nb.config_context["zabbix"]["proxy"]
|
||||
# Try matching proxy
|
||||
for px in proxy_list:
|
||||
if(px["host"] == proxy):
|
||||
if(px["name"] == proxy):
|
||||
self.zbxproxy = px["proxyid"]
|
||||
logger.debug(f"Found proxy {proxy}"
|
||||
f" for {self.name}.")
|
||||
@ -485,7 +497,7 @@ class NetworkDevice():
|
||||
logger.warning(e)
|
||||
return False
|
||||
|
||||
def createInZabbix(self, groups, templates, proxys,
|
||||
def createInZabbix(self, groups, templates, proxies,
|
||||
description="Host added by Netbox sync script."):
|
||||
"""
|
||||
Creates Zabbix host object with parameters from Netbox object.
|
||||
@ -500,7 +512,7 @@ class NetworkDevice():
|
||||
interfaces = self.setInterfaceDetails()
|
||||
groups = [{"groupid": self.group_id}]
|
||||
# Set Zabbix proxy if defined
|
||||
self.setProxy(proxys)
|
||||
self.setProxy(proxies)
|
||||
# Add host to Zabbix
|
||||
try:
|
||||
host = self.zabbix.host.create(host=self.name,
|
||||
@ -554,13 +566,13 @@ class NetworkDevice():
|
||||
logger.info(f"Updated host {self.name} with data {kwargs}.")
|
||||
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.
|
||||
"""
|
||||
self.getZabbixGroup(groups)
|
||||
self.zbxTemplatePrepper(templates)
|
||||
self.setProxy(proxys)
|
||||
self.setProxy(proxies)
|
||||
host = self.zabbix.host.get(filter={'hostid': self.zabbix_id},
|
||||
selectInterfaces=['type', 'ip',
|
||||
'port', 'details',
|
||||
|
Loading…
Reference in New Issue
Block a user