diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 4326a8207..6e9fddd4d 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -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(); @@ -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."); diff --git a/lib/icinga/downtime.cpp b/lib/icinga/downtime.cpp index 53de3a534..50c91b3f3 100644 --- a/lib/icinga/downtime.cpp +++ b/lib/icinga/downtime.cpp @@ -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); @@ -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 != 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()); } @@ -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 = ""; + 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"); @@ -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); } }); } @@ -557,7 +563,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 fa5ceee47..8be69826f 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 d3af82eed..0560947aa 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -993,7 +993,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector& } try { - Downtime::RemoveDowntime(rid, false, true); + Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime ID " << arguments[0]; @@ -1112,7 +1112,7 @@ void ExternalCommandProcessor::DelHostDowntime(double, const std::vector } try { - Downtime::RemoveDowntime(rid, false, true); + Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser); Log(LogNotice, "ExternalCommandProcessor") << "Removed downtime ID " << arguments[0]; @@ -1147,7 +1147,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 << "'."; @@ -1169,7 +1169,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 2ad9fa80d..f9ddc4385 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); } } }