Make sure that names for Graphite metrics are properly escaped

refs #7334
This commit is contained in:
Gunnar Beutner 2014-10-12 13:30:39 +02:00
parent 57d945347f
commit 989125cc39
2 changed files with 16 additions and 25 deletions

View File

@ -106,32 +106,20 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
else else
host = static_pointer_cast<Host>(checkable); host = static_pointer_cast<Host>(checkable);
String hostName = host->GetName();
SanitizeMetric(hostName);
String prefix;
MacroProcessor::ResolverList resolvers; MacroProcessor::ResolverList resolvers;
if (service) if (service)
resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("service", service));
resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("host", host));
resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
if (service) { String prefix;
String serviceName = service->GetShortName();
SanitizeMetric(serviceName);
/* custom prefix or default pattern */ if (service) {
prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr); prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric);
if (prefix.IsEmpty())
prefix = "icinga." + hostName + "." + serviceName;
SendMetric(prefix, "state", service->GetState()); SendMetric(prefix, "state", service->GetState());
} else { } else {
/* custom prefix or default pattern */ prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric);
prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr);
if (prefix.IsEmpty())
prefix = "icinga." + hostName;
SendMetric(prefix, "state", host->GetState()); SendMetric(prefix, "state", host->GetState());
} }
@ -168,8 +156,7 @@ void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr&
} }
} }
String escaped_key = pdv->GetLabel(); String escaped_key = EscapeMetric(pdv->GetLabel());
SanitizeMetric(escaped_key);
boost::algorithm::replace_all(escaped_key, "::", "."); boost::algorithm::replace_all(escaped_key, "::", ".");
SendMetric(prefix, escaped_key, pdv->GetValue()); SendMetric(prefix, escaped_key, pdv->GetValue());
@ -210,11 +197,15 @@ void GraphiteWriter::SendMetric(const String& prefix, const String& name, double
} }
} }
void GraphiteWriter::SanitizeMetric(String& str) String GraphiteWriter::EscapeMetric(const String& str)
{ {
boost::replace_all(str, " ", "_"); String result = str;
boost::replace_all(str, ".", "_");
boost::replace_all(str, "-", "_"); boost::replace_all(result, " ", "_");
boost::replace_all(str, "\\", "_"); boost::replace_all(result, ".", "_");
boost::replace_all(str, "/", "_"); boost::replace_all(result, "-", "_");
boost::replace_all(result, "\\", "_");
boost::replace_all(result, "/", "_");
return result;
} }

View File

@ -54,7 +54,7 @@ private:
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
void SendMetric(const String& prefix, const String& name, double value); void SendMetric(const String& prefix, const String& name, double value);
void SendPerfdata(const String& prefix, const CheckResult::Ptr& cr); void SendPerfdata(const String& prefix, const CheckResult::Ptr& cr);
static void SanitizeMetric(String& str); static String EscapeMetric(const String& str);
void ReconnectTimerHandler(void); void ReconnectTimerHandler(void);
}; };