From: Simon Murray Date: Thu, 6 Oct 2016 09:49:00 +0000 (+0100) Subject: InfluxDB: Always Write Out Metadata X-Git-Tag: v2.6.0~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c37a00daf2d711b1b603712307a47c9b8130507;p=icinga2 InfluxDB: Always Write Out Metadata 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 --- diff --git a/lib/perfdata/influxdbwriter.cpp b/lib/perfdata/influxdbwriter.cpp index 5163f0d38..b594e3ab4 100644 --- a/lib/perfdata/influxdbwriter.cpp +++ b/lib/perfdata/influxdbwriter.cpp @@ -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()) + 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()) - 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);