From a344f11e6cb0d7416132f7787cd48af7c6d513fb Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 30 Nov 2017 10:46:44 +0100 Subject: [PATCH] Replace StatsFunction with Function --- lib/base/CMakeLists.txt | 2 +- lib/base/statsfunction.cpp | 39 ---------------------------------- lib/base/statsfunction.hpp | 41 ++---------------------------------- lib/icinga/cib.cpp | 10 ++++++--- lib/remote/statushandler.cpp | 19 ++++++++++++----- 5 files changed, 24 insertions(+), 87 deletions(-) delete mode 100644 lib/base/statsfunction.cpp diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index 97b9209ea..a5580046f 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -37,7 +37,7 @@ set(base_SOURCES function.cpp function.thpp function-script.cpp perfdatavalue.cpp perfdatavalue.thpp scriptglobal.cpp scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp socketevents-epoll.cpp socketevents-poll.cpp stacktrace.cpp - statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp + stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp threadpool.cpp timer.cpp tlsstream.cpp tlsutility.cpp type.cpp typetype-script.cpp unixsocket.cpp utility.cpp value.cpp value-operators.cpp workqueue.cpp diff --git a/lib/base/statsfunction.cpp b/lib/base/statsfunction.cpp deleted file mode 100644 index fcfdce828..000000000 --- a/lib/base/statsfunction.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#include "base/statsfunction.hpp" -#include "base/registry.hpp" -#include "base/singleton.hpp" - -using namespace icinga; - -StatsFunction::StatsFunction(const Callback& function) - : m_Callback(function) -{ } - -void StatsFunction::Invoke(const Dictionary::Ptr& status, const Array::Ptr& perfdata) -{ - m_Callback(status, perfdata); -} - -StatsFunctionRegistry *StatsFunctionRegistry::GetInstance(void) -{ - return Singleton::GetInstance(); -} - diff --git a/lib/base/statsfunction.hpp b/lib/base/statsfunction.hpp index c8880d419..b56950520 100644 --- a/lib/base/statsfunction.hpp +++ b/lib/base/statsfunction.hpp @@ -21,50 +21,13 @@ #define STATSFUNCTION_H #include "base/i2-base.hpp" -#include "base/registry.hpp" -#include "base/value.hpp" -#include "base/dictionary.hpp" -#include "base/array.hpp" +#include "base/function.hpp" namespace icinga { -/** - * A stats function that can be used to execute a stats task. - * - * @ingroup base - */ -class I2_BASE_API StatsFunction : public Object -{ -public: - DECLARE_PTR_TYPEDEFS(StatsFunction); - - typedef std::function Callback; - - StatsFunction(const Callback& function); - - void Invoke(const Dictionary::Ptr& status, const Array::Ptr& perfdata); - -private: - Callback m_Callback; -}; - -/** - * A registry for script functions. - * - * @ingroup base - */ -class I2_BASE_API StatsFunctionRegistry : public Registry -{ -public: - static StatsFunctionRegistry *GetInstance(void); -}; - #define REGISTER_STATSFUNCTION(name, callback) \ - INITIALIZE_ONCE([]() { \ - StatsFunction::Ptr stf = new StatsFunction(callback); \ - StatsFunctionRegistry::GetInstance()->Register(#name, stf); \ - }) + REGISTER_SCRIPTFUNCTION_NS(StatsFunctions, name, callback, "status:perfdata") } diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index 293701937..f71738d5a 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -267,9 +267,13 @@ std::pair CIB::GetFeatureStats(void) Dictionary::Ptr status = new Dictionary(); Array::Ptr perfdata = new Array(); - String name; - for (const auto& kv : StatsFunctionRegistry::GetInstance()->GetItems()) { - kv.second->Invoke(status, perfdata); + Dictionary::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty); + + if (statsFunctions) { + ObjectLock olock(statsFunctions); + + for (const Dictionary::Pair& kv : statsFunctions) + static_cast(kv.second)->Invoke({ status, perfdata }); } return std::make_pair(status, perfdata); diff --git a/lib/remote/statushandler.cpp b/lib/remote/statushandler.cpp index 518a9513f..47627d051 100644 --- a/lib/remote/statushandler.cpp +++ b/lib/remote/statushandler.cpp @@ -35,15 +35,24 @@ public: virtual void FindTargets(const String& type, const std::function& addTarget) const override { - typedef std::pair kv_pair; - for (const kv_pair& kv : StatsFunctionRegistry::GetInstance()->GetItems()) { - addTarget(GetTargetByName("Status", kv.first)); + Dictionary::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty); + + if (statsFunctions) { + ObjectLock olock(statsFunctions); + + for (const Dictionary::Pair& kv : statsFunctions) + addTarget(GetTargetByName("Status", kv.first)); } } virtual Value GetTargetByName(const String& type, const String& name) const override { - StatsFunction::Ptr func = StatsFunctionRegistry::GetInstance()->GetItem(name); + Dictionary::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty); + + if (!statsFunctions) + BOOST_THROW_EXCEPTION(std::invalid_argument("No status functions are available.")); + + Function::Ptr func = statsFunctions->Get(name); if (!func) BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid status function name.")); @@ -52,7 +61,7 @@ public: Dictionary::Ptr status = new Dictionary(); Array::Ptr perfdata = new Array(); - func->Invoke(status, perfdata); + func->Invoke({ status, perfdata }); result->Set("name", name); result->Set("status", status); -- 2.40.0