]> granicus.if.org Git - icinga2/commitdiff
Replace StatsFunction with Function 5827/head
authorGunnar Beutner <gunnar.beutner@icinga.com>
Thu, 30 Nov 2017 09:46:44 +0000 (10:46 +0100)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Thu, 30 Nov 2017 18:02:25 +0000 (19:02 +0100)
lib/base/CMakeLists.txt
lib/base/statsfunction.cpp [deleted file]
lib/base/statsfunction.hpp
lib/icinga/cib.cpp
lib/remote/statushandler.cpp

index 97b9209ea5a65e3e198bb3213d368826fc7f8ff0..a5580046fbaa0555478d4c446724c7727a3125ed 100644 (file)
@@ -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 (file)
index fcfdce8..0000000
+++ /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<StatsFunctionRegistry>::GetInstance();
-}
-
index c8880d4197124f8bc72d522787d1905e83f48cf1..b56950520b3ff7047b78c3d49607005e4c847921 100644 (file)
 #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<void (const Dictionary::Ptr& status, const Array::Ptr& perfdata)> 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<StatsFunctionRegistry, StatsFunction::Ptr>
-{
-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")
 
 }
 
index 293701937318c461c14f8fcd850b5d4de6cafb0e..f71738d5af46376d3e5c640413ed7c0126043ea7 100644 (file)
@@ -267,9 +267,13 @@ std::pair<Dictionary::Ptr, Array::Ptr> 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<Function::Ptr>(kv.second)->Invoke({ status, perfdata });
        }
 
        return std::make_pair(status, perfdata);
index 518a9513f3283694b759d75c10ad216723352d78..47627d051cd59877b1e154c1d9f04a82e0dac3de 100644 (file)
@@ -35,15 +35,24 @@ public:
        virtual void FindTargets(const String& type,
            const std::function<void (const Value&)>& addTarget) const override
        {
-               typedef std::pair<String, StatsFunction::Ptr> 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);