diff --git a/lib/perfdata/opentsdbwriter.cpp b/lib/perfdata/opentsdbwriter.cpp index 1f0f1e5be..5b90ed52c 100644 --- a/lib/perfdata/opentsdbwriter.cpp +++ b/lib/perfdata/opentsdbwriter.cpp @@ -73,6 +73,8 @@ void OpenTsdbWriter::Resume() Log(LogInformation, "OpentsdbWriter") << "'" << GetName() << "' resumed."; + ReadConfigTemplate(m_ServiceConfigTemplate, m_HostConfigTemplate); + m_ReconnectTimer = new Timer(); m_ReconnectTimer->SetInterval(10); m_ReconnectTimer->OnTimerExpired.connect(std::bind(&OpenTsdbWriter::ReconnectTimerHandler, this)); @@ -151,33 +153,35 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C Service::Ptr service = dynamic_pointer_cast(checkable); Host::Ptr host; - Dictionary::Ptr config_tmpl_clean; + Dictionary::Ptr config_tmpl; + Dictionary::Ptr config_tmpl_tags; if (service) { host = service->GetHost(); - config_tmpl_clean = GetServiceTemplate(); + config_tmpl = m_ServiceConfigTemplate; } else { host = static_pointer_cast(checkable); - config_tmpl_clean = GetHostTemplate(); + config_tmpl = m_HostConfigTemplate; } - // Clone the config template and perform an in-place macro expansion of measurement and tag values - Dictionary::Ptr config_tmpl = static_pointer_cast(config_tmpl_clean->Clone()); - Dictionary::Ptr config_tmpl_tags = config_tmpl->Get("tags"); - - // 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()); + // Get the tags nested dictionary in the service/host template in the config + if (config_tmpl) { + config_tmpl_tags = config_tmpl->Get("tags"); + } String metric; std::map tags; // Resolve macros in configuration template and build custom tag list if (config_tmpl_tags) { + + // 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()); ObjectLock olock(config_tmpl_tags); @@ -379,6 +383,39 @@ String OpenTsdbWriter::EscapeMetric(const String& str) 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. diff --git a/lib/perfdata/opentsdbwriter.hpp b/lib/perfdata/opentsdbwriter.hpp index 4dd98811e..5531bdae2 100644 --- a/lib/perfdata/opentsdbwriter.hpp +++ b/lib/perfdata/opentsdbwriter.hpp @@ -39,6 +39,9 @@ private: Timer::Ptr m_ReconnectTimer; + Dictionary::Ptr m_ServiceConfigTemplate; + Dictionary::Ptr m_HostConfigTemplate; + void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void SendMetric(const Checkable::Ptr& checkable, const String& metric, const std::map& tags, double value, double ts); @@ -48,6 +51,9 @@ private: static String EscapeMetric(const String& str); void ReconnectTimerHandler(); + + void ReadConfigTemplate(const Dictionary::Ptr& stemplate, + const Dictionary::Ptr& htemplate); }; }