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,53 +661,56 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
executions->Set(uuid, pending_execution); executions->Set(uuid, pending_execution);
checkable->SetExecutions(executions); checkable->SetExecutions(executions);
/* Broadcast the update */ Endpoint::Ptr endpointPtr = Endpoint::GetByName(resolved_endpoint);
ApiListener::Ptr listener = ApiListener::GetInstance(); bool local = !endpointPtr || endpointPtr == Endpoint::GetLocalEndpoint();
if (!listener) if (local) {
return ApiActions::CreateResult(404, "No ApiListener instance available."); /* TODO */
} else {
/* Broadcast the update */
ApiListener::Ptr listener = ApiListener::GetInstance();
if (!listener)
return ApiActions::CreateResult(404, "No ApiListener instance available.");
Dictionary::Ptr updateParams = new Dictionary(); Dictionary::Ptr updateParams = new Dictionary();
updateParams->Set("host", host->GetName()); updateParams->Set("host", host->GetName());
if (service) if (service)
updateParams->Set("service", service->GetShortName()); updateParams->Set("service", service->GetShortName());
updateParams->Set("executions", executions); updateParams->Set("executions", executions);
Dictionary::Ptr updateMessage = new Dictionary(); Dictionary::Ptr updateMessage = new Dictionary();
updateMessage->Set("jsonrpc", "2.0"); updateMessage->Set("jsonrpc", "2.0");
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->SyncSendMessage(endpointPtr, updateMessage);
listener->RelayMessage(origin, checkable, updateMessage, true);
/* Execute command */ /* Execute command */
Dictionary::Ptr execMessage = new Dictionary(); Dictionary::Ptr execMessage = new Dictionary();
execMessage->Set("jsonrpc", "2.0"); execMessage->Set("jsonrpc", "2.0");
execMessage->Set("method", "event::ExecuteCommand"); execMessage->Set("method", "event::ExecuteCommand");
/* TODO set the right params */ /* TODO set the right params */
Dictionary::Ptr execParams = new Dictionary(); Dictionary::Ptr execParams = new Dictionary();
execMessage->Set("params", execParams); execMessage->Set("params", execParams);
execParams->Set("command_type", command_type); execParams->Set("command_type", command_type);
execParams->Set("command", resolved_command); execParams->Set("command", resolved_command);
execParams->Set("host", host->GetName()); execParams->Set("host", host->GetName());
if (service) if (service)
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);