mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-13 07:24:47 -06:00
Hostgroup CF checks for VMs
This commit is contained in:
parent
298e6c4370
commit
bd4d21c5d8
@ -724,7 +724,7 @@ class PhysicalDevice:
|
||||
# Check if hostgroups match
|
||||
if (sorted(host[group_dictname], key=itemgetter('groupid')) ==
|
||||
sorted(self.group_ids, key=itemgetter('groupid'))):
|
||||
self.logger.debug(f"Host {self.name}: hostgroups in-sync.")
|
||||
self.logger.debug(f"Host {self.name}: hostgroups in-sync.")
|
||||
else:
|
||||
self.logger.warning(f"Host {self.name}: hostgroups OUT of sync.")
|
||||
self.updateZabbixHost(groups=self.group_ids)
|
||||
|
@ -100,7 +100,7 @@ def remove_duplicates(input_list, sortkey=None):
|
||||
output_list.sort(key=lambda x: x[sortkey])
|
||||
return output_list
|
||||
|
||||
def verify_hg_format(hg_format, hg_type="dev", logger=None):
|
||||
def verify_hg_format(hg_format, device_cfs=[], vm_cfs=[], hg_type="dev", logger=None):
|
||||
"""
|
||||
Verifies hostgroup field format
|
||||
"""
|
||||
@ -126,7 +126,13 @@ def verify_hg_format(hg_format, hg_type="dev", logger=None):
|
||||
"cluster",
|
||||
"device",
|
||||
"platform"]
|
||||
,"cfs": {"dev": [], "vm": []}
|
||||
}
|
||||
for cf in device_cfs:
|
||||
allowed_objects['cfs']['dev'].append(cf.name)
|
||||
for cf in vm_cfs:
|
||||
allowed_objects['cfs']['vm'].append(cf.name)
|
||||
logger.debug(allowed_objects)
|
||||
hg_objects = []
|
||||
if isinstance(hg_format,list):
|
||||
for f in hg_format:
|
||||
@ -135,7 +141,8 @@ def verify_hg_format(hg_format, hg_type="dev", logger=None):
|
||||
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]:
|
||||
if (hg_object not in allowed_objects[hg_type] and
|
||||
hg_object not in allowed_objects['cfs'][hg_type]):
|
||||
e = (
|
||||
f"Hostgroup item {hg_object} is not valid. Make sure you"
|
||||
" use valid items and separate them with '/'."
|
||||
|
@ -85,9 +85,13 @@ def main(arguments):
|
||||
# Set NetBox API
|
||||
netbox = api(netbox_host, token=netbox_token, threading=True)
|
||||
# Create API call to get all custom fields which are on the device objects
|
||||
device_cfs = []
|
||||
try:
|
||||
device_cfs = list(
|
||||
netbox.extras.custom_fields.filter(type="text", content_type_id=23)
|
||||
netbox.extras.custom_fields.filter(type="text", content_types="dcim.device")
|
||||
)
|
||||
vm_cfs = list(
|
||||
netbox.extras.custom_fields.filter(type="text", content_types="virtualization.virtualmachine")
|
||||
)
|
||||
except RequestsConnectionError:
|
||||
logger.error(
|
||||
@ -98,12 +102,10 @@ def main(arguments):
|
||||
except NBRequestError as e:
|
||||
logger.error(f"NetBox error: {e}")
|
||||
sys.exit(1)
|
||||
for cf in device_cfs:
|
||||
allowed_objects.append(cf.name)
|
||||
logger.debug(device_cfs)
|
||||
# Check if the provided Hostgroup layout is valid
|
||||
verify_hg_format(hostgroup_format, hg_type="dev", logger=logger)
|
||||
verify_hg_format(vm_hostgroup_format, hg_type="vm", logger=logger)
|
||||
|
||||
verify_hg_format(hostgroup_format, device_cfs=device_cfs, hg_type="dev", logger=logger)
|
||||
verify_hg_format(vm_hostgroup_format, vm_cfs=vm_cfs, hg_type="vm", logger=logger)
|
||||
# Set Zabbix API
|
||||
try:
|
||||
ssl_ctx = ssl.create_default_context()
|
||||
|
Loading…
Reference in New Issue
Block a user