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

View File

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