mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-13 15:24:48 -06:00
Added verify of vm_hostgroup_format (moved fucntion to tools.py)
This commit is contained in:
parent
27ee4c341f
commit
77b0798b65
@ -1,5 +1,5 @@
|
|||||||
"""A collection of tools used by several classes"""
|
"""A collection of tools used by several classes"""
|
||||||
|
from modules.exceptions import HostgroupError
|
||||||
|
|
||||||
def convert_recordset(recordset):
|
def convert_recordset(recordset):
|
||||||
"""Converts netbox RedcordSet to list of dicts."""
|
"""Converts netbox RedcordSet to list of dicts."""
|
||||||
@ -99,3 +99,47 @@ def remove_duplicates(input_list, sortkey=None):
|
|||||||
if sortkey and isinstance(sortkey, str):
|
if sortkey and isinstance(sortkey, str):
|
||||||
output_list.sort(key=lambda x: x[sortkey])
|
output_list.sort(key=lambda x: x[sortkey])
|
||||||
return output_list
|
return output_list
|
||||||
|
|
||||||
|
def verify_hg_format(hg_format, hg_type="dev", logger=None):
|
||||||
|
"""
|
||||||
|
Verifies hostgroup field format
|
||||||
|
"""
|
||||||
|
allowed_objects = {"dev": ["location",
|
||||||
|
"rack",
|
||||||
|
"role",
|
||||||
|
"manufacturer",
|
||||||
|
"region",
|
||||||
|
"site",
|
||||||
|
"site_group",
|
||||||
|
"tenant",
|
||||||
|
"tenant_group",
|
||||||
|
"platform",
|
||||||
|
"cluster"]
|
||||||
|
,"vm": ["location",
|
||||||
|
"role",
|
||||||
|
"manufacturer",
|
||||||
|
"region",
|
||||||
|
"site",
|
||||||
|
"site_group",
|
||||||
|
"tenant",
|
||||||
|
"tenant_group",
|
||||||
|
"cluster",
|
||||||
|
"device",
|
||||||
|
"platform"]
|
||||||
|
}
|
||||||
|
hg_objects = []
|
||||||
|
if isinstance(hg_format,list):
|
||||||
|
for f in hg_format:
|
||||||
|
hg_objects = hg_objects + f.split("/")
|
||||||
|
else:
|
||||||
|
hg_objects = hg_format.split("/")
|
||||||
|
hg_objects = sorted(set(hg_objects))
|
||||||
|
for hg_object in hg_objects:
|
||||||
|
if hg_object not in allowed_objects[hg_type]:
|
||||||
|
e = (
|
||||||
|
f"Hostgroup item {hg_object} is not valid. Make sure you"
|
||||||
|
" use valid items and separate them with '/'."
|
||||||
|
)
|
||||||
|
logger.error(e)
|
||||||
|
raise HostgroupError(e)
|
||||||
|
|
||||||
|
@ -13,9 +13,9 @@ from requests.exceptions import ConnectionError as RequestsConnectionError
|
|||||||
from zabbix_utils import APIRequestError, ProcessingError, ZabbixAPI
|
from zabbix_utils import APIRequestError, ProcessingError, ZabbixAPI
|
||||||
|
|
||||||
from modules.device import PhysicalDevice
|
from modules.device import PhysicalDevice
|
||||||
from modules.exceptions import EnvironmentVarError, HostgroupError, SyncError
|
from modules.exceptions import EnvironmentVarError, SyncError
|
||||||
from modules.logging import get_logger, set_log_levels, setup_logger
|
from modules.logging import get_logger, set_log_levels, setup_logger
|
||||||
from modules.tools import convert_recordset, proxy_prepper
|
from modules.tools import convert_recordset, proxy_prepper, verify_hg_format
|
||||||
from modules.virtual_machine import VirtualMachine
|
from modules.virtual_machine import VirtualMachine
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -84,24 +84,6 @@ def main(arguments):
|
|||||||
netbox_token = environ.get("NETBOX_TOKEN")
|
netbox_token = environ.get("NETBOX_TOKEN")
|
||||||
# Set NetBox API
|
# Set NetBox API
|
||||||
netbox = api(netbox_host, token=netbox_token, threading=True)
|
netbox = api(netbox_host, token=netbox_token, threading=True)
|
||||||
# Check if the provided Hostgroup layout is valid
|
|
||||||
hg_objects = []
|
|
||||||
if isinstance(hostgroup_format,list):
|
|
||||||
for l in hostgroup_format:
|
|
||||||
hg_objects = hg_objects + l.split("/")
|
|
||||||
else:
|
|
||||||
hg_objects = hostgroup_format.split("/")
|
|
||||||
hg_objects = sorted(set(hg_objects))
|
|
||||||
allowed_objects = [
|
|
||||||
"location",
|
|
||||||
"role",
|
|
||||||
"manufacturer",
|
|
||||||
"region",
|
|
||||||
"site",
|
|
||||||
"site_group",
|
|
||||||
"tenant",
|
|
||||||
"tenant_group",
|
|
||||||
]
|
|
||||||
# Create API call to get all custom fields which are on the device objects
|
# Create API call to get all custom fields which are on the device objects
|
||||||
try:
|
try:
|
||||||
device_cfs = list(
|
device_cfs = list(
|
||||||
@ -118,14 +100,10 @@ def main(arguments):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
for cf in device_cfs:
|
for cf in device_cfs:
|
||||||
allowed_objects.append(cf.name)
|
allowed_objects.append(cf.name)
|
||||||
for hg_object in hg_objects:
|
# Check if the provided Hostgroup layout is valid
|
||||||
if hg_object not in allowed_objects:
|
verify_hg_format(hostgroup_format, hg_type="dev", logger=logger)
|
||||||
e = (
|
verify_hg_format(vm_hostgroup_format, hg_type="vm", logger=logger)
|
||||||
f"Hostgroup item {hg_object} is not valid. Make sure you"
|
|
||||||
" use valid items and separate them with '/'."
|
|
||||||
)
|
|
||||||
logger.error(e)
|
|
||||||
raise HostgroupError(e)
|
|
||||||
# Set Zabbix API
|
# Set Zabbix API
|
||||||
try:
|
try:
|
||||||
ssl_ctx = ssl.create_default_context()
|
ssl_ctx = ssl.create_default_context()
|
||||||
|
Loading…
Reference in New Issue
Block a user