[icinga-checkins] icinga.org: icinga2/master: InfluxDB: Always Write Out Metadata

git at icinga.org git at icinga.org
Wed Nov 9 16:11:16 CET 2016


Module: icinga2
Branch: master
Commit: 2c37a00daf2d711b1b603712307a47c9b8130507
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=2c37a00daf2d711b1b603712307a47c9b8130507

Author: Simon Murray <spjmurray at yahoo.co.uk>
Date:   Thu Oct  6 10:49:00 2016 +0100

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 <michael.friedrich at icinga.com>

---

 lib/perfdata/influxdbwriter.cpp |  106 +++++++++++++++++++++------------------
 1 file changed, 56 insertions(+), 50 deletions(-)

diff --git a/lib/perfdata/influxdbwriter.cpp b/lib/perfdata/influxdbwriter.cpp
index 5163f0d..b594e3a 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<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);



More information about the icinga-checkins mailing list