diff --git a/doc/10-icinga-template-library.md b/doc/10-icinga-template-library.md
index b5b5e7a86..1851a684e 100644
--- a/doc/10-icinga-template-library.md
+++ b/doc/10-icinga-template-library.md
@@ -160,6 +160,15 @@ Check command for the built-in `exception` check. This check throws an exception
For test and demo purposes only. The `exception` check command does not support
any vars.
+### sleep
+
+Check command for the built-in `sleep` check. This allows to use sleep for testing
+and debugging only.
+
+Name | Description
+----------------|--------------
+sleep\_time | **Optional.** The duration of the sleep in seconds. Defaults to 1s.
+
diff --git a/itl/command-icinga.conf b/itl/command-icinga.conf
index 9d28aecd5..206324a4e 100644
--- a/itl/command-icinga.conf
+++ b/itl/command-icinga.conf
@@ -35,3 +35,7 @@ object CheckCommand "random" {
object CheckCommand "exception" {
import "exception-check-command"
}
+
+object CheckCommand "sleep" {
+ import "sleep-check-command"
+}
diff --git a/lib/methods/CMakeLists.txt b/lib/methods/CMakeLists.txt
index f1cad8176..9fde9fddb 100644
--- a/lib/methods/CMakeLists.txt
+++ b/lib/methods/CMakeLists.txt
@@ -16,6 +16,7 @@ set(methods_SOURCES
pluginnotificationtask.cpp pluginnotificationtask.hpp
randomchecktask.cpp randomchecktask.hpp
timeperiodtask.cpp timeperiodtask.hpp
+ sleepchecktask.cpp sleepchecktask.hpp
)
if(ICINGA2_UNITY_BUILD)
diff --git a/lib/methods/methods-itl.conf b/lib/methods/methods-itl.conf
index 0b47c6199..f9126f7ca 100644
--- a/lib/methods/methods-itl.conf
+++ b/lib/methods/methods-itl.conf
@@ -54,6 +54,12 @@ System.assert(Internal.run_with_activation_context(function() {
template TimePeriod "even-minutes-timeperiod" use (EvenMinutesTimePeriod = Internal.EvenMinutesTimePeriod) {
update = EvenMinutesTimePeriod
}
+
+ template CheckCommand "sleep-check-command" use (SleepCheck = Internal.SleepCheck) {
+ execute = SleepCheck
+
+ vars.sleep_time = 1s
+ }
}))
var methods = [
@@ -70,7 +76,8 @@ var methods = [
"NullCheck",
"NullEvent",
"EmptyTimePeriod",
- "EvenMinutesTimePeriod"
+ "EvenMinutesTimePeriod",
+ "SleepCheck"
]
for (method in methods) {
diff --git a/lib/methods/sleepchecktask.cpp b/lib/methods/sleepchecktask.cpp
new file mode 100644
index 000000000..888a71ac8
--- /dev/null
+++ b/lib/methods/sleepchecktask.cpp
@@ -0,0 +1,51 @@
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
+
+#include "methods/sleepchecktask.hpp"
+#include "icinga/icingaapplication.hpp"
+#include "icinga/pluginutility.hpp"
+#include "base/utility.hpp"
+#include "base/convert.hpp"
+#include "base/function.hpp"
+#include "base/logger.hpp"
+
+using namespace icinga;
+
+REGISTER_FUNCTION_NONCONST(Internal, SleepCheck, &SleepCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
+
+void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
+ const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
+{
+ REQUIRE_NOT_NULL(checkable);
+ REQUIRE_NOT_NULL(cr);
+
+ CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
+
+ Host::Ptr host;
+ Service::Ptr service;
+ tie(host, service) = GetHostService(checkable);
+
+ MacroProcessor::ResolverList resolvers;
+ if (service)
+ resolvers.emplace_back("service", service);
+ resolvers.emplace_back("host", host);
+ resolvers.emplace_back("command", commandObj);
+ resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
+
+ double sleepTime = MacroProcessor::ResolveMacros("$sleep_time$", resolvers, checkable->GetLastCheckResult(),
+ nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
+
+ if (resolvedMacros && !useResolvedMacros)
+ return;
+
+ Utility::Sleep(sleepTime);
+
+ String output = "Slept for " + Convert::ToString(sleepTime) + " seconds.";
+
+ double now = Utility::GetTime();
+
+ cr->SetOutput(output);
+ cr->SetExecutionStart(now);
+ cr->SetExecutionEnd(now);
+
+ checkable->ProcessCheckResult(cr);
+}
\ No newline at end of file
diff --git a/lib/methods/sleepchecktask.hpp b/lib/methods/sleepchecktask.hpp
new file mode 100644
index 000000000..b104f60da
--- /dev/null
+++ b/lib/methods/sleepchecktask.hpp
@@ -0,0 +1,30 @@
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
+
+#ifndef SLEEPCHECKTASK_H
+#define SLEEPCHECKTASK_H
+
+#include "methods/i2-methods.hpp"
+#include "icinga/service.hpp"
+#include "base/dictionary.hpp"
+
+namespace icinga
+{
+
+/**
+ * Test class for additional check types. Implements the "sleep" check type.
+ *
+ * @ingroup methods
+ */
+class SleepCheckTask
+{
+public:
+ static void ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
+ const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros);
+
+private:
+ SleepCheckTask();
+};
+
+}
+
+#endif /* SLEEPCHECKTASK_H */
\ No newline at end of file