Add timestamp support for OpenTSDB

fixes #9183
This commit is contained in:
Tobias von der Krone 2015-09-08 06:34:33 +02:00
parent 5e06ad015a
commit da8613acf9
2 changed files with 22 additions and 20 deletions

View File

@ -113,22 +113,24 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
String escaped_hostName = EscapeMetric(host->GetName());
tags["host"] = escaped_hostName;
double ts = cr->GetExecutionEnd();
if (service) {
String serviceName = service->GetShortName();
String escaped_serviceName = EscapeMetric(serviceName);
metric = "icinga.service." + escaped_serviceName;
SendMetric(metric + ".state", tags, service->GetState());
SendMetric(metric + ".state", tags, service->GetState(), ts);
} else {
metric = "icinga.host";
SendMetric(metric + ".state", tags, host->GetState());
SendMetric(metric + ".state", tags, host->GetState(), ts);
}
SendMetric(metric + ".state_type", tags, checkable->GetStateType());
SendMetric(metric + ".reachable", tags, checkable->IsReachable());
SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth());
SendMetric(metric + ".state_type", tags, checkable->GetStateType(), ts);
SendMetric(metric + ".reachable", tags, checkable->IsReachable(), ts);
SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts);
SendPerfdata(metric, tags, cr);
SendPerfdata(metric, tags, cr, ts);
metric = "icinga.check";
@ -141,13 +143,13 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
tags["type"] = "host";
}
SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt());
SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts());
SendMetric(metric + ".latency", tags, Service::CalculateLatency(cr));
SendMetric(metric + ".execution_time", tags, Service::CalculateExecutionTime(cr));
SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts);
SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts);
SendMetric(metric + ".latency", tags, Service::CalculateLatency(cr), ts);
SendMetric(metric + ".execution_time", tags, Service::CalculateExecutionTime(cr), ts);
}
void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr)
void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts)
{
Array::Ptr perfdata = cr->GetPerformanceData();
@ -173,20 +175,20 @@ void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, S
String escaped_key = EscapeMetric(pdv->GetLabel());
boost::algorithm::replace_all(escaped_key, "::", ".");
SendMetric(metric + "." + escaped_key, tags, pdv->GetValue());
SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts);
if (pdv->GetCrit())
SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit());
SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts);
if (pdv->GetWarn())
SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn());
SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts);
if (pdv->GetMin())
SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin());
SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts);
if (pdv->GetMax())
SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax());
SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts);
}
}
void OpenTsdbWriter::SendMetric(const String& metric, const std::map<String, String>& tags, double value)
void OpenTsdbWriter::SendMetric(const String& metric, const std::map<String, String>& tags, double value, double ts)
{
String tags_string = "";
BOOST_FOREACH(const Dictionary::Pair& tag, tags) {
@ -199,7 +201,7 @@ void OpenTsdbWriter::SendMetric(const String& metric, const std::map<String, Str
* put <metric> <timestamp> <value> <tagk1=tagv1[ tagk2=tagv2 ...tagkN=tagvN]>
* "tags" must include at least one tag, we use "host=HOSTNAME"
*/
msgbuf << "put " << metric << " " << static_cast<long>(Utility::GetTime()) << " " << Convert::ToString(value) << " " << tags_string;
msgbuf << "put " << metric << " " << static_cast<long>(ts) << " " << Convert::ToString(value) << " " << tags_string;
Log(LogDebug, "OpenTsdbWriter")
<< "Add to metric list:'" << msgbuf.str() << "'.";

View File

@ -52,8 +52,8 @@ private:
Timer::Ptr m_ReconnectTimer;
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
void SendMetric(const String& metric, const std::map<String, String>& tags, double value);
void SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr);
void SendMetric(const String& metric, const std::map<String, String>& tags, double value, double ts);
void SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts);
static String EscapeTag(const String& str);
static String EscapeMetric(const String& str);