Optimize GraphiteWriter::SendMetric.

Fixes #5037
This commit is contained in:
Gunnar Beutner 2013-11-08 15:42:46 +01:00
parent 0cd2da410c
commit 0a67e760bd
4 changed files with 40 additions and 36 deletions

View File

@ -84,13 +84,22 @@ void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const Dicti
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata()) if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata())
return; 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 */ /* basic metrics */
SendMetric(service, "current_attempt", service->GetCheckAttempt()); SendMetric(prefix, "current_attempt", service->GetCheckAttempt());
SendMetric(service, "max_check_attempts", service->GetMaxCheckAttempts()); SendMetric(prefix, "max_check_attempts", service->GetMaxCheckAttempts());
SendMetric(service, "state_type", service->GetStateType()); SendMetric(prefix, "state_type", service->GetStateType());
SendMetric(service, "state", service->GetState()); SendMetric(prefix, "state", service->GetState());
SendMetric(service, "latency", Service::CalculateLatency(cr)); SendMetric(prefix, "latency", Service::CalculateLatency(cr));
SendMetric(service, "execution_time", Service::CalculateExecutionTime(cr)); SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr));
Value pdv = cr->Get("performance_data"); Value pdv = cr->Get("performance_data");
@ -109,23 +118,16 @@ void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const Dicti
else else
valueNum = static_cast<PerfdataValue::Ptr>(value)->GetValue(); 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 */ std::ostringstream msgbuf;
String hostName = service->GetHost()->GetName(); msgbuf << prefix << "." << name << " " << value << " " << static_cast<long>(Utility::GetTime()) << "\n";
String serviceName = service->GetShortName();
SanitizeMetric(hostName); String metric = msgbuf.str();
SanitizeMetric(serviceName);
String metricPrefix = hostName + "." + serviceName;
String graphitePrefix = "icinga";
String metric = graphitePrefix + "." + metricPrefix + "." + name + " " + Convert::ToString(value) + " " + Convert::ToString(static_cast<long>(Utility::GetTime())) + "\n";
Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'."); Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'.");
ObjectLock olock(this); ObjectLock olock(this);

View File

@ -50,7 +50,7 @@ private:
Timer::Ptr m_ReconnectTimer; Timer::Ptr m_ReconnectTimer;
void CheckResultHandler(const Service::Ptr& service, const Dictionary::Ptr& cr); 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); static void SanitizeMetric(String& str);
void ReconnectTimerHandler(void); void ReconnectTimerHandler(void);

View File

@ -22,22 +22,8 @@
using namespace icinga; 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) bool Convert::ToBool(const String& val)
{ {
return (ToLong(val) != 0); return (ToLong(val) != 0);
} }
String Convert::ToString(const Value& val)
{
return static_cast<String>(val);
}

View File

@ -22,6 +22,7 @@
#include "base/i2-base.h" #include "base/i2-base.h"
#include "base/value.h" #include "base/value.h"
#include <boost/lexical_cast.hpp>
namespace icinga namespace icinga
{ {
@ -34,10 +35,25 @@ namespace icinga
class I2_BASE_API Convert class I2_BASE_API Convert
{ {
public: public:
static long ToLong(const String& val); template<typename T>
static double ToDouble(const String& val); 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 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: private:
Convert(void); Convert(void);