Skip to content

ApiActions: Remove child downtimes recursively#10345

Merged
julianbrost merged 1 commit intomasterfrom
remove-child-downtimes
Mar 21, 2025
Merged

ApiActions: Remove child downtimes recursively#10345
julianbrost merged 1 commit intomasterfrom
remove-child-downtimes

Conversation

@yhabteab
Copy link
Copy Markdown
Member

@yhabteab yhabteab commented Feb 5, 2025

Services downtimes scheduled via the all_services flag get already removed automatically when removing their parent downtimes (introduced with #8913). Now, this commit makes it possible to perform the same actions for all child downtimes, i.e. not only for those of service objects, but for all child objects represented in the dependency tree.

Config

template Host "default" default {
	check_command = "dummy"
	check_interval = 120s
	vars.dummy_text = "I'm just testing something"
	vars.dummy_state = 2
}

object Host "father" {}
object Host "mother" {}
object Host "grandfather" {}
object Host "sister" {}
object Host "brother" {}

apply Dependency "father" to Host {
	parent_host_name = "father"
	redundancy_group = "Parents"
	assign where host.name == "sister" || host.name == "brother"
}

apply Dependency "mother" to Host {
 	parent_host_name = "mother"
 	redundancy_group = "Parents"
 	assign where host.name == "sister" || host.name == "brother"
}

apply Dependency "grandfather" to Host {
 	parent_host_name = "grandfather"
 	redundancy_group = "Grand Parents"
 	assign where host.name == "mother" || host.name == "father"
}
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/downtimes' | jq '.results | length'
0
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/hosts' | jq '.results | length'
5
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/dependencies' | jq '.results | length'
6

Tests

Before:

$ curl -k -s -S -i -u root:icinga -H 'Accept: application/json' \
 -X POST 'https://localhost:5667/v1/actions/schedule-downtime' \
 -d @<(cat <<EOF
{ "type": "Host", "filter": "host.name == \"grandfather\"", "start_time": $(date +%s), "end_time": $(date -v+30M +%s), "child_options": "DowntimeTriggeredChildren", "author": "icingaadmin", "comment": "IPv4 network maintenance", "pretty": true }
EOF
)
HTTP/1.1 200 OK
...
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/downtimes' | jq '.results | length'
5
$ curl -k -s -S -i -u root:icinga -H 'Accept: application/json' \
 -X POST 'https://localhost:5667/v1/actions/remove-downtime' \
 -d '{ "filter": "host.name == \"grandfather\"", "pretty": true, "type": "Host" }'
{
    "results": [
        {
            "code": 200,
            "status": "Successfully removed all downtimes for object 'grandfather' and 0 child downtimes."
        }
    ]
}
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/downtimes' | jq '.results | length'
4

After:

$ curl -k -s -S -i -u root:icinga -H 'Accept: application/json' \
 -X POST 'https://localhost:5667/v1/actions/schedule-downtime' \
 -d @<(cat <<EOF
{ "type": "Host", "filter": "host.name == \"grandfather\"", "start_time": $(date +%s), "end_time": $(date -v+30M +%s), "child_options": "DowntimeTriggeredChildren", "author": "icingaadmin", "comment": "IPv4 network maintenance", "pretty": true }
EOF
)

$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/downtimes' | jq '.results | length'
5
$ curl -k -s -S -i -u root:icinga -H 'Accept: application/json' \
 -X POST 'https://localhost:5667/v1/actions/remove-downtime' \
 -d '{ "filter": "host.name == \"grandfather\"", "pretty": true, "type": "Host" }'
{
    "results": [
        {
            "code": 200,
            "status": "Successfully removed all downtimes for object 'grandfather' and 4 child downtimes."
        }
    ]
}
$ curl -ksSu root:icinga 'https://localhost:5667/v1/objects/downtimes' | jq '.results | length'
0

fixes #10333

@cla-bot cla-bot bot added the cla/signed label Feb 5, 2025
@yhabteab yhabteab added enhancement New feature or request area/api REST API area/runtime Downtimes, comments, dependencies, events and removed area/api REST API labels Feb 5, 2025
@yhabteab yhabteab requested a review from julianbrost February 5, 2025 13:17
@yhabteab yhabteab added this to the 2.14.5 milestone Feb 5, 2025
@yhabteab yhabteab force-pushed the remove-child-downtimes branch from 66b2186 to 56b6f09 Compare February 5, 2025 13:32
@yhabteab yhabteab modified the milestones: 2.14.5, 2.15.0 Feb 5, 2025
@julianbrost
Copy link
Copy Markdown
Member

Now, this commit makes it possible to perform the same actions for all child downtimes

Not only performing the same action but also using the very same code, hence the diff is so small. So this just makes use of the existing parent-tracking mechanism for downtimes. It's probably noteworthy (for the changelog for example) that this will only work for new downtimes.

ScheduledDowntime has an child_options attribute as well that's handled somewhere else:

Downtime::Ptr childDowntime = Downtime::AddDowntime(child, GetAuthor(), GetComment(),
segment.first, segment.second, GetFixed(), trigger, GetDuration(), GetName(), GetName());

Did you intentionally leave that as-is? Maybe due to the following?

if (!config_owner.IsEmpty() && removalReason == DowntimeRemovedByUser) {
BOOST_THROW_EXCEPTION(invalid_downtime_removal_error("Cannot remove downtime '" + downtime->GetName() +
"'. It is owned by scheduled downtime object '" + config_owner + "'"));
}

@yhabteab
Copy link
Copy Markdown
Member Author

Did you intentionally leave that as-is? Maybe due to the following?

No. This is the only the place where downtimes owned by a ScheduledDowntime get removed and includeChildren is set to false.

if (!configOwnerHash.IsEmpty() && configOwnerHash != downtimeOptionsHash)
Downtime::RemoveDowntime(downtime->GetName(), false, DowntimeRemovedByConfigOwner);

Services downtimes scheduled via the `all_services` flag get already
removed automatically when removing their parent downtimes (introduced
with #8913). Now, this commit makes it possible to perform the same actions
for all child downtimes, i.e. not only for those of service objects, but
for all child objects represented in the dependency tree.
@yhabteab yhabteab force-pushed the remove-child-downtimes branch from 56b6f09 to 3d761c0 Compare March 13, 2025 11:13
@julianbrost julianbrost merged commit 0613381 into master Mar 21, 2025
25 checks passed
@julianbrost julianbrost deleted the remove-child-downtimes branch March 21, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Downtimes, comments, dependencies, events cla/signed enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatically delete downtimes for children

3 participants