Merge pull request #5827 from Icinga/feature/replace-statsfunction-with-function

Replace StatsFunction with Function
This commit is contained in:
Michael Friedrich 2017-11-30 21:28:32 +01:00 committed by GitHub
commit da056b052b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 87 deletions

View 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

View File

@ -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();
}

View File

@ -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<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")
}

View 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);

View 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);