Update netbox_zabbix_sync.py

Fixed variable path and changed spaces in map to underscores.

Forced strings when building the hg_format.
This commit is contained in:
David Mc Ken 2021-11-01 20:47:32 -04:00
parent 47ef3c6eb5
commit 0d0f937f52

View File

@ -2,6 +2,7 @@
"""Netbox to Zabbix sync script.""" """Netbox to Zabbix sync script."""
from os import environ, path from os import environ, path
import functools
import logging import logging
import argparse import argparse
from pynetbox import api from pynetbox import api
@ -70,7 +71,7 @@ def main(arguments):
# Go through all Netbox devices # Go through all Netbox devices
for nb_device in netbox_devices: for nb_device in netbox_devices:
try: try:
device = NetworkDevice(nb_device, zabbix, hg_spec=hg_spec) device = NetworkDevice(nb_device, zabbix, hg_spec=zabbix_hg_spec)
# Checks if device is part of cluster. # Checks if device is part of cluster.
# Requires the cluster argument. # Requires the cluster argument.
if(device.isCluster() and arguments.cluster): if(device.isCluster() and arguments.cluster):
@ -177,21 +178,20 @@ class NetworkDevice():
INPUT: (Netbox device class, ZabbixAPI class) INPUT: (Netbox device class, ZabbixAPI class)
""" """
_hg_map = { _hg_map = {
'cluster': 'cluster.name', 'cluster': 'cluster.name',
#'cluster group': 'cluster.name', #'cluster_group': 'cluster.name',
'device type': 'device_type.model', 'device_type': 'device_type.model',
'location': 'location.name', 'location': 'location.name',
'manufacturer': 'device_type.manufacturer.name', 'manufacturer': 'device_type.manufacturer.name',
'platform': 'platform.name', 'platform': 'platform.name',
'rack': 'rack.name', 'rack': 'rack.name',
'role': 'device_role.name', 'role': 'device_role.name',
'site': 'site.name', 'site': 'site.name',
#'site group': 'site.name', #'site_group': 'site.name',
'tenant': 'tenant.name', 'tenant': 'tenant.name',
#'tenant group': self.nb.site.name, #'tenant_group': 'site.name',
} }
def __init__(self, nb, zabbix, hg_spec = None): def __init__(self, nb, zabbix, hg_spec = None):
self.nb = nb self.nb = nb
self.id = nb.id self.id = nb.id
@ -208,7 +208,7 @@ class NetworkDevice():
for curr_part in parts: for curr_part in parts:
if curr_part in self._hg_map: if curr_part in self._hg_map:
try: try:
self.hg_format.append(rgetattr(self.nb), curr_part) self.hg_format.append(str(rgetattr(self.nb, curr_part)))
except AttributeError: except AttributeError:
logger.error("Value of '{0}' is not set on host '{1}'".\ logger.error("Value of '{0}' is not set on host '{1}'".\
format(curr_part, self.name)) format(curr_part, self.name))