From 37ccffc7e59b8f24e2749ad5d803561b8dbd885e Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 24 Oct 2018 13:29:27 +0200 Subject: [PATCH] Implement HA functionality for Graphite feature --- lib/perfdata/graphitewriter.cpp | 29 +++++++++++++++++++++++------ lib/perfdata/graphitewriter.hpp | 4 ++-- lib/perfdata/graphitewriter.ti | 3 +++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/perfdata/graphitewriter.cpp b/lib/perfdata/graphitewriter.cpp index d58e2c53b..a01f6c48b 100644 --- a/lib/perfdata/graphitewriter.cpp +++ b/lib/perfdata/graphitewriter.cpp @@ -49,6 +49,15 @@ void GraphiteWriter::OnConfigLoaded() ObjectImpl::OnConfigLoaded(); m_WorkQueue.SetName("GraphiteWriter, " + GetName()); + + if (!GetEnableHa()) { + Log(LogDebug, "GraphiteWriter") + << "HA functionality disabled. Won't pause connection: " << GetName(); + + SetHAMode(HARunEverywhere); + } else { + SetHAMode(HARunOnce); + } } void GraphiteWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata) @@ -72,12 +81,12 @@ void GraphiteWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& status->Set("graphitewriter", new Dictionary(std::move(nodes))); } -void GraphiteWriter::Start(bool runtimeCreated) +void GraphiteWriter::Resume() { - ObjectImpl::Start(runtimeCreated); + ObjectImpl::Resume(); Log(LogInformation, "GraphiteWriter") - << "'" << GetName() << "' started."; + << "'" << GetName() << "' resumed."; /* Register exception handler for WQ tasks. */ m_WorkQueue.SetExceptionCallback(std::bind(&GraphiteWriter::ExceptionHandler, this, _1)); @@ -93,14 +102,14 @@ void GraphiteWriter::Start(bool runtimeCreated) Checkable::OnNewCheckResult.connect(std::bind(&GraphiteWriter::CheckResultHandler, this, _1, _2)); } -void GraphiteWriter::Stop(bool runtimeRemoved) +void GraphiteWriter::Pause() { Log(LogInformation, "GraphiteWriter") - << "'" << GetName() << "' stopped."; + << "'" << GetName() << "' paused."; m_WorkQueue.Join(); - ObjectImpl::Stop(runtimeRemoved); + ObjectImpl::Pause(); } void GraphiteWriter::AssertOnWorkQueue() @@ -126,6 +135,11 @@ void GraphiteWriter::Reconnect() { AssertOnWorkQueue(); + if (IsPaused()) { + SetConnected(false); + return; + } + double startTime = Utility::GetTime(); CONTEXT("Reconnecting to Graphite '" + GetName() + "'"); @@ -175,6 +189,9 @@ void GraphiteWriter::Disconnect() void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { + if (IsPaused()) + return; + m_WorkQueue.Enqueue(std::bind(&GraphiteWriter::CheckResultHandlerInternal, this, checkable, cr)); } diff --git a/lib/perfdata/graphitewriter.hpp b/lib/perfdata/graphitewriter.hpp index 48a2f4203..6934d13b3 100644 --- a/lib/perfdata/graphitewriter.hpp +++ b/lib/perfdata/graphitewriter.hpp @@ -49,8 +49,8 @@ public: protected: void OnConfigLoaded() override; - void Start(bool runtimeCreated) override; - void Stop(bool runtimeRemoved) override; + void Resume() override; + void Pause() override; private: Stream::Ptr m_Stream; diff --git a/lib/perfdata/graphitewriter.ti b/lib/perfdata/graphitewriter.ti index dfe62a14b..b28ba87a9 100644 --- a/lib/perfdata/graphitewriter.ti +++ b/lib/perfdata/graphitewriter.ti @@ -47,6 +47,9 @@ class GraphiteWriter : ConfigObject [no_user_modify] bool should_connect { default {{{ return true; }}} }; + [config] bool enable_ha { + default {{{ return true; }}} + }; }; }