From 424929e66ae11a48524c40091f6be9c8e7483683 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 19 Mar 2019 09:38:32 +0100 Subject: [PATCH] Improve logging of OpenTsdbWriter --- lib/perfdata/opentsdbwriter.cpp | 48 +++++++++++++++++++-------------- lib/perfdata/opentsdbwriter.hpp | 6 +++-- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/perfdata/opentsdbwriter.cpp b/lib/perfdata/opentsdbwriter.cpp index df4583685..013d51148 100644 --- a/lib/perfdata/opentsdbwriter.cpp +++ b/lib/perfdata/opentsdbwriter.cpp @@ -3,6 +3,7 @@ #include "perfdata/opentsdbwriter.hpp" #include "perfdata/opentsdbwriter-ti.cpp" #include "icinga/service.hpp" +#include "icinga/checkcommand.hpp" #include "icinga/macroprocessor.hpp" #include "icinga/icingaapplication.hpp" #include "icinga/compatutility.hpp" @@ -134,18 +135,18 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C String escaped_serviceName = EscapeMetric(serviceName); metric = "icinga.service." + escaped_serviceName; - SendMetric(metric + ".state", tags, service->GetState(), ts); + SendMetric(checkable, metric + ".state", tags, service->GetState(), ts); } else { metric = "icinga.host"; - SendMetric(metric + ".state", tags, host->GetState(), ts); + SendMetric(checkable, metric + ".state", tags, host->GetState(), ts); } - SendMetric(metric + ".state_type", tags, checkable->GetStateType(), ts); - SendMetric(metric + ".reachable", tags, checkable->IsReachable(), ts); - SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts); - SendMetric(metric + ".acknowledgement", tags, checkable->GetAcknowledgement(), ts); + SendMetric(checkable, metric + ".state_type", tags, checkable->GetStateType(), ts); + SendMetric(checkable, metric + ".reachable", tags, checkable->IsReachable(), ts); + SendMetric(checkable, metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts); + SendMetric(checkable, metric + ".acknowledgement", tags, checkable->GetAcknowledgement(), ts); - SendPerfdata(metric, tags, cr, ts); + SendPerfdata(checkable, metric, tags, cr, ts); metric = "icinga.check"; @@ -158,19 +159,22 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C tags["type"] = "host"; } - SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts); - SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts); - SendMetric(metric + ".latency", tags, cr->CalculateLatency(), ts); - SendMetric(metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts); + SendMetric(checkable, metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts); + SendMetric(checkable, metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts); + SendMetric(checkable, metric + ".latency", tags, cr->CalculateLatency(), ts); + SendMetric(checkable, metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts); } -void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map& tags, const CheckResult::Ptr& cr, double ts) +void OpenTsdbWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& metric, + const std::map& tags, const CheckResult::Ptr& cr, double ts) { Array::Ptr perfdata = cr->GetPerformanceData(); if (!perfdata) return; + CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); + ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; @@ -182,7 +186,9 @@ void OpenTsdbWriter::SendPerfdata(const String& metric, const std::mapGetName() << "' and command '" + << checkCommand->GetName() << "' with value: " << val; continue; } } @@ -190,22 +196,24 @@ void OpenTsdbWriter::SendPerfdata(const String& metric, const std::mapGetLabel()); boost::algorithm::replace_all(escaped_key, "::", "."); - SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts); + SendMetric(checkable, metric + "." + escaped_key, tags, pdv->GetValue(), ts); if (pdv->GetCrit()) - SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts); + SendMetric(checkable, metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts); if (pdv->GetWarn()) - SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts); + SendMetric(checkable, metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts); if (pdv->GetMin()) - SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts); + SendMetric(checkable, metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts); if (pdv->GetMax()) - SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts); + SendMetric(checkable, metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts); } } -void OpenTsdbWriter::SendMetric(const String& metric, const std::map& tags, double value, double ts) +void OpenTsdbWriter::SendMetric(const Checkable::Ptr& checkable, const String& metric, + const std::map& tags, double value, double ts) { String tags_string = ""; + for (const Dictionary::Pair& tag : tags) { tags_string += " " + tag.first + "=" + Convert::ToString(tag.second); } @@ -219,7 +227,7 @@ void OpenTsdbWriter::SendMetric(const String& metric, const std::map(ts) << " " << Convert::ToString(value) << " " << tags_string; Log(LogDebug, "OpenTsdbWriter") - << "Add to metric list:'" << msgbuf.str() << "'."; + << "Checkable '" << checkable->GetName() << "' adds to metric list: '" << msgbuf.str() << "'."; /* do not send \n to debug log */ msgbuf << "\n"; diff --git a/lib/perfdata/opentsdbwriter.hpp b/lib/perfdata/opentsdbwriter.hpp index e499e6037..792377556 100644 --- a/lib/perfdata/opentsdbwriter.hpp +++ b/lib/perfdata/opentsdbwriter.hpp @@ -37,8 +37,10 @@ private: Timer::Ptr m_ReconnectTimer; void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); - void SendMetric(const String& metric, const std::map& tags, double value, double ts); - void SendPerfdata(const String& metric, const std::map& tags, const CheckResult::Ptr& cr, double ts); + void SendMetric(const Checkable::Ptr& checkable, const String& metric, + const std::map& tags, double value, double ts); + void SendPerfdata(const Checkable::Ptr& checkable, const String& metric, + const std::map& tags, const CheckResult::Ptr& cr, double ts); static String EscapeTag(const String& str); static String EscapeMetric(const String& str); -- 2.40.0