From 2e19fce31d972e4968ed9beba02ed695fde9ebc9 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 25 Apr 2025 12:08:08 +0200 Subject: [PATCH 1/3] Remove some superfluous if statements They're just useless, since a `CheckResult` handler is never going to be called without a check result and a checkable can't exist without a checkcommand. --- lib/perfdata/elasticsearchwriter.cpp | 37 ++++----------------- lib/perfdata/gelfwriter.cpp | 48 ++++++++-------------------- 2 files changed, 21 insertions(+), 64 deletions(-) diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index eaa19da59b1..39e411813be 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -272,22 +272,12 @@ void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& check fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts()); fields->Set("reachable", checkable->IsReachable()); + fields->Set("check_command", checkable->GetCheckCommand()->GetName()); - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); - - if (commandObj) - fields->Set("check_command", commandObj->GetName()); - - double ts = Utility::GetTime(); - - if (cr) { - AddCheckResult(fields, checkable, cr); - ts = cr->GetExecutionEnd(); - } - + AddCheckResult(fields, checkable, cr); AddTemplateTags(fields, checkable, cr); - Enqueue(checkable, "checkresult", fields, ts); + Enqueue(checkable, "checkresult", fields, cr->GetExecutionEnd()); } void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) @@ -325,21 +315,12 @@ void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& check fields->Set("last_hard_state", host->GetLastHardState()); } - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); - - if (commandObj) - fields->Set("check_command", commandObj->GetName()); - - double ts = Utility::GetTime(); - - if (cr) { - AddCheckResult(fields, checkable, cr); - ts = cr->GetExecutionEnd(); - } + fields->Set("check_command", checkable->GetCheckCommand()->GetName()); + AddCheckResult(fields, checkable, cr); AddTemplateTags(fields, checkable, cr); - Enqueue(checkable, "statechange", fields, ts); + Enqueue(checkable, "statechange", fields, cr->GetExecutionEnd()); } void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, @@ -396,11 +377,7 @@ void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Notifi fields->Set("notification_type", notificationTypeString); fields->Set("author", author); fields->Set("text", text); - - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); - - if (commandObj) - fields->Set("check_command", commandObj->GetName()); + fields->Set("check_command", checkable->GetCheckCommand()->GetName()); double ts = Utility::GetTime(); diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index c5b2bbd132e..35541ac60d6 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -306,22 +306,15 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con fields->Set("_reachable", checkable->IsReachable()); CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); + fields->Set("_check_command", checkCommand->GetName()); - if (checkCommand) - fields->Set("_check_command", checkCommand->GetName()); + fields->Set("_latency", cr->CalculateLatency()); + fields->Set("_execution_time", cr->CalculateExecutionTime()); + fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); + fields->Set("full_message", cr->GetOutput()); + fields->Set("_check_source", cr->GetCheckSource()); - double ts = Utility::GetTime(); - - if (cr) { - fields->Set("_latency", cr->CalculateLatency()); - fields->Set("_execution_time", cr->CalculateExecutionTime()); - fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); - fields->Set("full_message", cr->GetOutput()); - fields->Set("_check_source", cr->GetCheckSource()); - ts = cr->GetExecutionEnd(); - } - - if (cr && GetEnableSendPerfdata()) { + if (GetEnableSendPerfdata()) { Array::Ptr perfdata = cr->GetPerformanceData(); if (perfdata) { @@ -366,7 +359,7 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con } } - SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); } void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, @@ -430,11 +423,7 @@ void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& noti fields->Set("_command", commandName); fields->Set("_notification_type", notificationTypeString); fields->Set("_comment", authorComment); - - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); - - if (commandObj) - fields->Set("_check_command", commandObj->GetName()); + fields->Set("_check_command", checkable->GetCheckCommand()->GetName()); SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); } @@ -478,21 +467,12 @@ void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, con fields->Set("_last_hard_state", host->GetLastHardState()); } - CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); + fields->Set("_check_command", checkable->GetCheckCommand()->GetName()); + fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); + fields->Set("full_message", cr->GetOutput()); + fields->Set("_check_source", cr->GetCheckSource()); - if (commandObj) - fields->Set("_check_command", commandObj->GetName()); - - double ts = Utility::GetTime(); - - if (cr) { - fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); - fields->Set("full_message", cr->GetOutput()); - fields->Set("_check_source", cr->GetCheckSource()); - ts = cr->GetExecutionEnd(); - } - - SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); } String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts) From a589b87d6cc07b4da4712b4ec88f0bd458da0977 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 25 Apr 2025 12:14:31 +0200 Subject: [PATCH 2/3] Remove unused parameters --- lib/perfdata/elasticsearchwriter.cpp | 28 ++++++++++++-------------- lib/perfdata/elasticsearchwriter.hpp | 14 ++++++------- lib/perfdata/gelfwriter.cpp | 30 +++++++++++++--------------- lib/perfdata/gelfwriter.hpp | 14 ++++++------- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index 39e411813be..ed438193199 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -101,13 +101,13 @@ void ElasticsearchWriter::Resume() CheckResultHandler(checkable, cr); }); m_HandleStateChanges = Checkable::OnStateChange.connect([this](const Checkable::Ptr& checkable, - const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr&) { - StateChangeHandler(checkable, cr, type); + const CheckResult::Ptr& cr, StateType, const MessageOrigin::Ptr&) { + StateChangeHandler(checkable, cr); }); - m_HandleNotifications = Checkable::OnNotificationSentToAllUsers.connect([this](const Notification::Ptr& notification, + m_HandleNotifications = Checkable::OnNotificationSentToAllUsers.connect([this](const Notification::Ptr&, const Checkable::Ptr& checkable, const std::set& users, const NotificationType& type, const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr&) { - NotificationSentToAllUsersHandler(notification, checkable, users, type, cr, author, text); + NotificationSentToAllUsersHandler(checkable, users, type, cr, author, text); }); } @@ -280,15 +280,15 @@ void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& check Enqueue(checkable, "checkresult", fields, cr->GetExecutionEnd()); } -void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) +void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr, type]() { StateChangeHandlerInternal(checkable, cr, type); }); + m_WorkQueue.Enqueue([this, checkable, cr]() { StateChangeHandlerInternal(checkable, cr); }); } -void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) +void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { AssertOnWorkQueue(); @@ -323,21 +323,19 @@ void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& check Enqueue(checkable, "statechange", fields, cr->GetExecutionEnd()); } -void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, - const Checkable::Ptr& checkable, const std::set& users, NotificationType type, - const CheckResult::Ptr& cr, const String& author, const String& text) +void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Checkable::Ptr& checkable, const std::set& users, + NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text) { if (IsPaused()) return; - m_WorkQueue.Enqueue([this, notification, checkable, users, type, cr, author, text]() { - NotificationSentToAllUsersHandlerInternal(notification, checkable, users, type, cr, author, text); + m_WorkQueue.Enqueue([this, checkable, users, type, cr, author, text]() { + NotificationSentToAllUsersHandlerInternal(checkable, users, type, cr, author, text); }); } -void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Notification::Ptr& notification, - const Checkable::Ptr& checkable, const std::set& users, NotificationType type, - const CheckResult::Ptr& cr, const String& author, const String& text) +void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Checkable::Ptr& checkable, const std::set& users, + NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text) { AssertOnWorkQueue(); diff --git a/lib/perfdata/elasticsearchwriter.hpp b/lib/perfdata/elasticsearchwriter.hpp index 9ecd0fd769f..1f5eb60bfd0 100644 --- a/lib/perfdata/elasticsearchwriter.hpp +++ b/lib/perfdata/elasticsearchwriter.hpp @@ -42,16 +42,14 @@ class ElasticsearchWriter final : public ObjectImpl void AddCheckResult(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void AddTemplateTags(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type); - void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type); + void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); + void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void InternalCheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void NotificationSentToAllUsersHandler(const Notification::Ptr& notification, - const Checkable::Ptr& checkable, const std::set& users, NotificationType type, - const CheckResult::Ptr& cr, const String& author, const String& text); - void NotificationSentToAllUsersHandlerInternal(const Notification::Ptr& notification, - const Checkable::Ptr& checkable, const std::set& users, NotificationType type, - const CheckResult::Ptr& cr, const String& author, const String& text); + void NotificationSentToAllUsersHandler(const Checkable::Ptr& checkable, const std::set& users, + NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text); + void NotificationSentToAllUsersHandlerInternal(const Checkable::Ptr& checkable, const std::set& users, + NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text); void Enqueue(const Checkable::Ptr& checkable, const String& type, const Dictionary::Ptr& fields, double ts); diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index 35541ac60d6..5f7ae059b51 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -94,14 +94,14 @@ void GelfWriter::Resume() const CheckResult::Ptr& cr, const MessageOrigin::Ptr&) { CheckResultHandler(checkable, cr); }); - m_HandleNotifications = Checkable::OnNotificationSentToUser.connect([this](const Notification::Ptr& notification, - const Checkable::Ptr& checkable, const User::Ptr& user, const NotificationType& type, const CheckResult::Ptr& cr, + m_HandleNotifications = Checkable::OnNotificationSentToUser.connect([this](const Notification::Ptr&, + const Checkable::Ptr& checkable, const User::Ptr&, const NotificationType& type, const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& commandName, const MessageOrigin::Ptr&) { - NotificationToUserHandler(notification, checkable, user, type, cr, author, commentText, commandName); + NotificationToUserHandler(checkable, type, cr, author, commentText, commandName); }); m_HandleStateChanges = Checkable::OnStateChange.connect([this](const Checkable::Ptr& checkable, - const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr&) { - StateChangeHandler(checkable, cr, type); + const CheckResult::Ptr& cr, StateType, const MessageOrigin::Ptr&) { + StateChangeHandler(checkable, cr); }); } @@ -362,21 +362,19 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); } -void GelfWriter::NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, - const User::Ptr& user, NotificationType notificationType, CheckResult::Ptr const& cr, - const String& author, const String& commentText, const String& commandName) +void GelfWriter::NotificationToUserHandler(const Checkable::Ptr& checkable, NotificationType notificationType, + CheckResult::Ptr const& cr, const String& author, const String& commentText, const String& commandName) { if (IsPaused()) return; - m_WorkQueue.Enqueue([this, notification, checkable, user, notificationType, cr, author, commentText, commandName]() { - NotificationToUserHandlerInternal(notification, checkable, user, notificationType, cr, author, commentText, commandName); + m_WorkQueue.Enqueue([this, checkable, notificationType, cr, author, commentText, commandName]() { + NotificationToUserHandlerInternal(checkable, notificationType, cr, author, commentText, commandName); }); } -void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& notification, const Checkable::Ptr& checkable, - const User::Ptr& user, NotificationType notificationType, CheckResult::Ptr const& cr, - const String& author, const String& commentText, const String& commandName) +void GelfWriter::NotificationToUserHandlerInternal(const Checkable::Ptr& checkable, NotificationType notificationType, + CheckResult::Ptr const& cr, const String& author, const String& commentText, const String& commandName) { AssertOnWorkQueue(); @@ -428,15 +426,15 @@ void GelfWriter::NotificationToUserHandlerInternal(const Notification::Ptr& noti SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); } -void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) +void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr, type]() { StateChangeHandlerInternal(checkable, cr, type); }); + m_WorkQueue.Enqueue([this, checkable, cr]() { StateChangeHandlerInternal(checkable, cr); }); } -void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) +void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { AssertOnWorkQueue(); diff --git a/lib/perfdata/gelfwriter.hpp b/lib/perfdata/gelfwriter.hpp index ce9ee354580..b1dc90796b9 100644 --- a/lib/perfdata/gelfwriter.hpp +++ b/lib/perfdata/gelfwriter.hpp @@ -41,14 +41,12 @@ class GelfWriter final : public ObjectImpl void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void NotificationToUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, - const User::Ptr& user, NotificationType notificationType, const CheckResult::Ptr& cr, - const String& author, const String& commentText, const String& commandName); - void NotificationToUserHandlerInternal(const Notification::Ptr& notification, const Checkable::Ptr& checkable, - const User::Ptr& user, NotificationType notification_type, const CheckResult::Ptr& cr, - const String& author, const String& comment_text, const String& command_name); - void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type); - void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type); + void NotificationToUserHandler(const Checkable::Ptr& checkable, NotificationType notificationType, + const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& commandName); + void NotificationToUserHandlerInternal(const Checkable::Ptr& checkable, NotificationType notification_type, + const CheckResult::Ptr& cr, const String& author, const String& comment_text, const String& command_name); + void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); + void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); String ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts); void SendLogMessage(const Checkable::Ptr& checkable, const String& gelfMessage); From cef6fb77e559b1155ef87cb50df297e63e30228a Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 25 Apr 2025 12:17:19 +0200 Subject: [PATCH 3/3] Serialize fields before queueing the event to the workqueue --- lib/perfdata/elasticsearchwriter.cpp | 74 +++++------- lib/perfdata/elasticsearchwriter.hpp | 4 - lib/perfdata/gelfwriter.cpp | 160 ++++++++++++-------------- lib/perfdata/gelfwriter.hpp | 8 +- lib/perfdata/graphitewriter.cpp | 72 +++++------- lib/perfdata/graphitewriter.hpp | 3 +- lib/perfdata/influxdbcommonwriter.cpp | 118 +++++++++---------- lib/perfdata/influxdbcommonwriter.hpp | 1 - 8 files changed, 189 insertions(+), 251 deletions(-) diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index ed438193199..8e6848ab3f7 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -236,15 +236,6 @@ void ElasticsearchWriter::CheckResultHandler(const Checkable::Ptr& checkable, co if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { InternalCheckResultHandler(checkable, cr); }); -} - -void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("Elasticwriter processing check result for '" << checkable->GetName() << "'"); - if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; @@ -274,10 +265,15 @@ void ElasticsearchWriter::InternalCheckResultHandler(const Checkable::Ptr& check fields->Set("reachable", checkable->IsReachable()); fields->Set("check_command", checkable->GetCheckCommand()->GetName()); - AddCheckResult(fields, checkable, cr); AddTemplateTags(fields, checkable, cr); - Enqueue(checkable, "checkresult", fields, cr->GetExecutionEnd()); + m_WorkQueue.Enqueue([this, checkable, cr, fields = std::move(fields)]() { + CONTEXT("Elasticwriter processing check result for '" << checkable->GetName() << "'"); + + AddCheckResult(fields, checkable, cr); + + Enqueue(checkable, "checkresult", fields, cr->GetExecutionEnd()); + }); } void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) @@ -285,15 +281,6 @@ void ElasticsearchWriter::StateChangeHandler(const Checkable::Ptr& checkable, co if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { StateChangeHandlerInternal(checkable, cr); }); -} - -void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("Elasticwriter processing state change '" << checkable->GetName() << "'"); - Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); @@ -317,32 +304,22 @@ void ElasticsearchWriter::StateChangeHandlerInternal(const Checkable::Ptr& check fields->Set("check_command", checkable->GetCheckCommand()->GetName()); - AddCheckResult(fields, checkable, cr); AddTemplateTags(fields, checkable, cr); - Enqueue(checkable, "statechange", fields, cr->GetExecutionEnd()); -} + m_WorkQueue.Enqueue([this, checkable, cr, fields = std::move(fields)]() { + CONTEXT("Elasticwriter processing state change '" << checkable->GetName() << "'"); -void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Checkable::Ptr& checkable, const std::set& users, - NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text) -{ - if (IsPaused()) - return; + AddCheckResult(fields, checkable, cr); - m_WorkQueue.Enqueue([this, checkable, users, type, cr, author, text]() { - NotificationSentToAllUsersHandlerInternal(checkable, users, type, cr, author, text); + Enqueue(checkable, "statechange", fields, cr->GetExecutionEnd()); }); } -void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Checkable::Ptr& checkable, const std::set& users, +void ElasticsearchWriter::NotificationSentToAllUsersHandler(const Checkable::Ptr& checkable, const std::set& users, NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text) { - AssertOnWorkQueue(); - - CONTEXT("Elasticwriter processing notification to all users '" << checkable->GetName() << "'"); - - Log(LogDebug, "ElasticsearchWriter") - << "Processing notification for '" << checkable->GetName() << "'"; + if (IsPaused()) + return; Host::Ptr host; Service::Ptr service; @@ -377,21 +354,30 @@ void ElasticsearchWriter::NotificationSentToAllUsersHandlerInternal(const Checka fields->Set("text", text); fields->Set("check_command", checkable->GetCheckCommand()->GetName()); - double ts = Utility::GetTime(); + AddTemplateTags(fields, checkable, cr); + + m_WorkQueue.Enqueue([this, checkable, cr, fields = std::move(fields)]() { + CONTEXT("Elasticwriter processing notification to all users '" << checkable->GetName() << "'"); - if (cr) { - AddCheckResult(fields, checkable, cr); - ts = cr->GetExecutionEnd(); - } + Log(LogDebug, "ElasticsearchWriter") + << "Processing notification for '" << checkable->GetName() << "'"; - AddTemplateTags(fields, checkable, cr); + double ts = Utility::GetTime(); - Enqueue(checkable, "notification", fields, ts); + if (cr) { + AddCheckResult(fields, checkable, cr); + ts = cr->GetExecutionEnd(); + } + + Enqueue(checkable, "notification", fields, ts); + }); } void ElasticsearchWriter::Enqueue(const Checkable::Ptr& checkable, const String& type, const Dictionary::Ptr& fields, double ts) { + AssertOnWorkQueue(); + /* Atomically buffer the data point. */ std::unique_lock lock(m_DataBufferMutex); diff --git a/lib/perfdata/elasticsearchwriter.hpp b/lib/perfdata/elasticsearchwriter.hpp index 1f5eb60bfd0..c92f02c23a2 100644 --- a/lib/perfdata/elasticsearchwriter.hpp +++ b/lib/perfdata/elasticsearchwriter.hpp @@ -43,13 +43,9 @@ class ElasticsearchWriter final : public ObjectImpl void AddTemplateTags(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void InternalCheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void NotificationSentToAllUsersHandler(const Checkable::Ptr& checkable, const std::set& users, NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text); - void NotificationSentToAllUsersHandlerInternal(const Checkable::Ptr& checkable, const std::set& users, - NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text); void Enqueue(const Checkable::Ptr& checkable, const String& type, const Dictionary::Ptr& fields, double ts); diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index 5f7ae059b51..59561d39a74 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -268,18 +268,6 @@ void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const Check if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { CheckResultHandlerInternal(checkable, cr); }); -} - -void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("GELF Processing check result for '" << checkable->GetName() << "'"); - - Log(LogDebug, "GelfWriter") - << "Processing check result for '" << checkable->GetName() << "'"; - Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); @@ -308,81 +296,73 @@ void GelfWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, con CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); fields->Set("_check_command", checkCommand->GetName()); - fields->Set("_latency", cr->CalculateLatency()); - fields->Set("_execution_time", cr->CalculateExecutionTime()); - fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); - fields->Set("full_message", cr->GetOutput()); - fields->Set("_check_source", cr->GetCheckSource()); + m_WorkQueue.Enqueue([this, checkable, cr, fields = std::move(fields)]() { + CONTEXT("GELF Processing check result for '" << checkable->GetName() << "'"); - if (GetEnableSendPerfdata()) { - Array::Ptr perfdata = cr->GetPerformanceData(); - - if (perfdata) { - ObjectLock olock(perfdata); - for (const Value& val : perfdata) { - PerfdataValue::Ptr pdv; - - if (val.IsObjectType()) - pdv = val; - else { - try { - pdv = PerfdataValue::Parse(val); - } catch (const std::exception&) { - Log(LogWarning, "GelfWriter") - << "Ignoring invalid perfdata for checkable '" - << checkable->GetName() << "' and command '" - << checkCommand->GetName() << "' with value: " << val; - continue; + Log(LogDebug, "GelfWriter") + << "Processing check result for '" << checkable->GetName() << "'"; + + fields->Set("_latency", cr->CalculateLatency()); + fields->Set("_execution_time", cr->CalculateExecutionTime()); + fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); + fields->Set("full_message", cr->GetOutput()); + fields->Set("_check_source", cr->GetCheckSource()); + + if (GetEnableSendPerfdata()) { + Array::Ptr perfdata = cr->GetPerformanceData(); + + if (perfdata) { + ObjectLock olock(perfdata); + for (const Value& val : perfdata) { + PerfdataValue::Ptr pdv; + + if (val.IsObjectType()) + pdv = val; + else { + try { + pdv = PerfdataValue::Parse(val); + } catch (const std::exception&) { + Log(LogWarning, "GelfWriter") + << "Ignoring invalid perfdata for checkable '" + << checkable->GetName() << "' and command '" + << checkable->GetCheckCommand()->GetName() << "' with value: " << val; + continue; + } } - } - String escaped_key = pdv->GetLabel(); - boost::replace_all(escaped_key, " ", "_"); - boost::replace_all(escaped_key, ".", "_"); - boost::replace_all(escaped_key, "\\", "_"); - boost::algorithm::replace_all(escaped_key, "::", "."); - - fields->Set("_" + escaped_key, pdv->GetValue()); - - if (!pdv->GetMin().IsEmpty()) - fields->Set("_" + escaped_key + "_min", pdv->GetMin()); - if (!pdv->GetMax().IsEmpty()) - fields->Set("_" + escaped_key + "_max", pdv->GetMax()); - if (!pdv->GetWarn().IsEmpty()) - fields->Set("_" + escaped_key + "_warn", pdv->GetWarn()); - if (!pdv->GetCrit().IsEmpty()) - fields->Set("_" + escaped_key + "_crit", pdv->GetCrit()); - - if (!pdv->GetUnit().IsEmpty()) - fields->Set("_" + escaped_key + "_unit", pdv->GetUnit()); + String escaped_key = pdv->GetLabel(); + boost::replace_all(escaped_key, " ", "_"); + boost::replace_all(escaped_key, ".", "_"); + boost::replace_all(escaped_key, "\\", "_"); + boost::algorithm::replace_all(escaped_key, "::", "."); + + fields->Set("_" + escaped_key, pdv->GetValue()); + + if (!pdv->GetMin().IsEmpty()) + fields->Set("_" + escaped_key + "_min", pdv->GetMin()); + if (!pdv->GetMax().IsEmpty()) + fields->Set("_" + escaped_key + "_max", pdv->GetMax()); + if (!pdv->GetWarn().IsEmpty()) + fields->Set("_" + escaped_key + "_warn", pdv->GetWarn()); + if (!pdv->GetCrit().IsEmpty()) + fields->Set("_" + escaped_key + "_crit", pdv->GetCrit()); + + if (!pdv->GetUnit().IsEmpty()) + fields->Set("_" + escaped_key + "_unit", pdv->GetUnit()); + } } } - } - SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); + }); } void GelfWriter::NotificationToUserHandler(const Checkable::Ptr& checkable, NotificationType notificationType, - CheckResult::Ptr const& cr, const String& author, const String& commentText, const String& commandName) + const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& commandName) { if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, notificationType, cr, author, commentText, commandName]() { - NotificationToUserHandlerInternal(checkable, notificationType, cr, author, commentText, commandName); - }); -} - -void GelfWriter::NotificationToUserHandlerInternal(const Checkable::Ptr& checkable, NotificationType notificationType, - CheckResult::Ptr const& cr, const String& author, const String& commentText, const String& commandName) -{ - AssertOnWorkQueue(); - - CONTEXT("GELF Processing notification to all users '" << checkable->GetName() << "'"); - - Log(LogDebug, "GelfWriter") - << "Processing notification for '" << checkable->GetName() << "'"; - Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); @@ -423,7 +403,14 @@ void GelfWriter::NotificationToUserHandlerInternal(const Checkable::Ptr& checkab fields->Set("_comment", authorComment); fields->Set("_check_command", checkable->GetCheckCommand()->GetName()); - SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); + m_WorkQueue.Enqueue([this, checkable, ts, fields = std::move(fields)]() { + CONTEXT("GELF Processing notification to all users '" << checkable->GetName() << "'"); + + Log(LogDebug, "GelfWriter") + << "Processing notification for '" << checkable->GetName() << "'"; + + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); + }); } void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) @@ -431,18 +418,6 @@ void GelfWriter::StateChangeHandler(const Checkable::Ptr& checkable, const Check if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { StateChangeHandlerInternal(checkable, cr); }); -} - -void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("GELF Processing state change '" << checkable->GetName() << "'"); - - Log(LogDebug, "GelfWriter") - << "Processing state change for '" << checkable->GetName() << "'"; - Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); @@ -470,7 +445,14 @@ void GelfWriter::StateChangeHandlerInternal(const Checkable::Ptr& checkable, con fields->Set("full_message", cr->GetOutput()); fields->Set("_check_source", cr->GetCheckSource()); - SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), cr->GetExecutionEnd())); + m_WorkQueue.Enqueue([this, checkable, fields = std::move(fields), ts = cr->GetExecutionEnd()]() { + CONTEXT("GELF Processing state change '" << checkable->GetName() << "'"); + + Log(LogDebug, "GelfWriter") + << "Processing state change for '" << checkable->GetName() << "'"; + + SendLogMessage(checkable, ComposeGelfMessage(fields, GetSource(), ts)); + }); } String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts) @@ -484,6 +466,8 @@ String GelfWriter::ComposeGelfMessage(const Dictionary::Ptr& fields, const Strin void GelfWriter::SendLogMessage(const Checkable::Ptr& checkable, const String& gelfMessage) { + AssertOnWorkQueue(); + std::ostringstream msgbuf; msgbuf << gelfMessage; msgbuf << '\0'; diff --git a/lib/perfdata/gelfwriter.hpp b/lib/perfdata/gelfwriter.hpp index b1dc90796b9..9d6b336a7ef 100644 --- a/lib/perfdata/gelfwriter.hpp +++ b/lib/perfdata/gelfwriter.hpp @@ -40,13 +40,9 @@ class GelfWriter final : public ObjectImpl Timer::Ptr m_ReconnectTimer; void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void NotificationToUserHandler(const Checkable::Ptr& checkable, NotificationType notificationType, - const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& commandName); - void NotificationToUserHandlerInternal(const Checkable::Ptr& checkable, NotificationType notification_type, - const CheckResult::Ptr& cr, const String& author, const String& comment_text, const String& command_name); + void NotificationToUserHandler(const Checkable::Ptr& checkable, NotificationType notificationType, const CheckResult::Ptr& cr, + const String& author, const String& commentText, const String& commandName); void StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void StateChangeHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); String ComposeGelfMessage(const Dictionary::Ptr& fields, const String& source, double ts); void SendLogMessage(const Checkable::Ptr& checkable, const String& gelfMessage); diff --git a/lib/perfdata/graphitewriter.cpp b/lib/perfdata/graphitewriter.cpp index 6adae02337a..2adbb64f3f2 100644 --- a/lib/perfdata/graphitewriter.cpp +++ b/lib/perfdata/graphitewriter.cpp @@ -261,27 +261,6 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { CheckResultHandlerInternal(checkable, cr); }); -} - -/** - * Check result event handler, prepares metadata and perfdata values and calls Send*() - * - * Called inside the WQ. - * - * @param checkable Host/Service object - * @param cr Check result including performance data - */ -void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("Processing check result for '" << checkable->GetName() << "'"); - - /* TODO: Deal with missing connection here. Needs refactoring - * into parsing the actual performance data and then putting it - * into a queue for re-inserting. */ - if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; @@ -306,29 +285,34 @@ void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, }); } - String prefixPerfdata = prefix + ".perfdata"; - String prefixMetadata = prefix + ".metadata"; + std::vector> metadata; + if (GetEnableSendMetadata()) { + metadata = { + {"state", service ? service->GetState() : host->GetState()}, + {"current_attempt", checkable->GetCheckAttempt()}, + {"max_check_attempts", checkable->GetMaxCheckAttempts()}, + {"state_type", checkable->GetStateType()}, + {"reachable", checkable->IsReachable()}, + {"downtime_depth", checkable->GetDowntimeDepth()}, + {"acknowledgement", checkable->GetAcknowledgement()}, + {"latency", cr->CalculateLatency()}, + {"execution_time", cr->CalculateExecutionTime()} + }; + } - double ts = cr->GetExecutionEnd(); + m_WorkQueue.Enqueue([this, checkable, cr, prefix = std::move(prefix), metadata = std::move(metadata)]() { + CONTEXT("Processing check result for '" << checkable->GetName() << "'"); - if (GetEnableSendMetadata()) { - if (service) { - SendMetric(checkable, prefixMetadata, "state", service->GetState(), ts); - } else { - SendMetric(checkable, prefixMetadata, "state", host->GetState(), ts); - } + /* TODO: Deal with missing connection here. Needs refactoring + * into parsing the actual performance data and then putting it + * into a queue for re-inserting. */ - SendMetric(checkable, prefixMetadata, "current_attempt", checkable->GetCheckAttempt(), ts); - SendMetric(checkable, prefixMetadata, "max_check_attempts", checkable->GetMaxCheckAttempts(), ts); - SendMetric(checkable, prefixMetadata, "state_type", checkable->GetStateType(), ts); - SendMetric(checkable, prefixMetadata, "reachable", checkable->IsReachable(), ts); - SendMetric(checkable, prefixMetadata, "downtime_depth", checkable->GetDowntimeDepth(), ts); - SendMetric(checkable, prefixMetadata, "acknowledgement", checkable->GetAcknowledgement(), ts); - SendMetric(checkable, prefixMetadata, "latency", cr->CalculateLatency(), ts); - SendMetric(checkable, prefixMetadata, "execution_time", cr->CalculateExecutionTime(), ts); - } + for (auto& [name, val] : metadata) { + SendMetric(checkable, prefix + ".metadata", name, val, cr->GetExecutionEnd()); + } - SendPerfdata(checkable, prefixPerfdata, cr, ts); + SendPerfdata(checkable, prefix + ".perfdata", cr); + }); } /** @@ -337,10 +321,11 @@ void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable, * @param checkable Host/service object * @param prefix Metric prefix string * @param cr Check result including performance data - * @param ts Timestamp when the check result was created */ -void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr, double ts) +void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr) { + AssertOnWorkQueue(); + Array::Ptr perfdata = cr->GetPerformanceData(); if (!perfdata) @@ -367,6 +352,7 @@ void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& } String escapedKey = EscapeMetricLabel(pdv->GetLabel()); + double ts = cr->GetExecutionEnd(); SendMetric(checkable, prefix, escapedKey + ".value", pdv->GetValue(), ts); @@ -394,6 +380,8 @@ void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& */ void GraphiteWriter::SendMetric(const Checkable::Ptr& checkable, const String& prefix, const String& name, double value, double ts) { + AssertOnWorkQueue(); + namespace asio = boost::asio; std::ostringstream msgbuf; diff --git a/lib/perfdata/graphitewriter.hpp b/lib/perfdata/graphitewriter.hpp index e0c8b784661..9aa0dc04728 100644 --- a/lib/perfdata/graphitewriter.hpp +++ b/lib/perfdata/graphitewriter.hpp @@ -45,9 +45,8 @@ class GraphiteWriter final : public ObjectImpl Timer::Ptr m_ReconnectTimer; void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void SendMetric(const Checkable::Ptr& checkable, const String& prefix, const String& name, double value, double ts); - void SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr, double ts); + void SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr); static String EscapeMetric(const String& str); static String EscapeMetricLabel(const String& str); static Value EscapeMacroMetric(const Value& value); diff --git a/lib/perfdata/influxdbcommonwriter.cpp b/lib/perfdata/influxdbcommonwriter.cpp index d5aaa7c9839..14c5b004f1c 100644 --- a/lib/perfdata/influxdbcommonwriter.cpp +++ b/lib/perfdata/influxdbcommonwriter.cpp @@ -204,15 +204,6 @@ void InfluxdbCommonWriter::CheckResultHandler(const Checkable::Ptr& checkable, c if (IsPaused()) return; - m_WorkQueue.Enqueue([this, checkable, cr]() { CheckResultHandlerWQ(checkable, cr); }, PriorityLow); -} - -void InfluxdbCommonWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) -{ - AssertOnWorkQueue(); - - CONTEXT("Processing check result for '" << checkable->GetName() << "'"); - if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; @@ -225,10 +216,6 @@ void InfluxdbCommonWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable, resolvers.emplace_back("service", service); resolvers.emplace_back("host", host); - String prefix; - - double ts = cr->GetExecutionEnd(); - // Clone the template and perform an in-place macro expansion of measurement and tag values Dictionary::Ptr tmpl_clean = service ? GetServiceTemplate() : GetHostTemplate(); Dictionary::Ptr tmpl = static_pointer_cast(tmpl_clean->ShallowClone()); @@ -253,56 +240,9 @@ void InfluxdbCommonWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable, tmpl->Set("tags", tags); } - CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); - - Array::Ptr perfdata = cr->GetPerformanceData(); - - if (perfdata) { - ObjectLock olock(perfdata); - for (const Value& val : perfdata) { - PerfdataValue::Ptr pdv; - - if (val.IsObjectType()) - pdv = val; - else { - try { - pdv = PerfdataValue::Parse(val); - } catch (const std::exception&) { - Log(LogWarning, GetReflectionType()->GetName()) - << "Ignoring invalid perfdata for checkable '" - << checkable->GetName() << "' and command '" - << checkCommand->GetName() << "' with value: " << val; - continue; - } - } - - Dictionary::Ptr fields = new Dictionary(); - fields->Set("value", pdv->GetValue()); - - if (GetEnableSendThresholds()) { - if (!pdv->GetCrit().IsEmpty()) - fields->Set("crit", pdv->GetCrit()); - if (!pdv->GetWarn().IsEmpty()) - fields->Set("warn", pdv->GetWarn()); - if (!pdv->GetMin().IsEmpty()) - fields->Set("min", pdv->GetMin()); - if (!pdv->GetMax().IsEmpty()) - fields->Set("max", pdv->GetMax()); - } - if (!pdv->GetUnit().IsEmpty()) { - fields->Set("unit", pdv->GetUnit()); - } - - SendMetric(checkable, tmpl, pdv->GetLabel(), fields, ts); - } - } - + Dictionary::Ptr fields; if (GetEnableSendMetadata()) { - Host::Ptr host; - Service::Ptr service; - tie(host, service) = GetHostService(checkable); - - Dictionary::Ptr fields = new Dictionary(); + fields = new Dictionary(); if (service) fields->Set("state", new InfluxdbInteger(service->GetState())); @@ -317,9 +257,57 @@ void InfluxdbCommonWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable, fields->Set("acknowledgement", new InfluxdbInteger(checkable->GetAcknowledgement())); fields->Set("latency", cr->CalculateLatency()); fields->Set("execution_time", cr->CalculateExecutionTime()); - - SendMetric(checkable, tmpl, Empty, fields, ts); } + + m_WorkQueue.Enqueue([this, checkable, cr, tmpl = std::move(tmpl), metadataFields = std::move(fields)]() { + CONTEXT("Processing check result for '" << checkable->GetName() << "'"); + + double ts = cr->GetExecutionEnd(); + + if (Array::Ptr perfdata = cr->GetPerformanceData()) { + ObjectLock olock(perfdata); + for (const Value& val : perfdata) { + PerfdataValue::Ptr pdv; + + if (val.IsObjectType()) + pdv = val; + else { + try { + pdv = PerfdataValue::Parse(val); + } catch (const std::exception&) { + Log(LogWarning, GetReflectionType()->GetName()) + << "Ignoring invalid perfdata for checkable '" + << checkable->GetName() << "' and command '" + << checkable->GetCheckCommand()->GetName() << "' with value: " << val; + continue; + } + } + + Dictionary::Ptr fields = new Dictionary(); + fields->Set("value", pdv->GetValue()); + + if (GetEnableSendThresholds()) { + if (!pdv->GetCrit().IsEmpty()) + fields->Set("crit", pdv->GetCrit()); + if (!pdv->GetWarn().IsEmpty()) + fields->Set("warn", pdv->GetWarn()); + if (!pdv->GetMin().IsEmpty()) + fields->Set("min", pdv->GetMin()); + if (!pdv->GetMax().IsEmpty()) + fields->Set("max", pdv->GetMax()); + } + if (!pdv->GetUnit().IsEmpty()) { + fields->Set("unit", pdv->GetUnit()); + } + + SendMetric(checkable, tmpl, pdv->GetLabel(), fields, ts); + } + } + + if (metadataFields) { + SendMetric(checkable, tmpl, Empty, metadataFields, ts); + } + }, PriorityLow); } String InfluxdbCommonWriter::EscapeKeyOrTagValue(const String& str) @@ -364,6 +352,8 @@ String InfluxdbCommonWriter::EscapeValue(const Value& value) void InfluxdbCommonWriter::SendMetric(const Checkable::Ptr& checkable, const Dictionary::Ptr& tmpl, const String& label, const Dictionary::Ptr& fields, double ts) { + AssertOnWorkQueue(); + std::ostringstream msgbuf; msgbuf << EscapeKeyOrTagValue(tmpl->Get("measurement")); diff --git a/lib/perfdata/influxdbcommonwriter.hpp b/lib/perfdata/influxdbcommonwriter.hpp index 380b20c9f64..9d3427f7e3c 100644 --- a/lib/perfdata/influxdbcommonwriter.hpp +++ b/lib/perfdata/influxdbcommonwriter.hpp @@ -54,7 +54,6 @@ class InfluxdbCommonWriter : public ObjectImpl std::atomic_size_t m_DataBufferSize{0}; void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void CheckResultHandlerWQ(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void SendMetric(const Checkable::Ptr& checkable, const Dictionary::Ptr& tmpl, const String& label, const Dictionary::Ptr& fields, double ts); void FlushTimeout();