[icinga-checkins] icinga.org: icinga2/master: Implement support for time periods.

git at icinga.org git at icinga.org
Mon Jan 21 13:47:47 CET 2013


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

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Mon Jan 21 13:47:36 2013 +0100

Implement support for time periods.

---

 lib/base/asynctask.h              |    8 ++++
 lib/base/dynamicobject.h          |   14 ++++++
 lib/icinga/Makefile.am            |    4 +-
 lib/icinga/i2-icinga.h            |    2 +
 lib/icinga/icinga.vcxproj         |    4 +-
 lib/icinga/icinga.vcxproj.filters |    6 +++
 lib/icinga/timeperiod.cpp         |   80 +++++++++++++++++++++++++++++++++++++
 lib/icinga/timeperiod.h           |   46 +++++++++++++++++++++
 8 files changed, 162 insertions(+), 2 deletions(-)

diff --git a/lib/base/asynctask.h b/lib/base/asynctask.h
index e05c1c3..f195a18 100644
--- a/lib/base/asynctask.h
+++ b/lib/base/asynctask.h
@@ -75,6 +75,14 @@ public:
 	}
 
 	/**
+	 * Checks whether the task is finished.
+	 */
+	bool IsFinished(void) const
+	{
+		return m_Finished;
+	}
+
+	/**
 	 * Retrieves the result of the task. Throws an exception if one is stored in
 	 * the AsyncTask object.
 	 *
diff --git a/lib/base/dynamicobject.h b/lib/base/dynamicobject.h
index 828622d..f809f84 100644
--- a/lib/base/dynamicobject.h
+++ b/lib/base/dynamicobject.h
@@ -97,6 +97,20 @@ public:
 	static boost::signal<void (const DynamicObject::Ptr&)> OnUnregistered;
 	static boost::signal<void (const set<DynamicObject::Ptr>&)> OnTransactionClosing;
 
+	/**
+	 * Synchronously invokes a method. The method must not return before it's finished.
+	 *
+	 * @param method The name of the method.
+	 * @param arguments Arguments for the method.
+	 */
+	template<typename T>
+	T InvokeMethodSync(const String& method, const vector<Value>& arguments)
+	{
+		ScriptTask::Ptr task = InvokeMethod(method, arguments, ScriptTask::CompletionCallback());
+		assert(task->IsFinished());
+		return task->GetResult();
+	}
+
 	ScriptTask::Ptr InvokeMethod(const String& method,
 	    const vector<Value>& arguments, ScriptTask::CompletionCallback callback);
 
diff --git a/lib/icinga/Makefile.am b/lib/icinga/Makefile.am
index 9e003ac..5e40953 100644
--- a/lib/icinga/Makefile.am
+++ b/lib/icinga/Makefile.am
@@ -25,7 +25,9 @@ libicinga_la_SOURCES =  \
 	servicegroup.h \
 	service.h \
 	servicestatechangemessage.cpp \
-	servicestatechangemessage.h
+	servicestatechangemessage.h \
+	timeperiod.cpp \
+	timeperiod.h
 
 libicinga_la_CPPFLAGS = \
 	-DI2_ICINGA_BUILD \
diff --git a/lib/icinga/i2-icinga.h b/lib/icinga/i2-icinga.h
index cc924f4..f221e68 100644
--- a/lib/icinga/i2-icinga.h
+++ b/lib/icinga/i2-icinga.h
@@ -44,6 +44,8 @@ using boost::algorithm::is_any_of;
 #include "endpointmanager.h"
 #include "icingaapplication.h"
 
+#include "timeperiod.h"
+
 #include "host.h"
 #include "hostgroup.h"
 #include "service.h"
diff --git a/lib/icinga/icinga.vcxproj b/lib/icinga/icinga.vcxproj
index b8d5727..7f07355 100644
--- a/lib/icinga/icinga.vcxproj
+++ b/lib/icinga/icinga.vcxproj
@@ -35,6 +35,7 @@
     <ClCompile Include="service.cpp" />
     <ClCompile Include="servicegroup.cpp" />
     <ClCompile Include="servicestatechangemessage.cpp" />
+    <ClCompile Include="timeperiod.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="cib.h" />
@@ -48,6 +49,7 @@
     <ClInclude Include="service.h" />
     <ClInclude Include="servicegroup.h" />
     <ClInclude Include="servicestatechangemessage.h" />
+    <ClInclude Include="timeperiod.h" />
   </ItemGroup>
   <PropertyGroup Label="Globals">
     <ProjectGuid>{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}</ProjectGuid>
@@ -199,4 +201,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/lib/icinga/icinga.vcxproj.filters b/lib/icinga/icinga.vcxproj.filters
index 10cb4bb..73df5d7 100644
--- a/lib/icinga/icinga.vcxproj.filters
+++ b/lib/icinga/icinga.vcxproj.filters
@@ -34,6 +34,9 @@
     <ClCompile Include="pluginchecktask.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="timeperiod.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="i2-icinga.h">
@@ -69,6 +72,9 @@
     <ClInclude Include="pluginchecktask.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="timeperiod.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Filter Include="Headerdateien">
diff --git a/lib/icinga/timeperiod.cpp b/lib/icinga/timeperiod.cpp
new file mode 100644
index 0000000..7e35ef2
--- /dev/null
+++ b/lib/icinga/timeperiod.cpp
@@ -0,0 +1,80 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 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 "i2-icinga.h"
+
+using namespace icinga;
+
+static AttributeDescription timePeriodAttributes[] = {
+	{ "alias", Attribute_Config },
+	{ "cached_state", Attribute_Transient },
+	{ "cached_next_change", Attribute_Transient }
+};
+
+REGISTER_TYPE(TimePeriod, timePeriodAttributes);
+
+String TimePeriod::GetAlias(void) const
+{
+	String value = Get("alias");
+
+	if (!value.IsEmpty())
+		return value;
+	else
+		return GetName();
+}
+
+bool TimePeriod::Exists(const String& name)
+{
+	return (DynamicObject::GetObject("TimePeriod", name));
+}
+
+TimePeriod::Ptr TimePeriod::GetByName(const String& name)
+{
+	DynamicObject::Ptr configObject = DynamicObject::GetObject("TimePeriod", name);
+
+	if (!configObject)
+		throw_exception(invalid_argument("TimePeriod '" + name + "' does not exist."));
+
+	return dynamic_pointer_cast<TimePeriod>(configObject);
+}
+
+bool TimePeriod::IsActive(void) {
+	if (GetNextChange() > Utility::GetTime()) {
+		vector<Value> args;
+		args.push_back(static_cast<TimePeriod::Ptr>(GetSelf()));
+		return InvokeMethodSync<bool>("is_active", args);
+	} else {
+		return Get("cached_state");
+	}
+}
+
+double TimePeriod::GetNextChange(void) {
+	double next_change = Get("cached_next_change");
+
+	if (next_change < Utility::GetTime()) {
+		vector<Value> args;
+		args.push_back(static_cast<TimePeriod::Ptr>(GetSelf()));
+		next_change = InvokeMethodSync<bool>("is_active", args);
+		// TODO: figure out next_change by calling method
+
+		Set("cached_next_change", next_change);
+	}
+
+	return next_change;
+}
diff --git a/lib/icinga/timeperiod.h b/lib/icinga/timeperiod.h
new file mode 100644
index 0000000..fbf2184
--- /dev/null
+++ b/lib/icinga/timeperiod.h
@@ -0,0 +1,46 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 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 TIMEPERIOD_H
+#define TIMEPERIOD_H
+
+namespace icinga
+{
+	
+class TimePeriod : public DynamicObject {
+public:
+	typedef shared_ptr<TimePeriod> Ptr;
+	typedef weak_ptr<TimePeriod> WeakPtr;
+
+	TimePeriod(const Dictionary::Ptr& properties)
+		: DynamicObject(properties)
+	{ }
+
+	static bool Exists(const String& name);
+	static TimePeriod::Ptr GetByName(const String& name);
+
+	String GetAlias(void) const;
+
+	bool IsActive(void);
+	double GetNextChange(void);
+};
+
+}
+
+#endif /* TIMEPERIOD_H */
\ No newline at end of file





More information about the icinga-checkins mailing list