Skip to content

Commit

Permalink
Merge pull request #10124 from Icinga/do-not-fail-removing-obsolete-d…
Browse files Browse the repository at this point in the history
…owntimes-2.14

Don't fail to remove obsolete downtimes, remove RemoveAllDowntimes()
  • Loading branch information
yhabteab committed Sep 17, 2024
2 parents 2d970bc + ad7495d commit 1d0a984
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 29 deletions.
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
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 @@ -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

0 comments on commit 1d0a984

Please sign in to comment.