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
24 changes: 24 additions & 0 deletions module/netbox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ def __init__(self):
""",
default_value=False),

ConfigOption("orphaned_device_status",
str,
description="""Set the NetBox status of orphaned devices to this value.
If unset, the status will not be changed when a device is orphaned.
The status must be a valid device status in NetBox (e.g. 'decommissioning',
'offline', 'planned').
Note: only devices which support a 'status' field will be affected.
When an orphaned object reappears in a source, the status will be
restored to 'active'.
""",
config_example="decommissioning"),

ConfigOption("orphaned_vm_status",
str,
description="""Set the NetBox status of orphaned virtual machines to this value.
If unset, the status will not be changed when a VM is orphaned.
The status must be a valid VM status in NetBox (e.g. 'decommissioning',
'offline', 'planned').
Note: only VMs which support a 'status' field will be affected.
When an orphaned object reappears in a source, the status will be
restored to 'active'.
""",
config_example="decommissioning"),

ConfigOption("default_netbox_result_limit",
int,
description="""The maximum number of objects returned in a single request.
Expand Down
26 changes: 26 additions & 0 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ def tag_all_the_things(self, netbox_handler):
if netbox_handler.orphaned_tag in this_object_tags:
this_object.remove_tags(netbox_handler.orphaned_tag)

# restore status to active if it was changed during orphaning
if isinstance(this_object, (NBDevice, NBVM)):
current_status = grab(this_object, "data.status")
if isinstance(current_status, dict):
current_status = current_status.get("value")
if current_status is not None and current_status != "active":
this_object.update(data={"status": "active"})

# if object was tagged by this program in previous runs but is not present
# anymore then add the orphaned tag except it originated from a disabled source
else:
Expand Down Expand Up @@ -390,6 +398,24 @@ def tag_all_the_things(self, netbox_handler):

this_object.add_tags(netbox_handler.orphaned_tag)

# set orphaned status on devices/VMs if configured
# and pruning is enabled (if a source was unavailable,
# pruning is disabled to prevent false orphaning)
if netbox_handler.settings.prune_enabled is True:
if isinstance(this_object, NBDevice):
orphaned_status = netbox_handler.settings.orphaned_device_status
elif isinstance(this_object, NBVM):
orphaned_status = netbox_handler.settings.orphaned_vm_status
else:
orphaned_status = None

if orphaned_status:
current_status = grab(this_object, "data.status")
if isinstance(current_status, dict):
current_status = current_status.get("value")
if current_status != orphaned_status:
this_object.update(data={"status": orphaned_status})

def query_ptr_records_for_all_ips(self):
"""
Perform a DNS lookup for all IP address of a certain source if desired.
Expand Down
12 changes: 12 additions & 0 deletions settings-example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ host_fqdn = netbox.example.com
; Ricardo/netbox-sync/issues/176)
;ignore_unknown_source_object_pruning = False

; Set the NetBox status of orphaned devices to this value. If unset the status
; will not be changed. Must be a valid device status (e.g. 'decommissioning',
; 'offline', 'planned'). When an orphaned device reappears in a source, the
; status will be restored to 'active'.
;orphaned_device_status = decommissioning

; Set the NetBox status of orphaned VMs to this value. If unset the status
; will not be changed. Must be a valid VM status (e.g. 'decommissioning',
; 'offline', 'planned'). When an orphaned VM reappears in a source, the
; status will be restored to 'active'.
;orphaned_vm_status = decommissioning

; The maximum number of objects returned in a single request. If a NetBox instance is very
; quick responding the value should be raised
;default_netbox_result_limit = 200
Expand Down