Improve logging of GraphiteWriter
authorMichael Friedrich <michael.friedrich@icinga.com>
Thu, 17 Jan 2019 07:53:55 +0000 (08:53 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 19 Mar 2019 08:39:09 +0000 (09:39 +0100)
lib/perfdata/graphitewriter.cpp
lib/perfdata/graphitewriter.hpp

index b0b0142604a43e2e503755bee5f783852ae5925c..aee142e5a04c87f5c89515128951ea4acc728239 100644 (file)
@@ -3,6 +3,7 @@
 #include "perfdata/graphitewriter.hpp"
 #include "perfdata/graphitewriter-ti.cpp"
 #include "icinga/service.hpp"
+#include "icinga/checkcommand.hpp"
 #include "icinga/macroprocessor.hpp"
 #include "icinga/icingaapplication.hpp"
 #include "base/tcpsocket.hpp"
@@ -243,31 +244,33 @@ void GraphiteWriter::CheckResultHandlerInternal(const Checkable::Ptr& checkable,
 
        if (GetEnableSendMetadata()) {
                if (service) {
-                       SendMetric(prefixMetadata, "state", service->GetState(), ts);
+                       SendMetric(checkable, prefixMetadata, "state", service->GetState(), ts);
                } else {
-                       SendMetric(prefixMetadata, "state", host->GetState(), ts);
+                       SendMetric(checkable, prefixMetadata, "state", host->GetState(), ts);
                }
 
-               SendMetric(prefixMetadata, "current_attempt", checkable->GetCheckAttempt(), ts);
-               SendMetric(prefixMetadata, "max_check_attempts", checkable->GetMaxCheckAttempts(), ts);
-               SendMetric(prefixMetadata, "state_type", checkable->GetStateType(), ts);
-               SendMetric(prefixMetadata, "reachable", checkable->IsReachable(), ts);
-               SendMetric(prefixMetadata, "downtime_depth", checkable->GetDowntimeDepth(), ts);
-               SendMetric(prefixMetadata, "acknowledgement", checkable->GetAcknowledgement(), ts);
-               SendMetric(prefixMetadata, "latency", cr->CalculateLatency(), ts);
-               SendMetric(prefixMetadata, "execution_time", cr->CalculateExecutionTime(), ts);
+               SendMetric(checkable, prefixMetadata, "current_attempt", checkable->GetCheckAttempt(), ts);
+               SendMetric(checkable, prefixMetadata, "max_check_attempts", checkable->GetMaxCheckAttempts(), ts);
+               SendMetric(checkable, prefixMetadata, "state_type", checkable->GetStateType(), ts);
+               SendMetric(checkable, prefixMetadata, "reachable", checkable->IsReachable(), ts);
+               SendMetric(checkable, prefixMetadata, "downtime_depth", checkable->GetDowntimeDepth(), ts);
+               SendMetric(checkable, prefixMetadata, "acknowledgement", checkable->GetAcknowledgement(), ts);
+               SendMetric(checkable, prefixMetadata, "latency", cr->CalculateLatency(), ts);
+               SendMetric(checkable, prefixMetadata, "execution_time", cr->CalculateExecutionTime(), ts);
        }
 
-       SendPerfdata(prefixPerfdata, cr, ts);
+       SendPerfdata(checkable, prefixPerfdata, cr, ts);
 }
 
-void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& cr, double ts)
+void GraphiteWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr, double ts)
 {
        Array::Ptr perfdata = cr->GetPerformanceData();
 
        if (!perfdata)
                return;
 
+       CheckCommand::Ptr checkCommand = checkable->GetCheckCommand();
+
        ObjectLock olock(perfdata);
        for (const Value& val : perfdata) {
                PerfdataValue::Ptr pdv;
@@ -279,35 +282,37 @@ void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr&
                                pdv = PerfdataValue::Parse(val);
                        } catch (const std::exception&) {
                                Log(LogWarning, "GraphiteWriter")
-                                       << "Ignoring invalid perfdata value: " << val;
+                                       << "Ignoring invalid perfdata for checkable '"
+                                       << checkable->GetName() << "' and command '"
+                                       << checkCommand->GetName() << "' with value: " << val;
                                continue;
                        }
                }
 
                String escapedKey = EscapeMetricLabel(pdv->GetLabel());
 
-               SendMetric(prefix, escapedKey + ".value", pdv->GetValue(), ts);
+               SendMetric(checkable, prefix, escapedKey + ".value", pdv->GetValue(), ts);
 
                if (GetEnableSendThresholds()) {
                        if (pdv->GetCrit())
-                               SendMetric(prefix, escapedKey + ".crit", pdv->GetCrit(), ts);
+                               SendMetric(checkable, prefix, escapedKey + ".crit", pdv->GetCrit(), ts);
                        if (pdv->GetWarn())
-                               SendMetric(prefix, escapedKey + ".warn", pdv->GetWarn(), ts);
+                               SendMetric(checkable, prefix, escapedKey + ".warn", pdv->GetWarn(), ts);
                        if (pdv->GetMin())
-                               SendMetric(prefix, escapedKey + ".min", pdv->GetMin(), ts);
+                               SendMetric(checkable, prefix, escapedKey + ".min", pdv->GetMin(), ts);
                        if (pdv->GetMax())
-                               SendMetric(prefix, escapedKey + ".max", pdv->GetMax(), ts);
+                               SendMetric(checkable, prefix, escapedKey + ".max", pdv->GetMax(), ts);
                }
        }
 }
 
-void GraphiteWriter::SendMetric(const String& prefix, const String& name, double value, double ts)
+void GraphiteWriter::SendMetric(const Checkable::Ptr& checkable, const String& prefix, const String& name, double value, double ts)
 {
        std::ostringstream msgbuf;
        msgbuf << prefix << "." << name << " " << Convert::ToString(value) << " " << static_cast<long>(ts);
 
        Log(LogDebug, "GraphiteWriter")
-               << "Add to metric list: '" << msgbuf.str() << "'.";
+               << "Checkable '" << checkable->GetName() << "' adds to metric list: '" << msgbuf.str() << "'.";
 
        // do not send \n to debug log
        msgbuf << "\n";
index 8db8e49c7a16e266b8eacaea45d9630a4ac7ce84..2756c0fd451fd5f76431021e1eaca7ecb629eb3d 100644 (file)
@@ -45,8 +45,8 @@ private:
 
        void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
        void CheckResultHandlerInternal(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
-       void SendMetric(const String& prefix, const String& name, double value, double ts);
-       void SendPerfdata(const String& prefix, const CheckResult::Ptr& cr, double ts);
+       void SendMetric(const Checkable::Ptr& checkable, const String& prefix, const String& name, double value, double ts);
+       void SendPerfdata(const Checkable::Ptr& checkable, const String& prefix, const CheckResult::Ptr& cr, double ts);
        static String EscapeMetric(const String& str);
        static String EscapeMetricLabel(const String& str);
        static Value EscapeMacroMetric(const Value& value);