]> granicus.if.org Git - icinga2/commitdiff
InfluxDB: Always Write Out Metadata
authorSimon Murray <spjmurray@yahoo.co.uk>
Thu, 6 Oct 2016 09:49:00 +0000 (10:49 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 9 Nov 2016 15:10:21 +0000 (16:10 +0100)
Previously the logic would just bail out if no performance data was associated with a
check, the problem being that check metadata was skipped too.  This rearranges the code
to dump out performance metrics if they exist, then dump out metadata if requested.  This
also fixes an issue whereby metadata was being sent for every performance data in the
check result, rather than just once, so we save a bit of bandwidth as a result.

fixes #12276

Signed-off-by: Michael Friedrich <michael.friedrich@icinga.com>
lib/perfdata/influxdbwriter.cpp

index 5163f0d3849bc083b23005f4dd5e6841c4ce47f5..b594e3ab42fceed7aa4d2d992f4230e149e946a3 100644 (file)
@@ -171,61 +171,63 @@ String InfluxdbWriter::FormatBoolean(const bool val)
 void InfluxdbWriter::SendPerfdata(const Dictionary::Ptr& tmpl, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, double ts)
 {
        Array::Ptr perfdata = cr->GetPerformanceData();
+       if (perfdata) {
+               ObjectLock olock(perfdata);
+               for (const Value& val : perfdata) {
+                       PerfdataValue::Ptr pdv;
+
+                       if (val.IsObjectType<PerfdataValue>())
+                               pdv = val;
+                       else {
+                               try {
+                                       pdv = PerfdataValue::Parse(val);
+                               } catch (const std::exception&) {
+                                       Log(LogWarning, "InfluxdbWriter")
+                                           << "Ignoring invalid perfdata value: " << val;
+                                       continue;
+                               }
+                       }
 
-       if (!perfdata)
-               return;
-
-       ObjectLock olock(perfdata);
-       for (const Value& val : perfdata) {
-               PerfdataValue::Ptr pdv;
-
-               if (val.IsObjectType<PerfdataValue>())
-                       pdv = val;
-               else {
-                       try {
-                               pdv = PerfdataValue::Parse(val);
-                       } catch (const std::exception&) {
-                               Log(LogWarning, "InfluxdbWriter")
-                                   << "Ignoring invalid perfdata value: " << val;
-                               continue;
+                       Dictionary::Ptr fields = new Dictionary();
+                       fields->Set(String("value"), pdv->GetValue());
+
+                       if (GetEnableSendThresholds()) {
+                               if (pdv->GetCrit())
+                                       fields->Set(String("crit"), pdv->GetCrit());
+                               if (pdv->GetWarn())
+                                       fields->Set(String("warn"), pdv->GetWarn());
+                               if (pdv->GetMin())
+                                       fields->Set(String("min"), pdv->GetMin());
+                               if (pdv->GetMax())
+                                       fields->Set(String("max"), pdv->GetMax());
                        }
-               }
 
-               Dictionary::Ptr fields = new Dictionary();
-               fields->Set(String("value"), pdv->GetValue());
-
-               if (GetEnableSendThresholds()) {
-                       if (pdv->GetCrit())
-                               fields->Set(String("crit"), pdv->GetCrit());
-                       if (pdv->GetWarn())
-                               fields->Set(String("warn"), pdv->GetWarn());
-                       if (pdv->GetMin())
-                               fields->Set(String("min"), pdv->GetMin());
-                       if (pdv->GetMax())
-                               fields->Set(String("max"), pdv->GetMax());
+                       SendMetric(tmpl, pdv->GetLabel(), fields, ts);
                }
+       }
 
-               if (GetEnableSendMetadata()) {
-                       Host::Ptr host;
-                       Service::Ptr service;
-                       boost::tie(host, service) = GetHostService(checkable);
-
-                       if (service)
-                               fields->Set(String("state"), FormatInteger(service->GetState()));
-                       else
-                               fields->Set(String("state"), FormatInteger(host->GetState()));
-
-                       fields->Set(String("current_attempt"), FormatInteger(checkable->GetCheckAttempt()));
-                       fields->Set(String("max_check_attempts"), FormatInteger(checkable->GetMaxCheckAttempts()));
-                       fields->Set(String("state_type"), FormatInteger(checkable->GetStateType()));
-                       fields->Set(String("reachable"), FormatBoolean(checkable->IsReachable()));
-                       fields->Set(String("downtime_depth"), FormatInteger(checkable->GetDowntimeDepth()));
-                       fields->Set(String("acknowledgement"), FormatInteger(checkable->GetAcknowledgement()));
-                       fields->Set(String("latency"), cr->CalculateLatency());
-                       fields->Set(String("execution_time"), cr->CalculateExecutionTime());
-               }
+       if (GetEnableSendMetadata()) {
+               Host::Ptr host;
+               Service::Ptr service;
+               boost::tie(host, service) = GetHostService(checkable);
+
+               Dictionary::Ptr fields = new Dictionary();
 
-               SendMetric(tmpl, pdv->GetLabel(), fields, ts);
+               if (service)
+                       fields->Set(String("state"), FormatInteger(service->GetState()));
+               else
+                       fields->Set(String("state"), FormatInteger(host->GetState()));
+
+               fields->Set(String("current_attempt"), FormatInteger(checkable->GetCheckAttempt()));
+               fields->Set(String("max_check_attempts"), FormatInteger(checkable->GetMaxCheckAttempts()));
+               fields->Set(String("state_type"), FormatInteger(checkable->GetStateType()));
+               fields->Set(String("reachable"), FormatBoolean(checkable->IsReachable()));
+               fields->Set(String("downtime_depth"), FormatInteger(checkable->GetDowntimeDepth()));
+               fields->Set(String("acknowledgement"), FormatInteger(checkable->GetAcknowledgement()));
+               fields->Set(String("latency"), cr->CalculateLatency());
+               fields->Set(String("execution_time"), cr->CalculateExecutionTime());
+
+               SendMetric(tmpl, String(), fields, ts);
        }
 }
 
@@ -294,7 +296,11 @@ void InfluxdbWriter::SendMetric(const Dictionary::Ptr& tmpl, const String& label
                }
        }
 
-       msgbuf << ",metric=" << EscapeKey(label) << " ";
+       // Label is may be empty in the case of metadata
+       if (!label.IsEmpty())
+               msgbuf << ",metric=" << EscapeKey(label);
+
+       msgbuf << " ";
 
        bool first = true;
        ObjectLock fieldLock(fields);