diff --git a/module/netbox/config.py b/module/netbox/config.py index a4f1c89..6faa680 100644 --- a/module/netbox/config.py +++ b/module/netbox/config.py @@ -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. diff --git a/module/netbox/inventory.py b/module/netbox/inventory.py index 951d2b2..34db811 100644 --- a/module/netbox/inventory.py +++ b/module/netbox/inventory.py @@ -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: @@ -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. diff --git a/settings-example.ini b/settings-example.ini index 52a288f..43c5cf6 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -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