Replace executeactiontask with a lambda function

This commit is contained in:
Mattia Codato 2020-07-13 09:47:19 +02:00
parent fa4aebbfd2
commit 6bad8bbc0f
5 changed files with 49 additions and 123 deletions

View File

@ -6,7 +6,6 @@
#include "base/configuration.hpp"
#include "base/serializer.hpp"
#include "base/exception.hpp"
#include "methods/executeactiontask.hpp"
#include <boost/thread/once.hpp>
#include <thread>
@ -122,12 +121,50 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
Log(LogNotice, "ApiListener")
<< "Discarding 'ExecuteCheckFromQueue' event for checkable '" << checkable->GetName()
<< "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
<< "Discarding 'ExecuteCheckFromQueue' event for checkable '" << checkable->GetName()
<< "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
return;
}
Checkable::ExecuteCommandProcessFinishedHandler = ExecuteActionTask::ProcessFinishedHandler;
Checkable::ExecuteCommandProcessFinishedHandler = [listener, sourceEndpoint, origin, params] (const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr) -> void {
Checkable::CurrentConcurrentChecks.fetch_sub(1);
Checkable::DecreasePendingChecks();
if (pr.ExitStatus > 3) {
Process::Arguments parguments = Process::PrepareCommand(commandLine);
Log(LogWarning, "ApiListener")
<< "Check command for object '" << checkable->GetName() << "' (PID: " << pr.PID
<< ", arguments: " << Process::PrettyPrintArguments(parguments) << ") terminated with exit code "
<< pr.ExitStatus << ", output: " << pr.Output;
}
String output = pr.Output.Trim();
std::pair<String, String> co = PluginUtility::ParseCheckOutput(output);
cr->SetCommand(commandLine);
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(pr.ExitStatus));
cr->SetExitStatus(pr.ExitStatus);
cr->SetExecutionStart(pr.ExecutionStart);
cr->SetExecutionEnd(pr.ExecutionEnd);
Dictionary::Ptr executedParams = new Dictionary();
params->CopyTo(executedParams);
executedParams->Set("execution", params->Get("source"));
executedParams->Set("check_result", Serialize(cr));
if (origin->IsLocal()) {
ClusterEvents::ExecutedCommandAPIHandler(origin, executedParams);
} else {
Dictionary::Ptr executedMessage = new Dictionary();
executedMessage->Set("jsonrpc", "2.0");
executedMessage->Set("method", "event::ExecutedCommand");
executedMessage->Set("params", executedParams);
listener->SyncSendMessage(sourceEndpoint, executedMessage);
}
};
} else {
Checkable::ExecuteCommandProcessFinishedHandler = nullptr;
}

View File

@ -1002,14 +1002,6 @@ Value ClusterEvents::ExecutedCommandAPIHandler(const MessageOrigin::Ptr& origin,
return Empty;
}
if (!params->Contains("check_result")) {
Log(LogNotice, "ClusterEvents")
<< "Discarding 'update executions API handler' message for checkable '" << checkable->GetName()
<< "' from '" << origin->FromClient->GetIdentity() << "': No check result available.";
return Empty;
}
CheckResult::Ptr cr = params->Get("check_result");
Dictionary::Ptr execution = executions->Get(uuid);
if (!execution) {
Log(LogNotice, "ClusterEvents")
@ -1018,8 +1010,15 @@ Value ClusterEvents::ExecutedCommandAPIHandler(const MessageOrigin::Ptr& origin,
return Empty;
}
if (!params->Contains("check_result")) {
Log(LogNotice, "ClusterEvents")
<< "Discarding 'update executions API handler' message for checkable '" << checkable->GetName()
<< "' from '" << origin->FromClient->GetIdentity() << "': No check result available.";
return Empty;
}
execution->Set("check_result", params->Get("check_result"));
execution->Set("pending", false);
execution->Set("check_result", cr);
/* Broadcast the update */
Dictionary::Ptr executionsToBroadcast = new Dictionary();

View File

@ -17,7 +17,6 @@ set(methods_SOURCES
randomchecktask.cpp randomchecktask.hpp
timeperiodtask.cpp timeperiodtask.hpp
sleepchecktask.cpp sleepchecktask.hpp
executeactiontask.cpp exceptionchecktask.hpp
)
if(ICINGA2_UNITY_BUILD)

View File

@ -1,77 +0,0 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/executeactiontask.hpp"
#include "icinga/pluginutility.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/clusterevents.hpp"
#include "icinga/macroprocessor.hpp"
#include "icinga/icingaapplication.hpp"
#include "remote/apilistener.hpp"
#include "base/configtype.hpp"
#include "base/logger.hpp"
#include "base/function.hpp"
#include "base/utility.hpp"
#include "base/process.hpp"
#include "base/convert.hpp"
using namespace icinga;
void ExecuteActionTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr)
{
Checkable::CurrentConcurrentChecks.fetch_sub(1);
Checkable::DecreasePendingChecks();
if (pr.ExitStatus > 3) {
Process::Arguments parguments = Process::PrepareCommand(commandLine);
Log(LogWarning, "PluginCheckTask")
<< "Check command for object '" << checkable->GetName() << "' (PID: " << pr.PID
<< ", arguments: " << Process::PrettyPrintArguments(parguments) << ") terminated with exit code "
<< pr.ExitStatus << ", output: " << pr.Output;
}
String output = pr.Output.Trim();
std::pair<String, String> co = PluginUtility::ParseCheckOutput(output);
cr->SetCommand(commandLine);
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(pr.ExitStatus));
cr->SetExitStatus(pr.ExitStatus);
cr->SetExecutionStart(pr.ExecutionStart);
cr->SetExecutionEnd(pr.ExecutionEnd);
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
Dictionary::Ptr executedParams = new Dictionary();
executedParams->Set("host", host->GetName());
if (service)
executedParams->Set("service", service->GetShortName());
/* TODO set the execution UUID */
/*executedParams->Set("execution", uuid);*/
executedParams->Set("check_result", cr);
/* FIXME command endpoint overwritten by macro? */
Endpoint::Ptr commandEndpoint = checkable->GetCommandEndpoint();
bool local = !commandEndpoint || commandEndpoint == Endpoint::GetLocalEndpoint();
if (local) {
MessageOrigin::Ptr origin = new MessageOrigin();
ClusterEvents::ExecutedCommandAPIHandler(origin, executedParams);
} else {
ApiListener::Ptr listener = ApiListener::GetInstance();
if (listener) {
Dictionary::Ptr executedMessage = new Dictionary();
executedMessage->Set("jsonrpc", "2.0");
executedMessage->Set("method", "event::ExecutedCommand");
executedMessage->Set("params", executedParams);
listener->SyncSendMessage(commandEndpoint, executedMessage);
} else {
Log(LogCritical, "ExecuteActionTask") << "Api listener not found";
}
}
}

View File

@ -1,32 +0,0 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef EXECUTEACTIONTASK_H
#define EXECUTEACTIONTASK_H
#include "methods/i2-methods.hpp"
#include "base/process.hpp"
#include "icinga/service.hpp"
namespace icinga
{
/**
* Implements service checks based on external plugins.
*
* @ingroup methods
*/
class ExecuteActionTask
{
public:
static void ProcessFinishedHandler(const Checkable::Ptr& service,
const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr);
static thread_local String ExecutionUUID;
private:
ExecuteActionTask();
};
}
#endif /* EXECUTEACTIONTASK_H */