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, remove RemoveAllDowntimes() #10124

Merged
merged 4 commits into from
Sep 17, 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 @@ -549,7 +549,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 @@ -570,7 +570,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
7 changes: 0 additions & 7 deletions lib/icinga/checkable-downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

using namespace icinga;

void Checkable::RemoveAllDowntimes()
{
for (const Downtime::Ptr& downtime : GetDowntimes()) {
Downtime::RemoveDowntime(downtime->GetName(), true, true, true);
}
}

void Checkable::TriggerDowntimes(double triggerTime)
{
for (const Downtime::Ptr& downtime : GetDowntimes()) {
Expand Down
1 change: 0 additions & 1 deletion lib/icinga/checkable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class Checkable : public ObjectImpl<Checkable>
/* Downtimes */
int GetDowntimeDepth() const final;

void RemoveAllDowntimes();
void TriggerDowntimes(double triggerTime);
bool IsInDowntime() const;
bool IsAcknowledged() const;
Expand Down
32 changes: 19 additions & 13 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,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 @@ -359,18 +359,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 @@ -386,13 +386,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 @@ -455,7 +461,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 @@ -546,7 +552,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 @@ -986,7 +986,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>&
String rid = Downtime::GetDowntimeIDFromLegacyID(id);

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

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

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

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
Expand Down Expand Up @@ -1129,7 +1129,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 @@ -1151,7 +1151,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