mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-16 12:12:57 -06:00
Fixed proxy issue, rewrite of inventory logic (eval was ugly)
This commit is contained in:
parent
364d376f55
commit
091c9746c0
@ -398,19 +398,22 @@ class NetworkDevice():
|
|||||||
self.inventory_mode = -1
|
self.inventory_mode = -1
|
||||||
self.inventory = {}
|
self.inventory = {}
|
||||||
if inventory_sync:
|
if inventory_sync:
|
||||||
|
# Set inventory mode to automatic or manual
|
||||||
self.inventory_mode = 1 if inventory_automatic else 0
|
self.inventory_mode = 1 if inventory_automatic else 0
|
||||||
|
|
||||||
|
# Let's build an inventory dict for each property in the inventory_map
|
||||||
for nb_inv_field, zbx_inv_field in inventory_map.items():
|
for nb_inv_field, zbx_inv_field in inventory_map.items():
|
||||||
field_list = nb_inv_field.split("/")
|
field_list = nb_inv_field.split("/") # convert str to list based on delimiter
|
||||||
fieldstr = "nbdevice"
|
# start at the base of the dict...
|
||||||
for field in field_list:
|
value = nbdevice
|
||||||
fieldstr += "['" + field + "']"
|
# ... and step through the dict till we find the needed value
|
||||||
try:
|
for item in field_list:
|
||||||
nb_value = eval(fieldstr)
|
value = value[item]
|
||||||
except Exception:
|
# Check if the result is usable and expected
|
||||||
nb_value = None
|
if value and isinstance(value, int | float | str ):
|
||||||
if nb_value and isinstance(nb_value, int | float | str ):
|
self.inventory[zbx_inv_field] = str(value)
|
||||||
self.inventory[zbx_inv_field] = str(nb_value)
|
elif not value:
|
||||||
elif not nb_value:
|
# empty value should just be an empty string for API compatibility
|
||||||
logger.debug(f"Inventory lookup for '{nb_inv_field}' returned an empty value")
|
logger.debug(f"Inventory lookup for '{nb_inv_field}' returned an empty value")
|
||||||
self.inventory[zbx_inv_field] = ""
|
self.inventory[zbx_inv_field] = ""
|
||||||
else:
|
else:
|
||||||
@ -569,11 +572,10 @@ class NetworkDevice():
|
|||||||
logger.debug(f"Found proxy {proxy}"
|
logger.debug(f"Found proxy {proxy}"
|
||||||
f" for {self.name}.")
|
f" for {self.name}.")
|
||||||
return True
|
return True
|
||||||
|
e = f"{self.name}: Defined proxy {proxy} not found."
|
||||||
|
logger.warning(e)
|
||||||
return False
|
return False
|
||||||
e = f"{self.name}: Defined proxy {proxy} not found."
|
return True
|
||||||
logger.warning(e)
|
|
||||||
return False
|
|
||||||
return False
|
|
||||||
|
|
||||||
def createInZabbix(self, groups, templates, proxies,
|
def createInZabbix(self, groups, templates, proxies,
|
||||||
description="Host added by Netbox sync script."):
|
description="Host added by Netbox sync script."):
|
||||||
@ -748,13 +750,17 @@ class NetworkDevice():
|
|||||||
f"with proxy in Zabbix but not in Netbox. The"
|
f"with proxy in Zabbix but not in Netbox. The"
|
||||||
" -p flag was ommited: no "
|
" -p flag was ommited: no "
|
||||||
"changes have been made.")
|
"changes have been made.")
|
||||||
|
|
||||||
# Check host inventory
|
# Check host inventory
|
||||||
if inventory_sync:
|
if inventory_sync:
|
||||||
|
# check inventory mode first, as we need it set to parse
|
||||||
|
# actual inventory values
|
||||||
if str(host['inventory_mode']) == str(self.inventory_mode):
|
if str(host['inventory_mode']) == str(self.inventory_mode):
|
||||||
logger.debug(f"Device {self.name}: inventory_mode in-sync.")
|
logger.debug(f"Device {self.name}: inventory_mode in-sync.")
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Device {self.name}: inventory_mode OUT of sync.")
|
logger.warning(f"Device {self.name}: inventory_mode OUT of sync.")
|
||||||
self.updateZabbixHost(inventory_mode=str(self.inventory_mode))
|
self.updateZabbixHost(inventory_mode=str(self.inventory_mode))
|
||||||
|
# Now we can check if inventory is in-sync.
|
||||||
if host['inventory'] == self.inventory:
|
if host['inventory'] == self.inventory:
|
||||||
logger.debug(f"Device {self.name}: inventory in-sync.")
|
logger.debug(f"Device {self.name}: inventory in-sync.")
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user