]> granicus.if.org Git - icinga2/commitdiff
Optimize GraphiteWriter::SendMetric.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 8 Nov 2013 14:42:46 +0000 (15:42 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 8 Nov 2013 14:43:14 +0000 (15:43 +0100)
Fixes #5037

components/perfdata/graphitewriter.cpp
components/perfdata/graphitewriter.h
lib/base/convert.cpp
lib/base/convert.h

index 71920c5740f9d2f1dc1a637457c09ccf433a6707..edeeb734fbf797c1d9bf3b3f3d37bad0f494bfd0 100644 (file)
@@ -84,13 +84,22 @@ void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const Dicti
        if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata())
                return;
 
+       /* TODO: sanitize host and service names */
+       String hostName = service->GetHost()->GetName();
+       String serviceName = service->GetShortName();   
+
+       SanitizeMetric(hostName);
+       SanitizeMetric(serviceName);
+
+       String prefix = "icinga." + hostName + "." + serviceName;
+
        /* basic metrics */
-       SendMetric(service, "current_attempt", service->GetCheckAttempt());
-       SendMetric(service, "max_check_attempts", service->GetMaxCheckAttempts());
-       SendMetric(service, "state_type", service->GetStateType());
-       SendMetric(service, "state", service->GetState());
-       SendMetric(service, "latency", Service::CalculateLatency(cr));
-       SendMetric(service, "execution_time", Service::CalculateExecutionTime(cr));
+       SendMetric(prefix, "current_attempt", service->GetCheckAttempt());
+       SendMetric(prefix, "max_check_attempts", service->GetMaxCheckAttempts());
+       SendMetric(prefix, "state_type", service->GetStateType());
+       SendMetric(prefix, "state", service->GetState());
+       SendMetric(prefix, "latency", Service::CalculateLatency(cr));
+       SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr));
 
        Value pdv = cr->Get("performance_data");
 
@@ -109,23 +118,16 @@ void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const Dicti
                else
                        valueNum = static_cast<PerfdataValue::Ptr>(value)->GetValue();
 
-               SendMetric(service, key, valueNum);
+               SendMetric(prefix, key, valueNum);
        }
 }
 
-void GraphiteWriter::SendMetric(const Service::Ptr& service, const String& name, double value)
+void GraphiteWriter::SendMetric(const String& prefix, const String& name, double value)
 {
-       /* TODO: sanitize host and service names */
-       String hostName = service->GetHost()->GetName();
-       String serviceName = service->GetShortName();   
-
-       SanitizeMetric(hostName);
-       SanitizeMetric(serviceName);
-
-       String metricPrefix = hostName + "." + serviceName;
-       String graphitePrefix = "icinga";
+       std::ostringstream msgbuf;
+       msgbuf << prefix << "." << name << " " << value << " " << static_cast<long>(Utility::GetTime()) << "\n";
 
-       String metric = graphitePrefix + "." + metricPrefix + "." + name + " " + Convert::ToString(value) + " " + Convert::ToString(static_cast<long>(Utility::GetTime())) + "\n";
+       String metric = msgbuf.str();
        Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'.");
 
        ObjectLock olock(this);
index 133790fd155d270a73743c49a8a08c07fe4231ba..697ce393d7847770826f52175a227052a106cf9a 100644 (file)
@@ -50,7 +50,7 @@ private:
         Timer::Ptr m_ReconnectTimer;
 
        void CheckResultHandler(const Service::Ptr& service, const Dictionary::Ptr& cr);
-        void SendMetric(const Service::Ptr& service, const String& name, double value);
+        void SendMetric(const String& prefix, const String& name, double value);
         static void SanitizeMetric(String& str);
 
         void ReconnectTimerHandler(void);
index 6e9299c189cecad3f8d52cfe3b3c8061764aede1..b2727df749e2a4b9f76b8082925e1deb4f62bc3c 100644 (file)
 
 using namespace icinga;
 
-long Convert::ToLong(const String& val)
-{
-       return boost::lexical_cast<long>(val);
-}
-
-double Convert::ToDouble(const String& val)
-{
-       return boost::lexical_cast<double>(val);
-}
-
 bool Convert::ToBool(const String& val)
 {
        return (ToLong(val) != 0);
 }
 
-String Convert::ToString(const Value& val)
-{
-       return static_cast<String>(val);
-}
index 2301136e36eed2d57da2fc5a6c7bda0677b1758d..435a2fa388f0ffd7d513464b36be8f4caab6921c 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "base/i2-base.h"
 #include "base/value.h"
+#include <boost/lexical_cast.hpp>
 
 namespace icinga
 {
@@ -34,10 +35,25 @@ namespace icinga
 class I2_BASE_API Convert
 {
 public:
-       static long ToLong(const String& val);
-       static double ToDouble(const String& val);
+       template<typename T>
+       static long ToLong(const T& val)
+       {
+               return boost::lexical_cast<long>(val);
+       }
+
+       template<typename T>
+       static double ToDouble(const T& val)
+       {
+               return boost::lexical_cast<double>(val);
+       }
+
        static bool ToBool(const String& val);
-       static String ToString(const Value& val);
+
+       template<typename T>
+       static String ToString(const T& val)
+       {
+               return boost::lexical_cast<String>(val);
+       }
 
 private:
        Convert(void);