diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 885834edc..fb6002a49 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -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(); @@ -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."); diff --git a/lib/icinga/downtime.cpp b/lib/icinga/downtime.cpp index 2178953f3..e06d202b5 100644 --- a/lib/icinga/downtime.cpp +++ b/lib/icinga/downtime.cpp @@ -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); @@ -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 != DowntimeExpired) { 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); } } - if (cancelled) { + if (removalReason != DowntimeExpired) { downtime->SetRemovalInfo(removedBy, Utility::GetTime()); } @@ -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 = ""; + 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"); @@ -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); } }); } @@ -546,7 +552,7 @@ void Downtime::DowntimesOrphanedTimerHandler() for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType()) { /* 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); } } diff --git a/lib/icinga/downtime.hpp b/lib/icinga/downtime.hpp index 15aa0af5d..8c15c6ffa 100644 --- a/lib/icinga/downtime.hpp +++ b/lib/icinga/downtime.hpp @@ -18,6 +18,13 @@ enum DowntimeChildOptions DowntimeNonTriggeredChildren }; +enum DowntimeRemovalReason +{ + DowntimeExpired, + DowntimeRemovedByUser, + DowntimeRemovedByConfigOwner, +}; + /** * A downtime. * @@ -52,7 +59,7 @@ public: 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); diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 9850da030..ed9fd2893 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -986,7 +986,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector& String rid = Downtime::GetDowntimeIDFromLegacyID(id); try { - Downtime::RemoveDowntime(rid, false, true); + Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime ID " << arguments[0]; @@ -1094,7 +1094,7 @@ void ExternalCommandProcessor::DelHostDowntime(double, const std::vector String rid = Downtime::GetDowntimeIDFromLegacyID(id); try { - Downtime::RemoveDowntime(rid, false, true); + Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime ID " << arguments[0]; @@ -1129,7 +1129,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vectorGetDowntimes()) { try { String downtimeName = downtime->GetName(); - Downtime::RemoveDowntime(downtimeName, false, true); + Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime '" << downtimeName << "'."; @@ -1151,7 +1151,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vectorGetName(); - Downtime::RemoveDowntime(downtimeName, false, true); + Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime '" << downtimeName << "'."; diff --git a/lib/icinga/scheduleddowntime.cpp b/lib/icinga/scheduleddowntime.cpp index f23d3e436..b04755a05 100644 --- a/lib/icinga/scheduleddowntime.cpp +++ b/lib/icinga/scheduleddowntime.cpp @@ -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); } } }