]> granicus.if.org Git - icinga2/commitdiff
Make sure that names for Graphite metrics are properly escaped
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 12 Oct 2014 11:30:39 +0000 (13:30 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 12 Oct 2014 11:30:39 +0000 (13:30 +0200)
refs #7334

components/perfdata/graphitewriter.cpp
components/perfdata/graphitewriter.hpp

index 9e9dd8fe62b7c0b476cf9ea1e2f1fa5669151e29..b2deea515478413b288480ba62c88fa11f8b3677 100644 (file)
@@ -106,32 +106,20 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
        else
                host = static_pointer_cast<Host>(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;
 }
index c2ca0519168531653f62c1e82d827e7563565494..6e9520b5bd05d99ed60a45cde7c099f5483dfc91 100644 (file)
@@ -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);
 };