[icinga-checkins] icinga.org: icinga2/master: Implement REGISTER_SCRIPTFUNCTION() and clean up how check types are registered.

git at icinga.org git at icinga.org
Tue Jan 22 09:21:57 CET 2013


Module: icinga2
Branch: master
Commit: 02de634c12f663e86bd58503946ba37743d6d2d1
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=02de634c12f663e86bd58503946ba37743d6d2d1

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Tue Jan 22 09:21:50 2013 +0100

Implement REGISTER_SCRIPTFUNCTION() and clean up how check types are registered.

---

 components/checker/checkercomponent.cpp |    4 ----
 lib/base/dynamictype.h                  |    2 +-
 lib/base/scriptfunction.h               |   21 ++++++++++++++++++++-
 lib/icinga/nullchecktask.cpp            |    8 ++------
 lib/icinga/nullchecktask.h              |    3 ---
 lib/icinga/pluginchecktask.cpp          |    8 ++------
 lib/icinga/pluginchecktask.h            |    3 +--
 7 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp
index 928289a..744349b 100644
--- a/components/checker/checkercomponent.cpp
+++ b/components/checker/checkercomponent.cpp
@@ -37,10 +37,6 @@ void CheckerComponent::Start(void)
 	m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
 	m_CheckTimer->Start();
 
-	/* TODO: figure out a way to register check types */
-	PluginCheckTask::Register();
-	NullCheckTask::Register();
-
 	m_ResultTimer = boost::make_shared<Timer>();
 	m_ResultTimer->SetInterval(5);
 	m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
diff --git a/lib/base/dynamictype.h b/lib/base/dynamictype.h
index 7b47c75..818223b 100644
--- a/lib/base/dynamictype.h
+++ b/lib/base/dynamictype.h
@@ -100,7 +100,7 @@ shared_ptr<T> DynamicObjectFactory(const Dictionary::Ptr& serializedUpdate)
 }
 
 #define REGISTER_TYPE_ALIAS(type, alias, attributeDesc) \
-	static RegisterTypeHelper g_Register ## type(alias, DynamicObjectFactory<type>, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast<AttributeDescription *>(attributeDesc))[0]))
+	static RegisterTypeHelper g_RegisterDT_ ## type(alias, DynamicObjectFactory<type>, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast<AttributeDescription *>(attributeDesc))[0]))
 
 #define REGISTER_TYPE(type, attributeDesc) \
 	REGISTER_TYPE_ALIAS(type, #type, attributeDesc)
diff --git a/lib/base/scriptfunction.h b/lib/base/scriptfunction.h
index 86a4594..1075211 100644
--- a/lib/base/scriptfunction.h
+++ b/lib/base/scriptfunction.h
@@ -52,7 +52,26 @@ private:
 	static map<String, ScriptFunction::Ptr> m_Functions;
 };
 
+/**
+ * Helper class for registering ScriptFunction implementation classes.
+ *
+ * @ingroup base
+ */
+class RegisterFunctionHelper
+{
+public:
+	RegisterFunctionHelper(const String& name, const ScriptFunction::Callback& function)
+	{
+		if (!ScriptFunction::GetByName(name)) {
+			ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(function);
+			ScriptFunction::Register(name, func);
+		}
+	}
+};
+
+#define REGISTER_SCRIPTFUNCTION(name, callback) \
+	static RegisterFunctionHelper g_RegisterSF_ ## type(name, callback)
+
 }
 
 #endif /* SCRIPTFUNCTION_H */
-
diff --git a/lib/icinga/nullchecktask.cpp b/lib/icinga/nullchecktask.cpp
index 3df0234..9d6e92c 100644
--- a/lib/icinga/nullchecktask.cpp
+++ b/lib/icinga/nullchecktask.cpp
@@ -21,6 +21,8 @@
 
 using namespace icinga;
 
+REGISTER_SCRIPTFUNCTION("native::NullCheck",  &NullCheckTask::ScriptFunc);
+
 void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments)
 {
 	if (arguments.size() < 1)
@@ -37,9 +39,3 @@ void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>&
 
 	task->FinishResult(cr);
 }
-
-void NullCheckTask::Register(void)
-{
-	ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(&NullCheckTask::ScriptFunc);
-	ScriptFunction::Register("native::NullCheck", func);
-}
diff --git a/lib/icinga/nullchecktask.h b/lib/icinga/nullchecktask.h
index 13b2681..a38abaf 100644
--- a/lib/icinga/nullchecktask.h
+++ b/lib/icinga/nullchecktask.h
@@ -31,9 +31,6 @@ namespace icinga
 class I2_ICINGA_API NullCheckTask
 {
 public:
-	static void Register(void);
-
-private:
 	static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 };
 
diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp
index b699c4c..e2ca0d1 100644
--- a/lib/icinga/pluginchecktask.cpp
+++ b/lib/icinga/pluginchecktask.cpp
@@ -21,6 +21,8 @@
 
 using namespace icinga;
 
+REGISTER_SCRIPTFUNCTION("native::PluginCheck",  &PluginCheckTask::ScriptFunc);
+
 PluginCheckTask::PluginCheckTask(const ScriptTask::Ptr& task, const Process::Ptr& process)
 	: m_Task(task), m_Process(process)
 { }
@@ -124,9 +126,3 @@ void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String&
 	result->Set("output", text);
 	result->Set("performance_data_raw", perfdata);
 }
-
-void PluginCheckTask::Register(void)
-{
-	ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(&PluginCheckTask::ScriptFunc);
-	ScriptFunction::Register("native::PluginCheck", func);
-}
diff --git a/lib/icinga/pluginchecktask.h b/lib/icinga/pluginchecktask.h
index d8dd8b3..b5b46e0 100644
--- a/lib/icinga/pluginchecktask.h
+++ b/lib/icinga/pluginchecktask.h
@@ -31,10 +31,9 @@ namespace icinga
 class I2_ICINGA_API PluginCheckTask
 {
 public:
-	static void Register(void);
+	static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 
 private:
-	static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 
 	static void ProcessFinishedHandler(PluginCheckTask ct);
 	static void ProcessCheckOutput(const Dictionary::Ptr& result, String& output);





More information about the icinga-checkins mailing list