From da8613acf931147c2751f6c3dcf8b0de757cc13c Mon Sep 17 00:00:00 2001 From: Tobias von der Krone Date: Tue, 8 Sep 2015 06:34:33 +0200 Subject: [PATCH] Add timestamp support for OpenTSDB fixes #9183 --- lib/perfdata/opentsdbwriter.cpp | 38 +++++++++++++++++---------------- lib/perfdata/opentsdbwriter.hpp | 4 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/perfdata/opentsdbwriter.cpp b/lib/perfdata/opentsdbwriter.cpp index 8fa0f7131..101fa8f41 100644 --- a/lib/perfdata/opentsdbwriter.cpp +++ b/lib/perfdata/opentsdbwriter.cpp @@ -113,22 +113,24 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C String escaped_hostName = EscapeMetric(host->GetName()); tags["host"] = escaped_hostName; + double ts = cr->GetExecutionEnd(); + if (service) { String serviceName = service->GetShortName(); String escaped_serviceName = EscapeMetric(serviceName); metric = "icinga.service." + escaped_serviceName; - SendMetric(metric + ".state", tags, service->GetState()); + SendMetric(metric + ".state", tags, service->GetState(), ts); } else { metric = "icinga.host"; - SendMetric(metric + ".state", tags, host->GetState()); + SendMetric(metric + ".state", tags, host->GetState(), ts); } - SendMetric(metric + ".state_type", tags, checkable->GetStateType()); - SendMetric(metric + ".reachable", tags, checkable->IsReachable()); - SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth()); + SendMetric(metric + ".state_type", tags, checkable->GetStateType(), ts); + SendMetric(metric + ".reachable", tags, checkable->IsReachable(), ts); + SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts); - SendPerfdata(metric, tags, cr); + SendPerfdata(metric, tags, cr, ts); metric = "icinga.check"; @@ -141,13 +143,13 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C tags["type"] = "host"; } - SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt()); - SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts()); - SendMetric(metric + ".latency", tags, Service::CalculateLatency(cr)); - SendMetric(metric + ".execution_time", tags, Service::CalculateExecutionTime(cr)); + SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts); + SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts); + SendMetric(metric + ".latency", tags, Service::CalculateLatency(cr), ts); + SendMetric(metric + ".execution_time", tags, Service::CalculateExecutionTime(cr), ts); } -void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map& tags, const CheckResult::Ptr& cr) +void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map& tags, const CheckResult::Ptr& cr, double ts) { Array::Ptr perfdata = cr->GetPerformanceData(); @@ -173,20 +175,20 @@ void OpenTsdbWriter::SendPerfdata(const String& metric, const std::mapGetLabel()); boost::algorithm::replace_all(escaped_key, "::", "."); - SendMetric(metric + "." + escaped_key, tags, pdv->GetValue()); + SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts); if (pdv->GetCrit()) - SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit()); + SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts); if (pdv->GetWarn()) - SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn()); + SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts); if (pdv->GetMin()) - SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin()); + SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts); if (pdv->GetMax()) - SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax()); + SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts); } } -void OpenTsdbWriter::SendMetric(const String& metric, const std::map& tags, double value) +void OpenTsdbWriter::SendMetric(const String& metric, const std::map& tags, double value, double ts) { String tags_string = ""; BOOST_FOREACH(const Dictionary::Pair& tag, tags) { @@ -199,7 +201,7 @@ void OpenTsdbWriter::SendMetric(const String& metric, const std::map * "tags" must include at least one tag, we use "host=HOSTNAME" */ - msgbuf << "put " << metric << " " << static_cast(Utility::GetTime()) << " " << Convert::ToString(value) << " " << tags_string; + msgbuf << "put " << metric << " " << static_cast(ts) << " " << Convert::ToString(value) << " " << tags_string; Log(LogDebug, "OpenTsdbWriter") << "Add to metric list:'" << msgbuf.str() << "'."; diff --git a/lib/perfdata/opentsdbwriter.hpp b/lib/perfdata/opentsdbwriter.hpp index 94868ab6b..2ef0ef0e9 100644 --- a/lib/perfdata/opentsdbwriter.hpp +++ b/lib/perfdata/opentsdbwriter.hpp @@ -52,8 +52,8 @@ 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); - void SendPerfdata(const String& metric, const std::map& tags, 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); static String EscapeTag(const String& str); static String EscapeMetric(const String& str);