From 028946a1cc6b026260c64bc00c17e5002ff84696 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 24 Oct 2018 14:07:36 +0200 Subject: [PATCH] Implement HA functionality for Perfdata feature --- lib/perfdata/perfdatawriter.cpp | 32 ++++++++++++++++++++++++++------ lib/perfdata/perfdatawriter.hpp | 5 +++-- lib/perfdata/perfdatawriter.ti | 3 +++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/perfdata/perfdatawriter.cpp b/lib/perfdata/perfdatawriter.cpp index a00dc45d7..c560ef256 100644 --- a/lib/perfdata/perfdatawriter.cpp +++ b/lib/perfdata/perfdatawriter.cpp @@ -38,6 +38,20 @@ REGISTER_TYPE(PerfdataWriter); REGISTER_STATSFUNCTION(PerfdataWriter, &PerfdataWriter::StatsFunc); +void PerfdataWriter::OnConfigLoaded() +{ + ObjectImpl::OnConfigLoaded(); + + if (!GetEnableHa()) { + Log(LogDebug, "PerfdataWriter") + << "HA functionality disabled. Won't pause connection: " << GetName(); + + SetHAMode(HARunEverywhere); + } else { + SetHAMode(HARunOnce); + } +} + void PerfdataWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&) { DictionaryData nodes; @@ -49,12 +63,12 @@ void PerfdataWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&) status->Set("perfdatawriter", new Dictionary(std::move(nodes))); } -void PerfdataWriter::Start(bool runtimeCreated) +void PerfdataWriter::Resume() { - ObjectImpl::Start(runtimeCreated); + ObjectImpl::Resume(); Log(LogInformation, "PerfdataWriter") - << "'" << GetName() << "' started."; + << "'" << GetName() << "' resumed."; Checkable::OnNewCheckResult.connect(std::bind(&PerfdataWriter::CheckResultHandler, this, _1, _2)); @@ -67,12 +81,12 @@ void PerfdataWriter::Start(bool runtimeCreated) RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath()); } -void PerfdataWriter::Stop(bool runtimeRemoved) +void PerfdataWriter::Pause() { Log(LogInformation, "PerfdataWriter") - << "'" << GetName() << "' stopped."; + << "'" << GetName() << "' paused."; - ObjectImpl::Stop(runtimeRemoved); + ObjectImpl::Pause(); } Value PerfdataWriter::EscapeMacroMetric(const Value& value) @@ -85,6 +99,9 @@ Value PerfdataWriter::EscapeMacroMetric(const Value& value) void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { + if (IsPaused()) + return; + CONTEXT("Writing performance data for object '" + checkable->GetName() + "'"); if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) @@ -154,6 +171,9 @@ void PerfdataWriter::RotateFile(std::ofstream& output, const String& temp_path, void PerfdataWriter::RotationTimerHandler() { + if (IsPaused()) + return; + RotateFile(m_ServiceOutputFile, GetServiceTempPath(), GetServicePerfdataPath()); RotateFile(m_HostOutputFile, GetHostTempPath(), GetHostPerfdataPath()); } diff --git a/lib/perfdata/perfdatawriter.hpp b/lib/perfdata/perfdatawriter.hpp index 10fe7a2dd..56d01d382 100644 --- a/lib/perfdata/perfdatawriter.hpp +++ b/lib/perfdata/perfdatawriter.hpp @@ -46,8 +46,9 @@ public: void ValidateServiceFormatTemplate(const Lazy& lvalue, const ValidationUtils& utils) override; protected: - void Start(bool runtimeCreated) override; - void Stop(bool runtimeRemoved) override; + void OnConfigLoaded() override; + void Resume() override; + void Pause() override; private: void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); diff --git a/lib/perfdata/perfdatawriter.ti b/lib/perfdata/perfdatawriter.ti index 8381c6c9e..d2ceb6899 100644 --- a/lib/perfdata/perfdatawriter.ti +++ b/lib/perfdata/perfdatawriter.ti @@ -70,6 +70,9 @@ class PerfdataWriter : ConfigObject [config] double rotation_interval { default {{{ return 30; }}} }; + [config] bool enable_ha { + default {{{ return true; }}} + }; }; }