Add ExecuteCommandProcessFinishedHandler and checkable param to ExecuteRemoteCheck

This commit is contained in:
Mattia Codato 2020-07-10 16:56:07 +02:00
parent c3d9f6c17b
commit 15159b1632
6 changed files with 53 additions and 11 deletions

View File

@ -492,7 +492,7 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
} }
} }
void Checkable::ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros) void Checkable::ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros, const Checkable::Ptr& checkable)
{ {
CONTEXT("Executing remote check for object '" + GetName() + "'"); CONTEXT("Executing remote check for object '" + GetName() + "'");
@ -503,7 +503,11 @@ void Checkable::ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros)
cr->SetScheduleStart(scheduled_start); cr->SetScheduleStart(scheduled_start);
cr->SetExecutionStart(before_check); cr->SetExecutionStart(before_check);
if (!checkable) {
GetCheckCommand()->Execute(this, cr, resolvedMacros, true); GetCheckCommand()->Execute(this, cr, resolvedMacros, true);
} else {
GetCheckCommand()->Execute(checkable, cr, resolvedMacros, true);
}
} }
void Checkable::ExecuteCheck() void Checkable::ExecuteCheck()

View File

@ -20,6 +20,7 @@ boost::signals2::signal<void (const Checkable::Ptr&, const String&, double, cons
boost::signals2::signal<void (const Checkable::Ptr&, double)> Checkable::OnFlappingChange; boost::signals2::signal<void (const Checkable::Ptr&, double)> Checkable::OnFlappingChange;
static Timer::Ptr l_CheckablesFireSuppressedNotifications; static Timer::Ptr l_CheckablesFireSuppressedNotifications;
thread_local std::function<void(const Checkable::Ptr&, const CheckResult::Ptr&, const Value& /* commandLine */, const ProcessResult&)> Checkable::ExecuteCommandProcessFinishedHandler;
void Checkable::StaticInitialize() void Checkable::StaticInitialize()
{ {

View File

@ -5,6 +5,7 @@
#include "base/atomic.hpp" #include "base/atomic.hpp"
#include "base/timer.hpp" #include "base/timer.hpp"
#include "base/process.hpp"
#include "icinga/i2-icinga.hpp" #include "icinga/i2-icinga.hpp"
#include "icinga/checkable-ti.hpp" #include "icinga/checkable-ti.hpp"
#include "icinga/timeperiod.hpp" #include "icinga/timeperiod.hpp"
@ -14,6 +15,7 @@
#include "remote/endpoint.hpp" #include "remote/endpoint.hpp"
#include "remote/messageorigin.hpp" #include "remote/messageorigin.hpp"
#include <cstdint> #include <cstdint>
#include <functional>
namespace icinga namespace icinga
{ {
@ -55,6 +57,7 @@ public:
DECLARE_OBJECTNAME(Checkable); DECLARE_OBJECTNAME(Checkable);
static void StaticInitialize(); static void StaticInitialize();
static thread_local std::function<void(const Checkable::Ptr&, const CheckResult::Ptr&, const Value& /* commandLine */, const ProcessResult&)> ExecuteCommandProcessFinishedHandler;
Checkable(); Checkable();
@ -94,7 +97,7 @@ public:
static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type); static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr); void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr, const Checkable::Ptr& checkbale = nullptr);
void ExecuteCheck(); void ExecuteCheck();
void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr); void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);

View File

@ -6,6 +6,7 @@
#include "base/configuration.hpp" #include "base/configuration.hpp"
#include "base/serializer.hpp" #include "base/serializer.hpp"
#include "base/exception.hpp" #include "base/exception.hpp"
#include "methods/executeactiontask.hpp"
#include <boost/thread/once.hpp> #include <boost/thread/once.hpp>
#include <thread> #include <thread>
@ -97,14 +98,40 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
return; return;
} }
Checkable::Ptr checkable = nullptr;
if (params->Contains("source")) { if (params->Contains("source")) {
Log(LogCritical, "ApiListener", "Not implemented.");
String uuid = params->Get("source"); String uuid = params->Get("source");
Host::Ptr host = Host::GetByName(params->Get("host"));
if (!host) {
Log(LogCritical, "ApiListener", "Host not found.");
return; return;
} }
if (params->Contains("service"))
checkable = host->GetServiceByShortName(params->Get("service"));
else
checkable = host;
if (!checkable) {
Log(LogCritical, "ApiListener", "Checkable not found.");
return;
}
ObjectLock oLock (checkable);
if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
Log(LogNotice, "ApiListener")
<< "Discarding 'ExecuteCheckFromQueue' event for checkable '" << checkable->GetName()
<< "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
return;
}
Checkable::ExecuteCommandProcessFinishedHandler = ExecuteActionTask::ProcessFinishedHandler;
} else {
Checkable::ExecuteCommandProcessFinishedHandler = nullptr;
}
if (!listener->GetAcceptCommands()) { if (!listener->GetAcceptCommands()) {
Log(LogWarning, "ApiListener") Log(LogWarning, "ApiListener")
<< "Ignoring command. '" << listener->GetName() << "' does not accept commands."; << "Ignoring command. '" << listener->GetName() << "' does not accept commands.";
@ -182,7 +209,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
if (command_type == "check_command") { if (command_type == "check_command") {
try { try {
host->ExecuteRemoteCheck(macros); host->ExecuteRemoteCheck(macros, checkable);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
CheckResult::Ptr cr = new CheckResult(); CheckResult::Ptr cr = new CheckResult();
cr->SetState(ServiceUnknown); cr->SetState(ServiceUnknown);

View File

@ -54,7 +54,7 @@ void ExecuteActionTask::ProcessFinishedHandler(const Checkable::Ptr& checkable,
executedParams->Set("check_result", cr); executedParams->Set("check_result", cr);
/* FIXME command endpoint was overwrite by macro? */ /* FIXME command endpoint overwritten by macro? */
Endpoint::Ptr commandEndpoint = checkable->GetCommandEndpoint(); Endpoint::Ptr commandEndpoint = checkable->GetCommandEndpoint();
bool local = !commandEndpoint || commandEndpoint == Endpoint::GetLocalEndpoint(); bool local = !commandEndpoint || commandEndpoint == Endpoint::GetLocalEndpoint();
if (local) { if (local) {

View File

@ -43,9 +43,16 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
if (!checkable->GetCheckTimeout().IsEmpty()) if (!checkable->GetCheckTimeout().IsEmpty())
timeout = checkable->GetCheckTimeout(); timeout = checkable->GetCheckTimeout();
if (Checkable::ExecuteCommandProcessFinishedHandler) {
PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(),
resolvers, resolvedMacros, useResolvedMacros, timeout,
std::bind(Checkable::ExecuteCommandProcessFinishedHandler, checkable, cr, _1, _2));
} else {
PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(), PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(),
resolvers, resolvedMacros, useResolvedMacros, timeout, resolvers, resolvedMacros, useResolvedMacros, timeout,
std::bind(&PluginCheckTask::ProcessFinishedHandler, checkable, cr, _1, _2)); std::bind(&PluginCheckTask::ProcessFinishedHandler, checkable, cr, _1, _2));
}
if (!resolvedMacros || useResolvedMacros) { if (!resolvedMacros || useResolvedMacros) {
Checkable::CurrentConcurrentChecks.fetch_add(1); Checkable::CurrentConcurrentChecks.fetch_add(1);