From: Gunnar Beutner Date: Sun, 12 Oct 2014 11:30:39 +0000 (+0200) Subject: Make sure that names for Graphite metrics are properly escaped X-Git-Tag: v2.2.0~408 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=989125cc3937c2f186277feb067806bc18cc2695;p=icinga2 Make sure that names for Graphite metrics are properly escaped refs #7334 --- diff --git a/components/perfdata/graphitewriter.cpp b/components/perfdata/graphitewriter.cpp index 9e9dd8fe6..b2deea515 100644 --- a/components/perfdata/graphitewriter.cpp +++ b/components/perfdata/graphitewriter.cpp @@ -106,32 +106,20 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C else host = static_pointer_cast(checkable); - String hostName = host->GetName(); - SanitizeMetric(hostName); - - String prefix; - MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); - if (service) { - String serviceName = service->GetShortName(); - SanitizeMetric(serviceName); + String prefix; - /* custom prefix or default pattern */ - prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr); - if (prefix.IsEmpty()) - prefix = "icinga." + hostName + "." + serviceName; + if (service) { + prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric); SendMetric(prefix, "state", service->GetState()); } else { - /* custom prefix or default pattern */ - prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr); - if (prefix.IsEmpty()) - prefix = "icinga." + hostName; + prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr, NULL, &GraphiteWriter::EscapeMetric); SendMetric(prefix, "state", host->GetState()); } @@ -168,8 +156,7 @@ void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& } } - String escaped_key = pdv->GetLabel(); - SanitizeMetric(escaped_key); + String escaped_key = EscapeMetric(pdv->GetLabel()); boost::algorithm::replace_all(escaped_key, "::", "."); 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, " ", "_"); - boost::replace_all(str, ".", "_"); - boost::replace_all(str, "-", "_"); - boost::replace_all(str, "\\", "_"); - boost::replace_all(str, "/", "_"); + String result = str; + + boost::replace_all(result, " ", "_"); + boost::replace_all(result, ".", "_"); + boost::replace_all(result, "-", "_"); + boost::replace_all(result, "\\", "_"); + boost::replace_all(result, "/", "_"); + + return result; } diff --git a/components/perfdata/graphitewriter.hpp b/components/perfdata/graphitewriter.hpp index c2ca05191..6e9520b5b 100644 --- a/components/perfdata/graphitewriter.hpp +++ b/components/perfdata/graphitewriter.hpp @@ -54,7 +54,7 @@ private: void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void SendMetric(const String& prefix, const String& name, double value); void SendPerfdata(const String& prefix, const CheckResult::Ptr& cr); - static void SanitizeMetric(String& str); + static String EscapeMetric(const String& str); void ReconnectTimerHandler(void); };