Implement sleep CheckCommand in memory

Implements a check task with Utility::Sleep and custom var parameter sleep_time (default value: 1s)

refs #6964
This commit is contained in:
htriem 2019-05-02 16:24:42 +02:00
parent 09dfdaa71b
commit 75df3879f2
5 changed files with 94 additions and 1 deletions

View File

@ -35,3 +35,7 @@ object CheckCommand "random" {
object CheckCommand "exception" {
import "exception-check-command"
}
object CheckCommand "sleep" {
import "sleep-check-command"
}

View File

@ -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)

View File

@ -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) {

View File

@ -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);
}

View File

@ -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 */