diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index be2bc27aa..3245027fa 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -25,11 +25,20 @@ #include "base/utility.h" #include "base/logger_fwd.h" #include "base/exception.h" +#include "base/statsfunction.h" #include using namespace icinga; REGISTER_TYPE(CheckerComponent); +REGISTER_STATSFUNCTION(CheckerComponentStats, &CheckerComponent::StatsFunc); + +Value CheckerComponent::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + status->Set("checkercomponent_", 1); + + return 0; +} void CheckerComponent::OnConfigLoaded(void) { diff --git a/components/checker/checkercomponent.h b/components/checker/checkercomponent.h index 516feec21..63cc70a1a 100644 --- a/components/checker/checkercomponent.h +++ b/components/checker/checkercomponent.h @@ -71,6 +71,8 @@ public: virtual void Start(void); virtual void Stop(void); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + private: boost::mutex m_Mutex; boost::condition_variable m_CV; diff --git a/components/cluster/clusterlistener.cpp b/components/cluster/clusterlistener.cpp index a27b0db8e..d043d7891 100644 --- a/components/cluster/clusterlistener.cpp +++ b/components/cluster/clusterlistener.cpp @@ -31,12 +31,22 @@ #include "base/application.h" #include "base/convert.h" #include "base/context.h" +#include "base/statsfunction.h" #include using namespace icinga; REGISTER_TYPE(ClusterListener); +REGISTER_STATSFUNCTION(ClusterListenerStats, &ClusterListener::StatsFunc); + +Value ClusterListener::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + status->Set("clusterlistener_", 1); + + return 0; +} + /** * Starts the component. */ @@ -1637,22 +1647,11 @@ Dictionary::Ptr ClusterListener::GetClusterStatus(void) bag->Set("not_conn_endpoints", not_connected_endpoints); /* features */ - bag->Set("feature_CheckerComponent", SupportsChecks() ? 1 : 0); - bag->Set("feature_NotificationComponent", SupportsNotifications() ? 1 : 0); - - /* XXX find a more generic way of getting features as a list */ - bag->Set("feature_IdoMysqlConnection", SupportsFeature("IdoMysqlConnection") ? 1 : 0); - bag->Set("feature_IdoPgsqlConnection", SupportsFeature("IdoPgsqlConnection") ? 1 : 0); - bag->Set("feature_StatusDataWriter", SupportsFeature("StatusDataWriter") ? 1 : 0); - bag->Set("feature_CompatLogger", SupportsFeature("CompatLogger") ? 1 : 0); - bag->Set("feature_ExternalCommandListener", SupportsFeature("ExternalCommandListener") ? 1 : 0); - bag->Set("feature_CheckResultReader", SupportsFeature("CheckResultReader") ? 1 : 0); - bag->Set("feature_LivestatusListener", SupportsFeature("LivestatusListener") ? 1 : 0); - bag->Set("feature_GraphiteWriter", SupportsFeature("GraphiteWriter") ? 1 : 0); - bag->Set("feature_PerfdataWriter", SupportsFeature("PerfdataWriter") ? 1 : 0); - bag->Set("feature_FileLogger", SupportsFeature("FileLogger") ? 1 : 0); - bag->Set("feature_SyslogLogger", SupportsFeature("SyslogLogger") ? 1 : 0); + std::pair stats = CIB::GetFeatureStats(); + /* XXX find a more clean way */ + bag->Set("feature_status", stats.first); + bag->Set("feature_perfdata", stats.second); /* icinga stats */ double interval = Utility::GetTime() - Application::GetStartTime(); diff --git a/components/cluster/clusterlistener.h b/components/cluster/clusterlistener.h index cbcdd36b6..65c7de276 100644 --- a/components/cluster/clusterlistener.h +++ b/components/cluster/clusterlistener.h @@ -45,6 +45,8 @@ public: DECLARE_PTR_TYPEDEFS(ClusterListener); DECLARE_TYPENAME(ClusterListener); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + virtual void Start(void); virtual void Stop(void); diff --git a/components/compat/checkresultreader.cpp b/components/compat/checkresultreader.cpp index f86d58e91..664561b8d 100644 --- a/components/compat/checkresultreader.cpp +++ b/components/compat/checkresultreader.cpp @@ -29,12 +29,23 @@ #include "base/utility.h" #include "base/exception.h" #include "base/context.h" +#include "base/statsfunction.h" #include using namespace icinga; REGISTER_TYPE(CheckResultReader); +REGISTER_STATSFUNCTION(CheckResultReaderStats, &CheckResultReader::StatsFunc); + +Value CheckResultReader::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("checkresultreader_", 1); + + return 0; +} + /** * @threadsafety Always. */ diff --git a/components/compat/checkresultreader.h b/components/compat/checkresultreader.h index 6c1012ff8..13586a6a5 100644 --- a/components/compat/checkresultreader.h +++ b/components/compat/checkresultreader.h @@ -38,6 +38,8 @@ public: DECLARE_PTR_TYPEDEFS(CheckResultReader); DECLARE_TYPENAME(CheckResultReader); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); diff --git a/components/compat/compatlogger.cpp b/components/compat/compatlogger.cpp index 2df327c07..252c7566a 100644 --- a/components/compat/compatlogger.cpp +++ b/components/compat/compatlogger.cpp @@ -34,6 +34,7 @@ #include "base/application.h" #include "base/utility.h" #include "base/scriptfunction.h" +#include "base/statsfunction.h" #include #include @@ -42,6 +43,16 @@ using namespace icinga; REGISTER_TYPE(CompatLogger); REGISTER_SCRIPTFUNCTION(ValidateRotationMethod, &CompatLogger::ValidateRotationMethod); +REGISTER_STATSFUNCTION(CompatLoggerStats, &CompatLogger::StatsFunc); + +Value CompatLogger::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("compatlogger_", 1); + + return 0; +} + /** * @threadsafety Always. */ diff --git a/components/compat/compatlogger.h b/components/compat/compatlogger.h index d48b5f28a..0208cc1c4 100644 --- a/components/compat/compatlogger.h +++ b/components/compat/compatlogger.h @@ -39,6 +39,8 @@ public: DECLARE_PTR_TYPEDEFS(CompatLogger); DECLARE_TYPENAME(CompatLogger); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + static void ValidateRotationMethod(const String& location, const Dictionary::Ptr& attrs); protected: diff --git a/components/compat/externalcommandlistener.cpp b/components/compat/externalcommandlistener.cpp index 58d9b36b8..42efa38b9 100644 --- a/components/compat/externalcommandlistener.cpp +++ b/components/compat/externalcommandlistener.cpp @@ -23,11 +23,22 @@ #include "base/logger_fwd.h" #include "base/exception.h" #include "base/application.h" +#include "base/statsfunction.h" using namespace icinga; REGISTER_TYPE(ExternalCommandListener); +REGISTER_STATSFUNCTION(ExternalCommandListenerStats, &ExternalCommandListener::StatsFunc); + +Value ExternalCommandListener::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("externalcommandlisterner_", 1); + + return 0; +} + /** * Starts the component. */ diff --git a/components/compat/externalcommandlistener.h b/components/compat/externalcommandlistener.h index db6eb8171..d8dce048a 100644 --- a/components/compat/externalcommandlistener.h +++ b/components/compat/externalcommandlistener.h @@ -38,6 +38,8 @@ class ExternalCommandListener : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(ExternalCommandListener); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index 315d4b9ac..c90571e01 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -34,6 +34,7 @@ #include "base/exception.h" #include "base/application.h" #include "base/context.h" +#include "base/statsfunction.h" #include #include #include @@ -43,6 +44,16 @@ using namespace icinga; REGISTER_TYPE(StatusDataWriter); +REGISTER_STATSFUNCTION(StatusDataWriterStats, &StatusDataWriter::StatsFunc); + +Value StatusDataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("statusdatawriter_", 1); + + return 0; +} + /** * Hint: The reason why we're using "\n" rather than std::endl is because * std::endl also _flushes_ the output stream which severely degrades diff --git a/components/compat/statusdatawriter.h b/components/compat/statusdatawriter.h index b65538d8b..e1cb6c995 100644 --- a/components/compat/statusdatawriter.h +++ b/components/compat/statusdatawriter.h @@ -42,6 +42,8 @@ class StatusDataWriter : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(StatusDataWriter); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); diff --git a/components/db_ido_mysql/idomysqlconnection.cpp b/components/db_ido_mysql/idomysqlconnection.cpp index 15dcdaefc..701c9fd1b 100644 --- a/components/db_ido_mysql/idomysqlconnection.cpp +++ b/components/db_ido_mysql/idomysqlconnection.cpp @@ -24,6 +24,7 @@ #include "base/application.h" #include "base/dynamictype.h" #include "base/exception.h" +#include "base/statsfunction.h" #include "db_ido/dbtype.h" #include "db_ido/dbvalue.h" #include "db_ido_mysql/idomysqlconnection.h" @@ -32,10 +33,19 @@ using namespace icinga; -REGISTER_TYPE(IdoMysqlConnection); - #define SCHEMA_VERSION "1.11.0" +REGISTER_TYPE(IdoMysqlConnection); +REGISTER_STATSFUNCTION(IdoMysqlConnectionStats, &IdoMysqlConnection::StatsFunc); + +Value IdoMysqlConnection::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("ido_mysql_version_req", SCHEMA_VERSION); + + return 0; +} + void IdoMysqlConnection::Start(void) { DbConnection::Start(); diff --git a/components/db_ido_mysql/idomysqlconnection.h b/components/db_ido_mysql/idomysqlconnection.h index 17d6751e6..26f8425bd 100644 --- a/components/db_ido_mysql/idomysqlconnection.h +++ b/components/db_ido_mysql/idomysqlconnection.h @@ -41,6 +41,8 @@ class IdoMysqlConnection : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(IdoMysqlConnection); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); virtual void Stop(void); diff --git a/components/db_ido_pgsql/idopgsqlconnection.cpp b/components/db_ido_pgsql/idopgsqlconnection.cpp index a62941499..128bd278c 100644 --- a/components/db_ido_pgsql/idopgsqlconnection.cpp +++ b/components/db_ido_pgsql/idopgsqlconnection.cpp @@ -25,6 +25,7 @@ #include "base/dynamictype.h" #include "base/exception.h" #include "base/context.h" +#include "base/statsfunction.h" #include "db_ido/dbtype.h" #include "db_ido/dbvalue.h" #include "db_ido_pgsql/idopgsqlconnection.h" @@ -33,9 +34,19 @@ using namespace icinga; +#define SCHEMA_VERSION "1.11.0" + REGISTER_TYPE(IdoPgsqlConnection); -#define SCHEMA_VERSION "1.11.0" +REGISTER_STATSFUNCTION(IdoPgsqlConnectionStats, &IdoPgsqlConnection::StatsFunc); + +Value IdoPgsqlConnection::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("ido_pgsql_version_req", SCHEMA_VERSION); + + return 0; +} void IdoPgsqlConnection::Start(void) { diff --git a/components/db_ido_pgsql/idopgsqlconnection.h b/components/db_ido_pgsql/idopgsqlconnection.h index 20e4b07d3..3de6c5560 100644 --- a/components/db_ido_pgsql/idopgsqlconnection.h +++ b/components/db_ido_pgsql/idopgsqlconnection.h @@ -41,6 +41,8 @@ class IdoPgsqlConnection : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(IdoPgsqlConnection); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); virtual void Stop(void); diff --git a/components/livestatus/listener.cpp b/components/livestatus/listener.cpp index baa226c28..5b233277f 100644 --- a/components/livestatus/listener.cpp +++ b/components/livestatus/listener.cpp @@ -29,6 +29,7 @@ #include "base/networkstream.h" #include "base/application.h" #include "base/scriptfunction.h" +#include "base/statsfunction.h" #include "base/convert.h" using namespace icinga; @@ -40,6 +41,16 @@ static int l_ClientsConnected = 0; static int l_Connections = 0; static boost::mutex l_ComponentMutex; +REGISTER_STATSFUNCTION(LivestatusListenerStats, &LivestatusListener::StatsFunc); + +Value LivestatusListener::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("livestatus_connections", l_Connections); + + return 0; +} + /** * Starts the component. */ diff --git a/components/livestatus/listener.h b/components/livestatus/listener.h index 7f992e267..8f106a903 100644 --- a/components/livestatus/listener.h +++ b/components/livestatus/listener.h @@ -38,6 +38,8 @@ class LivestatusListener : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(LivestatusListener); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + static int GetClientsConnected(void); static int GetConnections(void); diff --git a/components/notification/notificationcomponent.cpp b/components/notification/notificationcomponent.cpp index 97427e804..2843e3051 100644 --- a/components/notification/notificationcomponent.cpp +++ b/components/notification/notificationcomponent.cpp @@ -24,12 +24,23 @@ #include "base/logger_fwd.h" #include "base/utility.h" #include "base/exception.h" +#include "base/statsfunction.h" #include using namespace icinga; REGISTER_TYPE(NotificationComponent); +REGISTER_STATSFUNCTION(NotificationComponentStats, &NotificationComponent::StatsFunc); + +Value NotificationComponent::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("notification_", 1); + + return 0; +} + /** * Starts the component. */ diff --git a/components/notification/notificationcomponent.h b/components/notification/notificationcomponent.h index ac1a9a824..ddb0f6ebf 100644 --- a/components/notification/notificationcomponent.h +++ b/components/notification/notificationcomponent.h @@ -36,6 +36,8 @@ class NotificationComponent : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(NotificationComponent); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + virtual void Start(void); private: diff --git a/components/perfdata/graphitewriter.cpp b/components/perfdata/graphitewriter.cpp index 583e1266f..ce8c61368 100644 --- a/components/perfdata/graphitewriter.cpp +++ b/components/perfdata/graphitewriter.cpp @@ -34,6 +34,7 @@ #include "base/networkstream.h" #include "base/bufferedstream.h" #include "base/exception.h" +#include "base/statsfunction.h" #include #include #include @@ -44,6 +45,16 @@ using namespace icinga; REGISTER_TYPE(GraphiteWriter); +REGISTER_STATSFUNCTION(GraphiteWriterStats, &GraphiteWriter::StatsFunc); + +Value GraphiteWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("graphite_writer_", 1); + + return 0; +} + void GraphiteWriter::Start(void) { DynamicObject::Start(); diff --git a/components/perfdata/graphitewriter.h b/components/perfdata/graphitewriter.h index 2a5ac0ed5..9a833ddcf 100644 --- a/components/perfdata/graphitewriter.h +++ b/components/perfdata/graphitewriter.h @@ -41,6 +41,8 @@ public: DECLARE_PTR_TYPEDEFS(GraphiteWriter); DECLARE_TYPENAME(GraphiteWriter); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); diff --git a/components/perfdata/perfdatawriter.cpp b/components/perfdata/perfdatawriter.cpp index bb3b6777e..32f2623ef 100644 --- a/components/perfdata/perfdatawriter.cpp +++ b/components/perfdata/perfdatawriter.cpp @@ -28,11 +28,22 @@ #include "base/utility.h" #include "base/context.h" #include "base/application.h" +#include "base/statsfunction.h" using namespace icinga; REGISTER_TYPE(PerfdataWriter); +REGISTER_STATSFUNCTION(PerfdataWriterStats, &PerfdataWriter::StatsFunc); + +Value PerfdataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("perfdatawriter_", 1); + + return 0; +} + void PerfdataWriter::Start(void) { DynamicObject::Start(); diff --git a/components/perfdata/perfdatawriter.h b/components/perfdata/perfdatawriter.h index 9dc114fd0..73627815d 100644 --- a/components/perfdata/perfdatawriter.h +++ b/components/perfdata/perfdatawriter.h @@ -40,6 +40,8 @@ public: DECLARE_PTR_TYPEDEFS(PerfdataWriter); DECLARE_TYPENAME(PerfdataWriter); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void Start(void); diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index eddda95a4..ae8b67391 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -31,7 +31,7 @@ add_library(base SHARED process-unix.cpp process-windows.cpp qstring.cpp ringbuffer.cpp script.cpp script.th scriptfunction.cpp scriptfunctionwrapper.cpp scriptinterpreter.cpp scriptlanguage.cpp scriptvariable.cpp serializer.cpp socket.cpp stacktrace.cpp - stdiostream.cpp stream_bio.cpp stream.cpp streamlogger.cpp streamlogger.th + statsfunction.cpp stdiostream.cpp stream_bio.cpp stream.cpp streamlogger.cpp streamlogger.th sysloglogger.cpp sysloglogger.th tcpsocket.cpp threadpool.cpp timer.cpp tlsstream.cpp tlsutility.cpp type.cpp unixsocket.cpp utility.cpp value.cpp workqueue.cpp zlibstream.cpp diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index ff78919d3..8f9cd3800 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -59,7 +59,7 @@ struct DictionaryKeyLessComparer }; /** - * Restrieves a value from a dictionary. + * Retrieves a value from a dictionary. * * @param key The key whose value should be retrieved. * @returns The value of an empty value if the key was not found. diff --git a/lib/base/filelogger.cpp b/lib/base/filelogger.cpp index af88ae74a..3f90c806c 100644 --- a/lib/base/filelogger.cpp +++ b/lib/base/filelogger.cpp @@ -19,12 +19,23 @@ #include "base/filelogger.h" #include "base/dynamictype.h" +#include "base/statsfunction.h" #include using namespace icinga; REGISTER_TYPE(FileLogger); +REGISTER_STATSFUNCTION(FileLoggerStats, &FileLogger::StatsFunc); + +Value FileLogger::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("filelogger_", 1); + + return 0; +} + /** * Constructor for the FileLogger class. */ diff --git a/lib/base/filelogger.h b/lib/base/filelogger.h index a06586c3a..2c6b6572f 100644 --- a/lib/base/filelogger.h +++ b/lib/base/filelogger.h @@ -36,6 +36,8 @@ class I2_BASE_API FileLogger : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(FileLogger); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + virtual void Start(void); }; diff --git a/lib/base/statsfunction.cpp b/lib/base/statsfunction.cpp new file mode 100644 index 000000000..edc5b8ba6 --- /dev/null +++ b/lib/base/statsfunction.cpp @@ -0,0 +1,45 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * * + * 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.h" +#include "base/registry.h" +#include "base/singleton.h" + +using namespace icinga; + +StatsFunction::StatsFunction(const Callback& function) + : m_Callback(function) +{ } + +Value StatsFunction::Invoke(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + return m_Callback(status, perfdata); +} + +RegisterStatsFunctionHelper::RegisterStatsFunctionHelper(const String& name, const StatsFunction::Callback& function) +{ + StatsFunction::Ptr func = make_shared(function); + StatsFunctionRegistry::GetInstance()->Register(name, func); +} + +StatsFunctionRegistry *StatsFunctionRegistry::GetInstance(void) +{ + return Singleton::GetInstance(); +} + diff --git a/lib/base/statsfunction.h b/lib/base/statsfunction.h new file mode 100644 index 000000000..4acbd322d --- /dev/null +++ b/lib/base/statsfunction.h @@ -0,0 +1,81 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * * + * 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. * + ******************************************************************************/ + +#ifndef STATSFUNCTION_H +#define STATSFUNCTION_H + +#include "base/i2-base.h" +#include "base/registry.h" +#include "base/singleton.h" +#include "base/value.h" +#include "base/dictionary.h" +#include +#include + +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 boost::function Callback; + + StatsFunction(const Callback& function); + + Value Invoke(Dictionary::Ptr& status, Dictionary::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); +}; + +/** + * Helper class for registering StatsFunction implementation classes. + * + * @ingroup base + */ +class I2_BASE_API RegisterStatsFunctionHelper +{ +public: + RegisterStatsFunctionHelper(const String& name, const StatsFunction::Callback& function); +}; + +#define REGISTER_STATSFUNCTION(name, callback) \ + I2_EXPORT icinga::RegisterStatsFunctionHelper g_RegisterSF_ ## name(#name, callback) + +} + +#endif /* STATSFUNCTION_H */ diff --git a/lib/base/sysloglogger.cpp b/lib/base/sysloglogger.cpp index 7f61682a9..64e203862 100644 --- a/lib/base/sysloglogger.cpp +++ b/lib/base/sysloglogger.cpp @@ -19,12 +19,23 @@ #include "base/sysloglogger.h" #include "base/dynamictype.h" +#include "base/statsfunction.h" #ifndef _WIN32 using namespace icinga; REGISTER_TYPE(SyslogLogger); +REGISTER_STATSFUNCTION(SyslogLoggerStats, &SyslogLogger::StatsFunc); + +Value SyslogLogger::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("sysloglogger_", 1); + + return 0; +} + /** * Processes a log entry and outputs it to syslog. * diff --git a/lib/base/sysloglogger.h b/lib/base/sysloglogger.h index 213e3104e..a16edee7a 100644 --- a/lib/base/sysloglogger.h +++ b/lib/base/sysloglogger.h @@ -37,6 +37,8 @@ class I2_BASE_API SyslogLogger : public ObjectImpl public: DECLARE_PTR_TYPEDEFS(SyslogLogger); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + protected: virtual void ProcessLogEntry(const LogEntry& entry); }; diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index e98d8c2dc..424be3b75 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -22,7 +22,9 @@ #include "base/objectlock.h" #include "base/utility.h" #include "base/dynamictype.h" +#include "base/statsfunction.h" #include +#include using namespace icinga; @@ -166,3 +168,24 @@ HostStatistics CIB::CalculateHostStats(void) return hs; } + + +std::pair CIB::GetFeatureStats(void) +{ + Dictionary::Ptr status = make_shared(); + Dictionary::Ptr perfdata = make_shared(); + + String name; + Value ret; + BOOST_FOREACH(boost::tie(name, boost::tuples::ignore), StatsFunctionRegistry::GetInstance()->GetItems()) { + StatsFunction::Ptr func = StatsFunctionRegistry::GetInstance()->GetItem(name); + + if (!func) + BOOST_THROW_EXCEPTION(std::invalid_argument("Function '" + name + "' does not exist.")); + + ret = func->Invoke(status, perfdata); + } + + return std::make_pair(status, perfdata); +} + diff --git a/lib/icinga/cib.h b/lib/icinga/cib.h index e44d5bdcf..f88e8338f 100644 --- a/lib/icinga/cib.h +++ b/lib/icinga/cib.h @@ -22,6 +22,7 @@ #include "icinga/i2-icinga.h" #include "base/ringbuffer.h" +#include "base/dictionary.h" namespace icinga { @@ -76,6 +77,8 @@ public: static ServiceStatistics CalculateServiceStats(void); static HostStatistics CalculateHostStats(void); + static std::pair GetFeatureStats(void); + private: CIB(void); diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index ffef35b6b..5b651afad 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -27,6 +27,7 @@ #include "base/timer.h" #include "base/scriptvariable.h" #include "base/initialize.h" +#include "base/statsfunction.h" using namespace icinga; @@ -45,6 +46,16 @@ void IcingaApplication::StaticInitialize(void) ScriptVariable::Set("IcingaNodeName", Utility::GetHostName()); } +REGISTER_STATSFUNCTION(IcingaApplicationStats, &IcingaApplication::StatsFunc); + +Value IcingaApplication::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata) +{ + /* FIXME */ + status->Set("icingaapplication_", 1); + + return 0; +} + /** * The entry point for the Icinga application. * diff --git a/lib/icinga/icingaapplication.h b/lib/icinga/icingaapplication.h index cc8b1369d..474dc4fa5 100644 --- a/lib/icinga/icingaapplication.h +++ b/lib/icinga/icingaapplication.h @@ -42,6 +42,8 @@ public: int Main(void); + static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata); + static IcingaApplication::Ptr GetInstance(void); String GetPidPath(void) const;