From 75df3879f2bb1880a273d84991525e0c974e120f Mon Sep 17 00:00:00 2001 From: htriem Date: Thu, 2 May 2019 16:24:42 +0200 Subject: [PATCH] Implement sleep CheckCommand in memory Implements a check task with Utility::Sleep and custom var parameter sleep_time (default value: 1s) refs #6964 --- itl/command-icinga.conf | 4 +++ lib/methods/CMakeLists.txt | 1 + lib/methods/methods-itl.conf | 9 +++++- lib/methods/sleepchecktask.cpp | 51 ++++++++++++++++++++++++++++++++++ lib/methods/sleepchecktask.hpp | 30 ++++++++++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 lib/methods/sleepchecktask.cpp create mode 100644 lib/methods/sleepchecktask.hpp 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