mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-13 15:24:48 -06:00
perform hostgroup creation before consistency check
This commit is contained in:
parent
bd4d21c5d8
commit
bfadd88542
@ -85,14 +85,10 @@ 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(
|
# Get NetBox version
|
||||||
netbox.extras.custom_fields.filter(type="text", content_types="dcim.device")
|
nb_version = netbox.version
|
||||||
)
|
logger.debug(f"NetBox version is {nb_version}.")
|
||||||
vm_cfs = list(
|
|
||||||
netbox.extras.custom_fields.filter(type="text", content_types="virtualization.virtualmachine")
|
|
||||||
)
|
|
||||||
except RequestsConnectionError:
|
except RequestsConnectionError:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Unable to connect to NetBox with URL {netbox_host}."
|
f"Unable to connect to NetBox with URL {netbox_host}."
|
||||||
@ -102,10 +98,18 @@ 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)
|
||||||
logger.debug(device_cfs)
|
|
||||||
# Check if the provided Hostgroup layout is valid
|
# Check if the provided Hostgroup layout is valid
|
||||||
|
device_cfs = []
|
||||||
|
vm_cfs = []
|
||||||
|
device_cfs = list(
|
||||||
|
netbox.extras.custom_fields.filter(type="text", content_types="dcim.device")
|
||||||
|
)
|
||||||
verify_hg_format(hostgroup_format, device_cfs=device_cfs, 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, vm_cfs=vm_cfs, hg_type="vm", logger=logger)
|
if sync_vms:
|
||||||
|
vm_cfs = list(
|
||||||
|
netbox.extras.custom_fields.filter(type="text", content_types="virtualization.virtualmachine")
|
||||||
|
)
|
||||||
|
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()
|
||||||
@ -152,9 +156,6 @@ def main(arguments):
|
|||||||
# Prepare list of all proxy and proxy_groups
|
# Prepare list of all proxy and proxy_groups
|
||||||
zabbix_proxy_list = proxy_prepper(zabbix_proxies, zabbix_proxygroups)
|
zabbix_proxy_list = proxy_prepper(zabbix_proxies, zabbix_proxygroups)
|
||||||
|
|
||||||
# Get NetBox API version
|
|
||||||
nb_version = netbox.version
|
|
||||||
|
|
||||||
# Go through all NetBox devices
|
# Go through all NetBox devices
|
||||||
for nb_vm in netbox_vms:
|
for nb_vm in netbox_vms:
|
||||||
try:
|
try:
|
||||||
@ -191,6 +192,14 @@ def main(arguments):
|
|||||||
# Check if the VM is in the disabled state
|
# Check if the VM is in the disabled state
|
||||||
if vm.status in zabbix_device_disable:
|
if vm.status in zabbix_device_disable:
|
||||||
vm.zabbix_state = 1
|
vm.zabbix_state = 1
|
||||||
|
# Add hostgroup if config is set
|
||||||
|
if create_hostgroups:
|
||||||
|
# Create new hostgroup. Potentially multiple groups if nested
|
||||||
|
hostgroups = vm.createZabbixHostgroup(zabbix_groups)
|
||||||
|
# go through all newly created hostgroups
|
||||||
|
for group in hostgroups:
|
||||||
|
# Add new hostgroups to zabbix group list
|
||||||
|
zabbix_groups.append(group)
|
||||||
# Check if VM is already in Zabbix
|
# Check if VM is already in Zabbix
|
||||||
if vm.zabbix_id:
|
if vm.zabbix_id:
|
||||||
vm.ConsistencyCheck(
|
vm.ConsistencyCheck(
|
||||||
@ -201,14 +210,6 @@ def main(arguments):
|
|||||||
create_hostgroups,
|
create_hostgroups,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
# Add hostgroup if config is set
|
|
||||||
if create_hostgroups:
|
|
||||||
# Create new hostgroup. Potentially multiple groups if nested
|
|
||||||
hostgroups = vm.createZabbixHostgroup(zabbix_groups)
|
|
||||||
# go through all newly created hostgroups
|
|
||||||
for group in hostgroups:
|
|
||||||
# Add new hostgroups to zabbix group list
|
|
||||||
zabbix_groups.append(group)
|
|
||||||
# Add VM to Zabbix
|
# Add VM to Zabbix
|
||||||
vm.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list)
|
vm.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list)
|
||||||
except SyncError:
|
except SyncError:
|
||||||
@ -268,6 +269,14 @@ def main(arguments):
|
|||||||
# Check if the device is in the disabled state
|
# Check if the device is in the disabled state
|
||||||
if device.status in zabbix_device_disable:
|
if device.status in zabbix_device_disable:
|
||||||
device.zabbix_state = 1
|
device.zabbix_state = 1
|
||||||
|
# Add hostgroup is config is set
|
||||||
|
if create_hostgroups:
|
||||||
|
# Create new hostgroup. Potentially multiple groups if nested
|
||||||
|
hostgroups = device.createZabbixHostgroup(zabbix_groups)
|
||||||
|
# go through all newly created hostgroups
|
||||||
|
for group in hostgroups:
|
||||||
|
# Add new hostgroups to zabbix group list
|
||||||
|
zabbix_groups.append(group)
|
||||||
# Check if device is already in Zabbix
|
# Check if device is already in Zabbix
|
||||||
if device.zabbix_id:
|
if device.zabbix_id:
|
||||||
device.ConsistencyCheck(
|
device.ConsistencyCheck(
|
||||||
@ -278,14 +287,6 @@ def main(arguments):
|
|||||||
create_hostgroups,
|
create_hostgroups,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
# Add hostgroup is config is set
|
|
||||||
if create_hostgroups:
|
|
||||||
# Create new hostgroup. Potentially multiple groups if nested
|
|
||||||
hostgroups = device.createZabbixHostgroup(zabbix_groups)
|
|
||||||
# go through all newly created hostgroups
|
|
||||||
for group in hostgroups:
|
|
||||||
# Add new hostgroups to zabbix group list
|
|
||||||
zabbix_groups.append(group)
|
|
||||||
# Add device to Zabbix
|
# Add device to Zabbix
|
||||||
device.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list)
|
device.createInZabbix(zabbix_groups, zabbix_templates, zabbix_proxy_list)
|
||||||
except SyncError:
|
except SyncError:
|
||||||
|
Loading…
Reference in New Issue
Block a user