From 6a7e83a5e6f70fc088aaf76db86d8235c8bd071d Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Thu, 31 Oct 2019 11:18:54 +0100 Subject: [PATCH] Improve Downtime & DowntimeHistory --- lib/icingadb/icingadb-objects.cpp | 117 ++++++------------------------ lib/icingadb/icingadb.hpp | 2 - 2 files changed, 24 insertions(+), 95 deletions(-) diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index 73fb00094..0ff9ff1e2 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -87,8 +87,6 @@ void IcingaDB::ConfigStaticInitialize() IcingaDB::VersionChangedHandler(object); }); - /* fixed/flexible downtime add */ - Downtime::OnDowntimeAdded.connect(&IcingaDB::DowntimeAddedHandler); /* fixed downtime start */ Downtime::OnDowntimeStarted.connect(&IcingaDB::DowntimeStartedHandler); /* flexible downtime start */ @@ -1031,8 +1029,10 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a attributes->Set("flexible_duration", TimestampToMilliseconds(downtime->GetDuration())); attributes->Set("is_flexible", !downtime->GetFixed()); attributes->Set("is_in_effect", downtime->IsInEffect()); - if (downtime->IsInEffect()) - attributes->Set("actual_start_time", TimestampToMilliseconds(downtime->GetTriggerTime())); + if (downtime->IsInEffect()) { + attributes->Set("start_time", TimestampToMilliseconds(downtime->GetTriggerTime())); + attributes->Set("end_time", TimestampToMilliseconds(downtime->GetFixed() ? downtime->GetEndTime() : (downtime->GetTriggerTime() + downtime->GetDuration()))); + } Host::Ptr host; Service::Ptr service; @@ -1048,6 +1048,11 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a attributes->Set("service_id", "00000000000000000000000000000000"); } + auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy())); + if (triggeredBy) { + attributes->Set("triggered_by_id", GetObjectIdentifier(triggeredBy)); + } + return true; } @@ -1281,73 +1286,6 @@ void IcingaDB::SendSentNotification( m_Rcon->FireAndForgetQuery(std::move(xAdd)); } -void IcingaDB::SendAddedDowntime(const Downtime::Ptr& downtime) -{ - if (!m_Rcon || !m_Rcon->IsConnected()) - return; - - auto checkable (downtime->GetCheckable()); - auto service (dynamic_pointer_cast(checkable)); - auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy())); - - std::vector xAdd ({ - "XADD", "icinga:history:stream:downtime", "*", - "downtime_id", GetObjectIdentifier(downtime), - "environment_id", SHA1(GetEnvironment()), - "entry_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEntryTime())), - "author", Utility::ValidateUTF8(downtime->GetAuthor()), - "comment", Utility::ValidateUTF8(downtime->GetComment()), - "is_flexible", Convert::ToString((unsigned short)!downtime->GetFixed()), - "flexible_duration", Convert::ToString(downtime->GetDuration()), - "scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())), - "scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())), - "was_started", "0", - "was_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), - "is_in_effect", Convert::ToString((unsigned short)downtime->IsInEffect()), - "trigger_time", Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())), - "event_id", Utility::NewUniqueID(), - "event_type", "downtime_schedule" - }); - - if (service) { - xAdd.emplace_back("object_type"); - xAdd.emplace_back("service"); - xAdd.emplace_back("service_id"); - xAdd.emplace_back(GetObjectIdentifier(checkable)); - } else { - xAdd.emplace_back("object_type"); - xAdd.emplace_back("host"); - xAdd.emplace_back("host_id"); - xAdd.emplace_back(GetObjectIdentifier(checkable)); - } - - if (triggeredBy) { - xAdd.emplace_back("triggered_by_id"); - xAdd.emplace_back(GetObjectIdentifier(triggeredBy)); - } - - if (downtime->GetFixed()) { - xAdd.emplace_back("actual_start_time"); - xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime()))); - xAdd.emplace_back("actual_end_time"); - xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime()))); - } else { - xAdd.emplace_back("actual_start_time"); - xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime()))); - xAdd.emplace_back("actual_end_time"); - xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + downtime->GetDuration()))); - } - - auto endpoint (Endpoint::GetLocalEndpoint()); - - if (endpoint) { - xAdd.emplace_back("endpoint_id"); - xAdd.emplace_back(GetObjectIdentifier(endpoint)); - } - - m_Rcon->FireAndForgetQuery(std::move(xAdd)); -} - void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime) { if (!m_Rcon || !m_Rcon->IsConnected()) @@ -1370,9 +1308,7 @@ void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime) "flexible_duration", Convert::ToString(downtime->GetDuration()), "scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())), "scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())), - "was_started", "1", - "was_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), - "is_in_effect", Convert::ToString((unsigned short)downtime->IsInEffect()), + "has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), "trigger_time", Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())), "event_id", Utility::NewUniqueID(), "event_type", "downtime_start" @@ -1396,14 +1332,14 @@ void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime) } if (downtime->GetFixed()) { - xAdd.emplace_back("actual_start_time"); + xAdd.emplace_back("start_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime()))); - xAdd.emplace_back("actual_end_time"); + xAdd.emplace_back("end_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime()))); } else { - xAdd.emplace_back("actual_start_time"); + xAdd.emplace_back("start_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime()))); - xAdd.emplace_back("actual_end_time"); + xAdd.emplace_back("end_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + downtime->GetDuration()))); } @@ -1426,6 +1362,10 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime) auto service (dynamic_pointer_cast(checkable)); auto triggeredBy (Downtime::GetByName(downtime->GetTriggeredBy())); + // Downtime never got triggered (didn't send "downtime_start") so we don't want to send "downtime_end" + if (downtime->GetTriggerTime() == 0) + return; + std::vector xAdd ({ "XADD", "icinga:history:stream:downtime", "*", "downtime_id", GetObjectIdentifier(downtime), @@ -1437,11 +1377,9 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime) "flexible_duration", Convert::ToString(downtime->GetDuration()), "scheduled_start_time", Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime())), "scheduled_end_time", Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime())), - "was_started", "1", - "was_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), - "is_in_effect", Convert::ToString((unsigned short)downtime->IsInEffect()), + "has_been_cancelled", Convert::ToString((unsigned short)downtime->GetWasCancelled()), "trigger_time", Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime())), - "deletion_time", Convert::ToString(TimestampToMilliseconds(Utility::GetTime())), + "cancel_time", Convert::ToString(TimestampToMilliseconds(Utility::GetTime())), "event_id", Utility::NewUniqueID(), "event_type", "downtime_end" }); @@ -1464,14 +1402,14 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime) } if (downtime->GetFixed()) { - xAdd.emplace_back("actual_start_time"); + xAdd.emplace_back("start_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetStartTime()))); - xAdd.emplace_back("actual_end_time"); + xAdd.emplace_back("end_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetEndTime()))); } else { - xAdd.emplace_back("actual_start_time"); + xAdd.emplace_back("start_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime()))); - xAdd.emplace_back("actual_end_time"); + xAdd.emplace_back("end_time"); xAdd.emplace_back(Convert::ToString(TimestampToMilliseconds(downtime->GetTriggerTime() + downtime->GetDuration()))); } @@ -1791,13 +1729,6 @@ void IcingaDB::VersionChangedHandler(const ConfigObject::Ptr& object) } } -void IcingaDB::DowntimeAddedHandler(const Downtime::Ptr& downtime) -{ - for (auto& rw : ConfigType::GetObjectsByType()) { - rw->m_WorkQueue.Enqueue([rw, downtime]() { rw->SendAddedDowntime(downtime); }); - } -} - void IcingaDB::DowntimeStartedHandler(const Downtime::Ptr& downtime) { StateChangeHandler(downtime->GetCheckable()); diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 5437422c5..aeb955d4a 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -87,7 +87,6 @@ private: NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text ); - void SendAddedDowntime(const Downtime::Ptr& downtime); void SendStartedDowntime(const Downtime::Ptr& downtime); void SendRemovedDowntime(const Downtime::Ptr& downtime); void SendAddedComment(const Comment::Ptr& comment); @@ -123,7 +122,6 @@ private: static void StateChangeHandler(const ConfigObject::Ptr& object); static void StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type); static void VersionChangedHandler(const ConfigObject::Ptr& object); - static void DowntimeAddedHandler(const Downtime::Ptr& downtime); static void DowntimeStartedHandler(const Downtime::Ptr& downtime); static void DowntimeRemovedHandler(const Downtime::Ptr& downtime);