Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion module/netbox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ def __init__(self):
ConfigOption("cache_directory_location",
str,
description="The location of the directory where the cache files should be stored",
default_value="cache")
default_value="cache"),

ConfigOption("skip_prune_on_source_failure",
bool,
description="""Safety switch: If any enabled source fails (init/connect/query),
pruning will be skipped for this run to prevent mass orphaning/deletions
during temporary source outages.
""",
default_value=True)
]

super().__init__()
Expand Down
18 changes: 17 additions & 1 deletion netbox-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,23 @@ def main():
# update data in NetBox
nb_handler.update_instance()

# prune orphaned objects from NetBox
# prune orphaned objects from NetBox (safe-guard)
failed_enabled_sources = [
s for s in inventory.source_list
if getattr(getattr(s, "settings", None), "enabled", False) is True
and getattr(s, "init_successful", False) is False
]

if nb_handler.settings.prune_enabled and \
getattr(nb_handler.settings, "skip_prune_on_source_failure", True) and \
len(failed_enabled_sources) > 0:

failed_names = ", ".join([getattr(s, "name", "<unknown>") for s in failed_enabled_sources])
log.warning(
f"Skipping prune because {len(failed_enabled_sources)} enabled source(s) failed init: {failed_names}"
)

else:
nb_handler.prune_data()

# delete tags which are not used anymore
Expand Down
3 changes: 3 additions & 0 deletions settings-example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ host_fqdn = netbox.example.com
;;; a sources name. Sources can be defined multiple times to represent different sources.
;;;

; Safety: If any enabled source fails, skip pruning for this run
;skip_prune_on_source_failure = True

[source/my-vcenter-example]

; Defines if this source is enabled or not
Expand Down
Loading