]> granicus.if.org Git - icinga2/commitdiff
Implement StatsFunction Registry for features.
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 17 Feb 2014 15:34:18 +0000 (16:34 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 17 Feb 2014 16:53:41 +0000 (17:53 +0100)
Refs #5622

36 files changed:
components/checker/checkercomponent.cpp
components/checker/checkercomponent.h
components/cluster/clusterlistener.cpp
components/cluster/clusterlistener.h
components/compat/checkresultreader.cpp
components/compat/checkresultreader.h
components/compat/compatlogger.cpp
components/compat/compatlogger.h
components/compat/externalcommandlistener.cpp
components/compat/externalcommandlistener.h
components/compat/statusdatawriter.cpp
components/compat/statusdatawriter.h
components/db_ido_mysql/idomysqlconnection.cpp
components/db_ido_mysql/idomysqlconnection.h
components/db_ido_pgsql/idopgsqlconnection.cpp
components/db_ido_pgsql/idopgsqlconnection.h
components/livestatus/listener.cpp
components/livestatus/listener.h
components/notification/notificationcomponent.cpp
components/notification/notificationcomponent.h
components/perfdata/graphitewriter.cpp
components/perfdata/graphitewriter.h
components/perfdata/perfdatawriter.cpp
components/perfdata/perfdatawriter.h
lib/base/CMakeLists.txt
lib/base/dictionary.cpp
lib/base/filelogger.cpp
lib/base/filelogger.h
lib/base/statsfunction.cpp [new file with mode: 0644]
lib/base/statsfunction.h [new file with mode: 0644]
lib/base/sysloglogger.cpp
lib/base/sysloglogger.h
lib/icinga/cib.cpp
lib/icinga/cib.h
lib/icinga/icingaapplication.cpp
lib/icinga/icingaapplication.h

index be2bc27aa40d9016b3a33ff3bfcdf49bade0f0e5..3245027fafdac4c1bdf41fdf1fdb99bca7cf0165 100644 (file)
 #include "base/utility.h"
 #include "base/logger_fwd.h"
 #include "base/exception.h"
+#include "base/statsfunction.h"
 #include <boost/foreach.hpp>
 
 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)
 {
index 516feec21c71165d525b7d243c7e8b3a303e54b1..63cc70a1adaea3e7f9bc7d3caa83832c548df49f 100644 (file)
@@ -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;
index a27b0db8ee469ae01d8d59e49e0d5117813c4d37..d043d78919b0e8f1566b804d0df4d1f02c3eed7b 100644 (file)
 #include "base/application.h"
 #include "base/convert.h"
 #include "base/context.h"
+#include "base/statsfunction.h"
 #include <fstream>
 
 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<Dictionary::Ptr, Dictionary::Ptr> 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();
index cbcdd36b6f98d46714bfc46d0d351277aee3792a..65c7de276c6da542bf9a463a7a4b7bd39bd3d465 100644 (file)
@@ -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);
 
index f86d58e91142f5bea7cc5ece47a9fa67b288cb9f..664561b8d59ccb287f7a2017562b0110e55e74a5 100644 (file)
 #include "base/utility.h"
 #include "base/exception.h"
 #include "base/context.h"
+#include "base/statsfunction.h"
 #include <fstream>
 
 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.
  */
index 6c1012ff8484d6466eaff0cd07373a7ea0c53e41..13586a6a5b1e069679261dfd2c5bc0112119d589 100644 (file)
@@ -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);
 
index 2df327c07fab94c78d42ab17dee4d473c9c417e3..252c7566a9523ee01d4f38fc8206858c51d0ec23 100644 (file)
@@ -34,6 +34,7 @@
 #include "base/application.h"
 #include "base/utility.h"
 #include "base/scriptfunction.h"
+#include "base/statsfunction.h"
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string.hpp>
 
@@ -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.
  */
index d48b5f28ad5d7a1cd86c8f1af0ca8b427452288b..0208cc1c4e0b649c752775a26554707f4f91de0e 100644 (file)
@@ -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:
index 58d9b36b8b77352780162e7f77479835cb757a79..42efa38b94c87f1d26f5cbe819824a77c0d83d62 100644 (file)
 #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.
  */
index db6eb8171b71e0b95010dc6893a894c4d9bde716..d8dce048a0d57fe6fc401ffd8875677829103341 100644 (file)
@@ -38,6 +38,8 @@ class ExternalCommandListener : public ObjectImpl<ExternalCommandListener>
 public:
        DECLARE_PTR_TYPEDEFS(ExternalCommandListener);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
 protected:
        virtual void Start(void);
 
index 315d4b9ac25144f8636733d6fa478480236aa2a8..c90571e01f984aebfb2d599553a8368472038690 100644 (file)
@@ -34,6 +34,7 @@
 #include "base/exception.h"
 #include "base/application.h"
 #include "base/context.h"
+#include "base/statsfunction.h"
 #include <boost/foreach.hpp>
 #include <boost/tuple/tuple.hpp>
 #include <boost/algorithm/string/replace.hpp>
@@ -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
index b65538d8b9041d15224f90223d722f64a1a664fd..e1cb6c995b7afbafd5e4dca47a0cfc141528db98 100644 (file)
@@ -42,6 +42,8 @@ class StatusDataWriter : public ObjectImpl<StatusDataWriter>
 public:
        DECLARE_PTR_TYPEDEFS(StatusDataWriter);
 
+       static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
 protected:
        virtual void Start(void);
 
index 15dcdaefc50f7ce29d0efa14afa1f4c9b073fded..701c9fd1b8f9eab9cecde818b93a016fe24cff6d 100644 (file)
@@ -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"
 
 using namespace icinga;
 
+#define SCHEMA_VERSION "1.11.0"
+
 REGISTER_TYPE(IdoMysqlConnection);
+REGISTER_STATSFUNCTION(IdoMysqlConnectionStats, &IdoMysqlConnection::StatsFunc);
 
-#define SCHEMA_VERSION "1.11.0"
+Value IdoMysqlConnection::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
+{
+       /* FIXME */
+       status->Set("ido_mysql_version_req", SCHEMA_VERSION);
+
+       return 0;
+}
 
 void IdoMysqlConnection::Start(void)
 {
index 17d6751e606336f1c3ba32562acee5a94b6983d9..26f8425bd8a4ad0676892f6763fba271651f15df 100644 (file)
@@ -41,6 +41,8 @@ class IdoMysqlConnection : public ObjectImpl<IdoMysqlConnection>
 public:
        DECLARE_PTR_TYPEDEFS(IdoMysqlConnection);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
 protected:
        virtual void Start(void);
        virtual void Stop(void);
index a629414998aa3faf740ae2d9c5ea2d1735f4c264..128bd278c1604c238a34577beb7cbe5beaf12eb1 100644 (file)
@@ -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"
 
 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)
 {
index 20e4b07d31b53241011153c614a4f377613d37b9..3de6c55607d9245df01a9249f43bdc521e1ae816 100644 (file)
@@ -41,6 +41,8 @@ class IdoPgsqlConnection : public ObjectImpl<IdoPgsqlConnection>
 public:
        DECLARE_PTR_TYPEDEFS(IdoPgsqlConnection);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
 protected:
        virtual void Start(void);
        virtual void Stop(void);
index baa226c281bd62e38e7623b83a09e2998fffeeb5..5b233277f70b3dace3653b52a32f5d40d2e324ba 100644 (file)
@@ -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.
  */
index 7f992e2674e01a48d0aa2729c77db3d887c64b9c..8f106a9035ddbd4804dfd4de236fd2f55d5883eb 100644 (file)
@@ -38,6 +38,8 @@ class LivestatusListener : public ObjectImpl<LivestatusListener>
 public:
        DECLARE_PTR_TYPEDEFS(LivestatusListener);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
        static int GetClientsConnected(void);
        static int GetConnections(void);
 
index 97427e804dcccd5067ecb32d4d7adb6a5c64cdd4..2843e3051963f8bff7b0b5e3559f022b2364cf6a 100644 (file)
 #include "base/logger_fwd.h"
 #include "base/utility.h"
 #include "base/exception.h"
+#include "base/statsfunction.h"
 #include <boost/foreach.hpp>
 
 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.
  */
index ac1a9a8242794b464284c496a5062e55c0a17fd6..ddb0f6ebf6d65b8e13eef421f04e1e9d63a87497 100644 (file)
@@ -36,6 +36,8 @@ class NotificationComponent : public ObjectImpl<NotificationComponent>
 public:
        DECLARE_PTR_TYPEDEFS(NotificationComponent);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
        virtual void Start(void);
 
 private:
index 583e1266fa314b2b4f359f418faa5f3a924cf240..ce8c613688132214b6b8768d3058a716015cf7e0 100644 (file)
@@ -34,6 +34,7 @@
 #include "base/networkstream.h"
 #include "base/bufferedstream.h"
 #include "base/exception.h"
+#include "base/statsfunction.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/foreach.hpp>
@@ -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();
index 2a5ac0ed5b8ed00e2e68fbabcf3317c4c90aefca..9a833ddcf734df66c8fc95ee54a632c79dd8692c 100644 (file)
@@ -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);
 
index bb3b6777ee2688c545bb8afd0bb0f9392d175dcf..32f2623ef92462662c0d3279dc5196b39d741aed 100644 (file)
 #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();
index 9dc114fd0905e94e1ed6e5f2d4d3daf969f17da2..73627815d48ffbadef84d1a46c8f779b6e4b9231 100644 (file)
@@ -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);
 
index eddda95a482f3a15a464b2bd5af9ec239036bf56..ae8b67391ff12dfcff338049fb238d2292700866 100644 (file)
@@ -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
index ff78919d38000d40c90d3698a6a69f3bc793a6d9..8f9cd3800c84d9e06f682e853a5d07f5fcaaacbe 100644 (file)
@@ -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.
index af88ae74ab67e8814e35c27b4553c2fd7ba7b80e..3f90c806cb1b864d4bb2adb5d565369f4a592edd 100644 (file)
 
 #include "base/filelogger.h"
 #include "base/dynamictype.h"
+#include "base/statsfunction.h"
 #include <fstream>
 
 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.
  */
index a06586c3ab364ecbd8b3d7082e8139155320130c..2c6b6572f9b83e3ee03317af9799cfcef69ad16c 100644 (file)
@@ -36,6 +36,8 @@ class I2_BASE_API FileLogger : public ObjectImpl<FileLogger>
 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 (file)
index 0000000..edc5b8b
--- /dev/null
@@ -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<StatsFunction>(function);
+       StatsFunctionRegistry::GetInstance()->Register(name, func);
+}
+
+StatsFunctionRegistry *StatsFunctionRegistry::GetInstance(void)
+{
+       return Singleton<StatsFunctionRegistry>::GetInstance();
+}
+
diff --git a/lib/base/statsfunction.h b/lib/base/statsfunction.h
new file mode 100644 (file)
index 0000000..4acbd32
--- /dev/null
@@ -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 <vector>
+#include <boost/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 boost::function<Value (Dictionary::Ptr& status, Dictionary::Ptr& perfdata)> 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<StatsFunctionRegistry, StatsFunction::Ptr>
+{
+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 */
index 7f61682a99b6001d3eda28ba9ae13866699939ab..64e203862559985d105c8b770206823a15d51050 100644 (file)
 
 #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.
  *
index 213e3104e7deb75e7775b9232ea20151ee922c23..a16edee7a832e584456e482e7fd586c245d41126 100644 (file)
@@ -37,6 +37,8 @@ class I2_BASE_API SyslogLogger : public ObjectImpl<SyslogLogger>
 public:
        DECLARE_PTR_TYPEDEFS(SyslogLogger);
 
+        static Value StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata);
+
 protected:
        virtual void ProcessLogEntry(const LogEntry& entry);
 };
index e98d8c2dcfa7bec3f867720e7b351bdf6a3bc20b..424be3b7561425853942608f6b0348cf45c35a44 100644 (file)
@@ -22,7 +22,9 @@
 #include "base/objectlock.h"
 #include "base/utility.h"
 #include "base/dynamictype.h"
+#include "base/statsfunction.h"
 #include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
 
 using namespace icinga;
 
@@ -166,3 +168,24 @@ HostStatistics CIB::CalculateHostStats(void)
 
        return hs;
 }
+
+
+std::pair<Dictionary::Ptr, Dictionary::Ptr> CIB::GetFeatureStats(void)
+{
+       Dictionary::Ptr status = make_shared<Dictionary>();
+       Dictionary::Ptr perfdata = make_shared<Dictionary>();
+
+       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);
+}
+
index e44d5bdcf1a7f3610dec842a59054bf98d076bb7..f88e8338f038ee996489b307eb1afacd40fb5093 100644 (file)
@@ -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<Dictionary::Ptr, Dictionary::Ptr> GetFeatureStats(void);
+
 private:
        CIB(void);
 
index ffef35b6b8dec85e324513ce646e28e34607eebf..5b651afad8d4a5a83e16fcbc8973caca66f913de 100644 (file)
@@ -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.
  *
index cc8b1369d5ac10f602ce0a53f5d0feb3702c82bb..474dc4fa533a7ce8b1580e2ff95097e0906ca04d 100644 (file)
@@ -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;