Fix resolve macros. Check if endpoint is local or remote

This commit is contained in:
Mattia Codato 2020-06-26 12:32:12 +02:00
parent 714e75bbd1
commit 82444a59e2
1 changed files with 54 additions and 61 deletions

View File

@ -569,32 +569,23 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
if (params->Contains("endpoint"))
endpoint = params->Get("endpoint");
/* Resolve endpoint macro */
MacroProcessor::ResolverList resolvers;
if (params->Contains("macros")) {
if (params->Get("macros").IsObjectType<Dictionary>())
resolvers.emplace_back("override",HttpUtility::GetLastParameter(params, "macros"));
else
return ApiActions::CreateResult(400, "macros must be a dictionary");
}
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
Dictionary::Ptr resolvedMacros;
bool useResolvedMacros;
if (params->Contains("macros") && !params->Get("macros").IsObjectType<Dictionary>()) {
return ApiActions::CreateResult(400, "macros must be a dictionary");
}
if (params->Contains("macros")) {
resolvedMacros = HttpUtility::GetLastParameter(params, "macros");
useResolvedMacros = true;
} else {
resolvedMacros = new Dictionary();
useResolvedMacros = false;
}
String resolved_endpoint = MacroProcessor::ResolveMacros(
endpoint, resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros,
useResolvedMacros
nullptr, MacroProcessor::EscapeCallback(), nullptr,
false
);
/* Check if endpoint exists */
@ -626,9 +617,8 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
/* Resolve command macro */
String resolved_command = MacroProcessor::ResolveMacros(
command, resolvers, checkable->GetLastCheckResult(),
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros,
useResolvedMacros
command, resolvers, checkable->GetLastCheckResult(), nullptr,
MacroProcessor::EscapeCallback(), nullptr, false
);
/* Check if resolved_command is not empty */
@ -671,6 +661,11 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
executions->Set(uuid, pending_execution);
checkable->SetExecutions(executions);
Endpoint::Ptr endpointPtr = Endpoint::GetByName(resolved_endpoint);
bool local = !endpointPtr || endpointPtr == Endpoint::GetLocalEndpoint();
if (local) {
/* TODO */
} else {
/* Broadcast the update */
ApiListener::Ptr listener = ApiListener::GetInstance();
if (!listener)
@ -687,9 +682,8 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
updateMessage->Set("method", "event::UpdateExecutions");
updateMessage->Set("params", updateParams);
/* FIXME origin? */
MessageOrigin::Ptr origin = new MessageOrigin();
listener->RelayMessage(origin, checkable, updateMessage, true);
listener->SyncSendMessage(endpointPtr, updateMessage);
/* Execute command */
Dictionary::Ptr execMessage = new Dictionary();
@ -706,18 +700,17 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
execParams->Set("service", service->GetShortName());
/*
* FIXME?
* If the host/service object specifies the 'check_timeout' attribute,
* forward this to the remote endpoint to limit the command execution time.
*/
if (!checkable->GetCheckTimeout().IsEmpty())
execParams->Set("check_timeout", checkable->GetCheckTimeout());
execParams->Set("macros", resolvedMacros);
execParams->Set("source", uuid);
execParams->Set("deadline", deadline);
listener->SyncSendMessage(Endpoint::GetByName(resolved_endpoint), execMessage);
listener->SyncSendMessage(endpointPtr, execMessage);
}
Dictionary::Ptr result = new Dictionary();
result->Set(checkable->GetName(), uuid);