"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/perfdata/opentsdbwriter.cpp" between
icinga2-2.11.5.tar.gz and icinga2-2.12.0.tar.gz

About: Icinga 2 is an enterprise grade monitoring system which keeps watch over networks and any conceivable network resource.

opentsdbwriter.cpp  (icinga2-2.11.5):opentsdbwriter.cpp  (icinga2-2.12.0)
skipping to change at line 76 skipping to change at line 76
/** /**
* Resume is equivalent to Start, but with HA capabilities to resume at runtime. * Resume is equivalent to Start, but with HA capabilities to resume at runtime.
*/ */
void OpenTsdbWriter::Resume() void OpenTsdbWriter::Resume()
{ {
ObjectImpl<OpenTsdbWriter>::Resume(); ObjectImpl<OpenTsdbWriter>::Resume();
Log(LogInformation, "OpentsdbWriter") Log(LogInformation, "OpentsdbWriter")
<< "'" << GetName() << "' resumed."; << "'" << GetName() << "' resumed.";
ReadConfigTemplate(m_ServiceConfigTemplate, m_HostConfigTemplate);
m_ReconnectTimer = new Timer(); m_ReconnectTimer = new Timer();
m_ReconnectTimer->SetInterval(10); m_ReconnectTimer->SetInterval(10);
m_ReconnectTimer->OnTimerExpired.connect(std::bind(&OpenTsdbWriter::Recon nectTimerHandler, this)); m_ReconnectTimer->OnTimerExpired.connect(std::bind(&OpenTsdbWriter::Recon nectTimerHandler, this));
m_ReconnectTimer->Start(); m_ReconnectTimer->Start();
m_ReconnectTimer->Reschedule(0); m_ReconnectTimer->Reschedule(0);
Service::OnNewCheckResult.connect(std::bind(&OpenTsdbWriter::CheckResultH andler, this, _1, _2)); Service::OnNewCheckResult.connect(std::bind(&OpenTsdbWriter::CheckResultH andler, this, _1, _2));
} }
/** /**
skipping to change at line 116 skipping to change at line 118
void OpenTsdbWriter::ReconnectTimerHandler() void OpenTsdbWriter::ReconnectTimerHandler()
{ {
if (IsPaused()) if (IsPaused())
return; return;
SetShouldConnect(true); SetShouldConnect(true);
if (GetConnected()) if (GetConnected())
return; return;
double startTime = Utility::GetTime();
Log(LogNotice, "OpenTsdbWriter") Log(LogNotice, "OpenTsdbWriter")
<< "Reconnect to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() << "'."; << "Reconnecting to OpenTSDB TSD on host '" << GetHost() << "' po rt '" << GetPort() << "'.";
/* /*
* We're using telnet as input method. Future PRs may change this into us ing the HTTP API. * We're using telnet as input method. Future PRs may change this into us ing the HTTP API.
* http://opentsdb.net/docs/build/html/user_guide/writing/index.html#teln et * http://opentsdb.net/docs/build/html/user_guide/writing/index.html#teln et
*/ */
m_Stream = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
m_Stream = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext()
);
try { try {
icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort()); icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort());
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogWarning, "OpenTsdbWriter") Log(LogWarning, "OpenTsdbWriter")
<< "Can't connect to OpenTSDB on host '" << GetHost() << "' port '" << GetPort() << ".'"; << "Can't connect to OpenTSDB on host '" << GetHost() << "' port '" << GetPort() << ".'";
SetConnected(false);
return;
} }
SetConnected(true); SetConnected(true);
Log(LogInformation, "OpenTsdbWriter")
<< "Finished reconnecting to OpenTSDB in " << std::setw(2) << Uti
lity::GetTime() - startTime << " second(s).";
} }
/** /**
* Registered check result handler processing data. * Registered check result handler processing data.
* Calculates tags from the config. * Calculates tags from the config.
* *
* @param checkable Host/service object * @param checkable Host/service object
* @param cr Check result * @param cr Check result
*/ */
void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C heckResult::Ptr& cr) void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C heckResult::Ptr& cr)
skipping to change at line 155 skipping to change at line 165
if (IsPaused()) if (IsPaused())
return; return;
CONTEXT("Processing check result for '" + checkable->GetName() + "'"); CONTEXT("Processing check result for '" + checkable->GetName() + "'");
if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable- >GetEnablePerfdata()) if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable- >GetEnablePerfdata())
return; return;
Service::Ptr service = dynamic_pointer_cast<Service>(checkable); Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
Host::Ptr host; Host::Ptr host;
Dictionary::Ptr config_tmpl;
Dictionary::Ptr config_tmpl_tags;
String config_tmpl_metric;
if (service) if (service) {
host = service->GetHost(); host = service->GetHost();
else config_tmpl = m_ServiceConfigTemplate;
}
else {
host = static_pointer_cast<Host>(checkable); host = static_pointer_cast<Host>(checkable);
config_tmpl = m_HostConfigTemplate;
}
// Get the tags nested dictionary in the service/host template in the con
fig
if (config_tmpl) {
config_tmpl_tags = config_tmpl->Get("tags");
config_tmpl_metric = config_tmpl->Get("metric");
}
String metric; String metric;
std::map<String, String> tags; std::map<String, String> tags;
// Resolve macros in configuration template and build custom tag list
if (config_tmpl_tags || !config_tmpl_metric.IsEmpty()) {
// Configure config template macro resolver
MacroProcessor::ResolverList resolvers;
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance()
);
// Resolve macros for the service and host template config line
if (config_tmpl_tags) {
ObjectLock olock(config_tmpl_tags);
for (const Dictionary::Pair& pair : config_tmpl_tags) {
String missing_macro;
Value value = MacroProcessor::ResolveMacros(pair.
second, resolvers, cr, &missing_macro);
if (!missing_macro.IsEmpty()) {
Log(LogDebug, "OpenTsdbWriter")
<< "Unable to resolve macro:'" <<
missing_macro
<< "' for this host or service.";
continue;
}
String tagname = Convert::ToString(pair.first);
tags[tagname] = EscapeTag(value);
}
}
// Resolve macros for the metric config line
if (!config_tmpl_metric.IsEmpty()) {
String missing_macro;
Value value = MacroProcessor::ResolveMacros(config_tmpl_m
etric, resolvers, cr, &missing_macro);
if (!missing_macro.IsEmpty()) {
Log(LogDebug, "OpenTsdbWriter")
<< "Unable to resolve macro:'" << missing
_macro
<< "' for this host or service.";
}
else {
config_tmpl_metric = Convert::ToString(value);
}
}
}
String escaped_hostName = EscapeTag(host->GetName()); String escaped_hostName = EscapeTag(host->GetName());
tags["host"] = escaped_hostName; tags["host"] = escaped_hostName;
double ts = cr->GetExecutionEnd(); double ts = cr->GetExecutionEnd();
if (service) { if (service) {
String serviceName = service->GetShortName();
String escaped_serviceName = EscapeMetric(serviceName); if (!config_tmpl_metric.IsEmpty()) {
metric = "icinga.service." + escaped_serviceName; metric = config_tmpl_metric;
} else {
String serviceName = service->GetShortName();
String escaped_serviceName = EscapeMetric(serviceName);
metric = "icinga.service." + escaped_serviceName;
}
SendMetric(checkable, metric + ".state", tags, service->GetState( ), ts); SendMetric(checkable, metric + ".state", tags, service->GetState( ), ts);
} else { } else {
metric = "icinga.host"; if (!config_tmpl_metric.IsEmpty()) {
metric = config_tmpl_metric;
} else {
metric = "icinga.host";
}
SendMetric(checkable, metric + ".state", tags, host->GetState(), ts); SendMetric(checkable, metric + ".state", tags, host->GetState(), ts);
} }
SendMetric(checkable, metric + ".state_type", tags, checkable->GetStateTy pe(), ts); SendMetric(checkable, metric + ".state_type", tags, checkable->GetStateTy pe(), ts);
SendMetric(checkable, metric + ".reachable", tags, checkable->IsReachable (), ts); SendMetric(checkable, metric + ".reachable", tags, checkable->IsReachable (), ts);
SendMetric(checkable, metric + ".downtime_depth", tags, checkable->GetDow ntimeDepth(), ts); SendMetric(checkable, metric + ".downtime_depth", tags, checkable->GetDow ntimeDepth(), ts);
SendMetric(checkable, metric + ".acknowledgement", tags, checkable->GetAc knowledgement(), ts); SendMetric(checkable, metric + ".acknowledgement", tags, checkable->GetAc knowledgement(), ts);
SendPerfdata(checkable, metric, tags, cr, ts); SendPerfdata(checkable, metric, tags, cr, ts);
skipping to change at line 241 skipping to change at line 327
pdv = PerfdataValue::Parse(val); pdv = PerfdataValue::Parse(val);
} catch (const std::exception&) { } catch (const std::exception&) {
Log(LogWarning, "OpenTsdbWriter") Log(LogWarning, "OpenTsdbWriter")
<< "Ignoring invalid perfdata for checkab le '" << "Ignoring invalid perfdata for checkab le '"
<< checkable->GetName() << "' and command '" << checkable->GetName() << "' and command '"
<< checkCommand->GetName() << "' with val ue: " << val; << checkCommand->GetName() << "' with val ue: " << val;
continue; continue;
} }
} }
String escaped_key = EscapeMetric(pdv->GetLabel()); String metric_name;
boost::algorithm::replace_all(escaped_key, "::", "."); std::map<String, String> tags_new = tags;
// Do not break original functionality where perfdata labels form
// part of the metric name
if (!GetEnableGenericMetrics()) {
String escaped_key = EscapeMetric(pdv->GetLabel());
boost::algorithm::replace_all(escaped_key, "::", ".");
metric_name = metric + "." + escaped_key;
} else {
String escaped_key = EscapeTag(pdv->GetLabel());
metric_name = metric;
tags_new["label"] = escaped_key;
}
SendMetric(checkable, metric + "." + escaped_key, tags, pdv->GetV alue(), ts); SendMetric(checkable, metric_name, tags_new, pdv->GetValue(), ts) ;
if (pdv->GetCrit()) if (!pdv->GetCrit().IsEmpty())
SendMetric(checkable, metric + "." + escaped_key + "_crit SendMetric(checkable, metric_name + "_crit", tags_new, pd
", tags, pdv->GetCrit(), ts); v->GetCrit(), ts);
if (pdv->GetWarn()) if (!pdv->GetWarn().IsEmpty())
SendMetric(checkable, metric + "." + escaped_key + "_warn SendMetric(checkable, metric_name + "_warn", tags_new, pd
", tags, pdv->GetWarn(), ts); v->GetWarn(), ts);
if (pdv->GetMin()) if (!pdv->GetMin().IsEmpty())
SendMetric(checkable, metric + "." + escaped_key + "_min" SendMetric(checkable, metric_name + "_min", tags_new, pdv
, tags, pdv->GetMin(), ts); ->GetMin(), ts);
if (pdv->GetMax()) if (!pdv->GetMax().IsEmpty())
SendMetric(checkable, metric + "." + escaped_key + "_max" SendMetric(checkable, metric_name + "_max", tags_new, pdv
, tags, pdv->GetMax(), ts); ->GetMax(), ts);
} }
} }
/** /**
* Send given metric to OpenTSDB * Send given metric to OpenTSDB
* *
* @param checkable Host/service object * @param checkable Host/service object
* @param metric Full metric name * @param metric Full metric name
* @param tags Tag key pairs * @param tags Tag key pairs
* @param value Floating point metric value * @param value Floating point metric value
skipping to change at line 320 skipping to change at line 418
* *
* @param str Tag name * @param str Tag name
* @return Escaped tag * @return Escaped tag
*/ */
String OpenTsdbWriter::EscapeTag(const String& str) String OpenTsdbWriter::EscapeTag(const String& str)
{ {
String result = str; String result = str;
boost::replace_all(result, " ", "_"); boost::replace_all(result, " ", "_");
boost::replace_all(result, "\\", "_"); boost::replace_all(result, "\\", "_");
boost::replace_all(result, ":", "_");
return result; return result;
} }
/** /**
* Escape metric name for OpenTSDB * Escape metric name for OpenTSDB
* http://opentsdb.net/docs/build/html/user_guide/query/timeseries.html#precisio ns-on-metrics-and-tags * http://opentsdb.net/docs/build/html/user_guide/query/timeseries.html#precisio ns-on-metrics-and-tags
* *
* @param str Metric name * @param str Metric name
* @return Escaped metric * @return Escaped metric
skipping to change at line 342 skipping to change at line 441
{ {
String result = str; String result = str;
boost::replace_all(result, " ", "_"); boost::replace_all(result, " ", "_");
boost::replace_all(result, ".", "_"); boost::replace_all(result, ".", "_");
boost::replace_all(result, "\\", "_"); boost::replace_all(result, "\\", "_");
boost::replace_all(result, ":", "_"); boost::replace_all(result, ":", "_");
return result; return result;
} }
/**
* Saves the template dictionaries defined in the config file into running memory
*
* @param stemplate The dictionary to save the service configuration to
* @param htemplate The dictionary to save the host configuration to
*/
void OpenTsdbWriter::ReadConfigTemplate(const Dictionary::Ptr& stemplate,
const Dictionary::Ptr& htemplate)
{
m_ServiceConfigTemplate = GetServiceTemplate();
if (!m_ServiceConfigTemplate) {
Log(LogDebug, "OpenTsdbWriter")
<< "Unable to locate service template configuration.";
} else if (m_ServiceConfigTemplate->GetLength() == 0) {
Log(LogDebug, "OpenTsdbWriter")
<< "The service template configuration is empty.";
}
m_HostConfigTemplate = GetHostTemplate();
if (!m_HostConfigTemplate) {
Log(LogDebug, "OpenTsdbWriter")
<< "Unable to locate host template configuration.";
} else if (m_HostConfigTemplate->GetLength() == 0) {
Log(LogDebug, "OpenTsdbWriter")
<< "The host template configuration is empty.";
}
}
/**
* Validates the host_template configuration block in the configuration
* file and checks for syntax errors.
*
* @param lvalue The host_template dictionary
* @param utils Validation helper utilities
*/
void OpenTsdbWriter::ValidateHostTemplate(const Lazy<Dictionary::Ptr>& lvalue, c
onst ValidationUtils& utils)
{
ObjectImpl<OpenTsdbWriter>::ValidateHostTemplate(lvalue, utils);
String metric = lvalue()->Get("metric");
if (!MacroProcessor::ValidateMacroString(metric))
BOOST_THROW_EXCEPTION(ValidationError(this, { "host_template", "m
etric" }, "Closing $ not found in macro format string '" + metric + "'."));
Dictionary::Ptr tags = lvalue()->Get("tags");
if (tags) {
ObjectLock olock(tags);
for (const Dictionary::Pair& pair : tags) {
if (!MacroProcessor::ValidateMacroString(pair.second))
BOOST_THROW_EXCEPTION(ValidationError(this, { "ho
st_template", "tags", pair.first }, "Closing $ not found in macro format string
'" + pair.second));
}
}
}
/**
* Validates the service_template configuration block in the
* configuration file and checks for syntax errors.
*
* @param lvalue The service_template dictionary
* @param utils Validation helper utilities
*/
void OpenTsdbWriter::ValidateServiceTemplate(const Lazy<Dictionary::Ptr>& lvalue
, const ValidationUtils& utils)
{
ObjectImpl<OpenTsdbWriter>::ValidateServiceTemplate(lvalue, utils);
String metric = lvalue()->Get("metric");
if (!MacroProcessor::ValidateMacroString(metric))
BOOST_THROW_EXCEPTION(ValidationError(this, { "service_template",
"metric" }, "Closing $ not found in macro format string '" + metric + "'."));
Dictionary::Ptr tags = lvalue()->Get("tags");
if (tags) {
ObjectLock olock(tags);
for (const Dictionary::Pair& pair : tags) {
if (!MacroProcessor::ValidateMacroString(pair.second))
BOOST_THROW_EXCEPTION(ValidationError(this, { "se
rvice_template", "tags", pair.first }, "Closing $ not found in macro format stri
ng '" + pair.second));
}
}
}
 End of changes. 19 change blocks. 
25 lines changed or deleted 130 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)