mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-13 15:24:48 -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
|
# Check if hostgroups match
|
||||||
if (sorted(host[group_dictname], key=itemgetter('groupid')) ==
|
if (sorted(host[group_dictname], key=itemgetter('groupid')) ==
|
||||||
sorted(self.group_ids, 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:
|
else:
|
||||||
self.logger.warning(f"Host {self.name}: hostgroups OUT of sync.")
|
self.logger.warning(f"Host {self.name}: hostgroups OUT of sync.")
|
||||||
self.updateZabbixHost(groups=self.group_ids)
|
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])
|
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):
|
def verify_hg_format(hg_format, device_cfs=[], vm_cfs=[], hg_type="dev", logger=None):
|
||||||
"""
|
"""
|
||||||
Verifies hostgroup field format
|
Verifies hostgroup field format
|
||||||
"""
|
"""
|
||||||
@ -126,7 +126,13 @@ def verify_hg_format(hg_format, hg_type="dev", logger=None):
|
|||||||
"cluster",
|
"cluster",
|
||||||
"device",
|
"device",
|
||||||
"platform"]
|
"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 = []
|
hg_objects = []
|
||||||
if isinstance(hg_format,list):
|
if isinstance(hg_format,list):
|
||||||
for f in hg_format:
|
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 = hg_format.split("/")
|
||||||
hg_objects = sorted(set(hg_objects))
|
hg_objects = sorted(set(hg_objects))
|
||||||
for hg_object in 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 = (
|
e = (
|
||||||
f"Hostgroup item {hg_object} is not valid. Make sure you"
|
f"Hostgroup item {hg_object} is not valid. Make sure you"
|
||||||
" use valid items and separate them with '/'."
|
" use valid items and separate them with '/'."
|
||||||
|
@ -85,9 +85,13 @@ def main(arguments):
|
|||||||
# Set NetBox API
|
# Set NetBox API
|
||||||
netbox = api(netbox_host, token=netbox_token, threading=True)
|
netbox = api(netbox_host, token=netbox_token, threading=True)
|
||||||
# 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
|
||||||
|
device_cfs = []
|
||||||
try:
|
try:
|
||||||
device_cfs = list(
|
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:
|
except RequestsConnectionError:
|
||||||
logger.error(
|
logger.error(
|
||||||
@ -98,12 +102,10 @@ def main(arguments):
|
|||||||
except NBRequestError as e:
|
except NBRequestError as e:
|
||||||
logger.error(f"NetBox error: {e}")
|
logger.error(f"NetBox error: {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
for cf in device_cfs:
|
logger.debug(device_cfs)
|
||||||
allowed_objects.append(cf.name)
|
|
||||||
# Check if the provided Hostgroup layout is valid
|
# Check if the provided Hostgroup layout is valid
|
||||||
verify_hg_format(hostgroup_format, hg_type="dev", logger=logger)
|
verify_hg_format(hostgroup_format, device_cfs=device_cfs, hg_type="dev", logger=logger)
|
||||||
verify_hg_format(vm_hostgroup_format, hg_type="vm", logger=logger)
|
verify_hg_format(vm_hostgroup_format, vm_cfs=vm_cfs, hg_type="vm", logger=logger)
|
||||||
|
|
||||||
# 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