Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail to remove obsolete downtimes #10062

Merged
merged 3 commits into from
May 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions lib/icinga/apiactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
childCount += downtime->GetChildren().size();

try {
Downtime::RemoveDowntime(downtime->GetName(), true, true, false, author);
Downtime::RemoveDowntime(downtime->GetName(), true, DowntimeRemovedByUser, author);
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ApiActions") << error.what();

Expand All @@ -578,7 +578,7 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,

try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, true, true, false, author);
Downtime::RemoveDowntime(downtimeName, true, DowntimeRemovedByUser, author);

return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName +
"' and " + std::to_string(childCount) + " child downtimes.");
Expand Down
32 changes: 19 additions & 13 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ Downtime::Ptr Downtime::AddDowntime(const Checkable::Ptr& checkable, const Strin
return downtime;
}

void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cancelled, bool expired,
void Downtime::RemoveDowntime(const String& id, bool includeChildren, DowntimeRemovalReason removalReason,
const String& removedBy, const MessageOrigin::Ptr& origin)
{
Downtime::Ptr downtime = Downtime::GetByName(id);
Expand All @@ -369,18 +369,18 @@ void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cance

String config_owner = downtime->GetConfigOwner();

if (!config_owner.IsEmpty() && !expired) {
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 + "'"));
}

if (includeChildren) {
for (const Downtime::Ptr& child : downtime->GetChildren()) {
Downtime::RemoveDowntime(child->GetName(), true, true);
Downtime::RemoveDowntime(child->GetName(), true, removalReason, removedBy);
}
}

if (cancelled) {
if (removalReason != DowntimeExpired) {
downtime->SetRemovalInfo(removedBy, Utility::GetTime());
}

Expand All @@ -396,13 +396,19 @@ void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cance
}

String reason;

if (expired) {
reason = "expired at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", downtime->GetEndTime());
} else if (cancelled) {
reason = "cancelled by user";
} else {
reason = "<unknown>";
switch (removalReason) {
case DowntimeExpired:
reason = "expired at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", downtime->GetEndTime());
break;
case DowntimeRemovedByUser:
reason = "cancelled by user";
if (!removedBy.IsEmpty()) {
reason += " '" + removedBy + "'";
}
break;
case DowntimeRemovedByConfigOwner:
reason = "cancelled by '" + config_owner + "' of type 'ScheduledDowntime'";
break;
}

Log msg (LogInformation, "Downtime");
Expand Down Expand Up @@ -465,7 +471,7 @@ void Downtime::SetupCleanupTimer()
auto downtime (Downtime::GetByName(name));

if (downtime && downtime->IsExpired()) {
RemoveDowntime(name, false, false, true);
RemoveDowntime(name, false, DowntimeExpired);
}
});
}
Expand Down Expand Up @@ -557,7 +563,7 @@ void Downtime::DowntimesOrphanedTimerHandler()
for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
/* Only remove downtimes which are activated after daemon start. */
if (downtime->IsActive() && !downtime->HasValidConfigOwner())
RemoveDowntime(downtime->GetName(), false, false, true);
RemoveDowntime(downtime->GetName(), false, DowntimeRemovedByConfigOwner);
}
}

Expand Down
9 changes: 8 additions & 1 deletion lib/icinga/downtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ enum DowntimeChildOptions
DowntimeNonTriggeredChildren
};

enum DowntimeRemovalReason
{
DowntimeExpired,
DowntimeRemovedByUser,
DowntimeRemovedByConfigOwner,
};

/**
* A downtime.
*
Expand Down Expand Up @@ -52,7 +59,7 @@ class Downtime final : public ObjectImpl<Downtime>
const String& scheduledBy = String(), const String& parent = String(), const String& id = String(),
const MessageOrigin::Ptr& origin = nullptr);

static void RemoveDowntime(const String& id, bool includeChildren, bool cancelled, bool expired = false,
static void RemoveDowntime(const String& id, bool includeChildren, DowntimeRemovalReason removalReason,
const String& removedBy = "", const MessageOrigin::Ptr& origin = nullptr);

void RegisterChild(const Downtime::Ptr& downtime);
Expand Down
8 changes: 4 additions & 4 deletions lib/icinga/externalcommandprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>&
}

try {
Downtime::RemoveDowntime(rid, false, true);
Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
Expand Down Expand Up @@ -1112,7 +1112,7 @@ void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>
}

try {
Downtime::RemoveDowntime(rid, false, true);
Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
Expand Down Expand Up @@ -1147,7 +1147,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, false, true);
Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";
Expand All @@ -1169,7 +1169,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S

try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, false, true);
Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";
Expand Down
2 changes: 1 addition & 1 deletion lib/icinga/scheduleddowntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void ScheduledDowntime::RemoveObsoleteDowntimes()
auto configOwnerHash (downtime->GetConfigOwnerHash());

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