Set exit code 126 if endpoint doens't support the new executeCommand API

This commit is contained in:
Mattia Codato 2020-08-05 15:53:34 +02:00
parent 7c004af6be
commit 7474ab6de5
2 changed files with 48 additions and 47 deletions

View File

@ -824,13 +824,12 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
if (childEndpoint->GetIcingaVersion() < 21300) { if (childEndpoint->GetIcingaVersion() < 21300) {
/* Update execution */ /* Update execution */
double now = Utility::GetTime(); double now = Utility::GetTime();
pending_execution->Set("exit", 2); pending_execution->Set("exit", 126);
pending_execution->Set("output", "Endpoint '" + childEndpoint->GetName() + "' has version < 2.13."); pending_execution->Set("output", "Endpoint '" + childEndpoint->GetName() + "' has version < 2.13.");
pending_execution->Set("start", now); pending_execution->Set("start", now);
pending_execution->Set("end", now); pending_execution->Set("end", now);
pending_execution->Remove("pending"); pending_execution->Remove("pending");
checkable->SetExecutions(executions);
listener->RelayMessage(origin, checkable, updateMessage, true); listener->RelayMessage(origin, checkable, updateMessage, true);
Dictionary::Ptr result = new Dictionary(); Dictionary::Ptr result = new Dictionary();

View File

@ -623,64 +623,66 @@ Value ClusterEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin,
if (!listener) if (!listener)
return Empty; return Empty;
Endpoint::Ptr execEndpoint = Endpoint::GetByName(params->Get("endpoint")); if (params->Contains("endpoint")) {
if (execEndpoint != Endpoint::GetLocalEndpoint()) { Endpoint::Ptr execEndpoint = Endpoint::GetByName(params->Get("endpoint"));
if (execEndpoint != Endpoint::GetLocalEndpoint()) {
Zone::Ptr endpointZone = execEndpoint->GetZone(); Zone::Ptr endpointZone = execEndpoint->GetZone();
Zone::Ptr localZone = Zone::GetLocalZone(); Zone::Ptr localZone = Zone::GetLocalZone();
if (!endpointZone->IsChildOf(localZone)) { if (!endpointZone->IsChildOf(localZone)) {
return Empty; return Empty;
} }
/* Check if the child endpoints have Icinga version >= 2.13 */ /* Check if the child endpoints have Icinga version >= 2.13 */
for (const Zone::Ptr& zone : ConfigType::GetObjectsByType<Zone>()) { for (const Zone::Ptr &zone : ConfigType::GetObjectsByType<Zone>()) {
/* Fetch immediate child zone members */ /* Fetch immediate child zone members */
if (zone->GetParent() == localZone && zone->CanAccessObject(endpointZone)) { if (zone->GetParent() == localZone && zone->CanAccessObject(endpointZone)) {
std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints(); std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints();
for (const Endpoint::Ptr& childEndpoint : endpoints) { for (const Endpoint::Ptr &childEndpoint : endpoints) {
if (childEndpoint->GetIcingaVersion() < 21300) { if (childEndpoint->GetIcingaVersion() < 21300) {
/* Update execution */ double now = Utility::GetTime();
double now = Utility::GetTime(); Dictionary::Ptr executedParams = new Dictionary();
Dictionary::Ptr execution = new Dictionary(); executedParams->Set("execution", params->Get("source"));
execution->Set("exit", 2); executedParams->Set("host", params->Get("host"));
execution->Set("output", "Endpoint '" + childEndpoint->GetName() + "' has version < 2.13."); if (params->Contains("service"))
execution->Set("start", now); executedParams->Set("service", params->Get("service"));
execution->Set("end", now); executedParams->Set("exit", 126);
executedParams->Set("output",
"Endpoint '" + childEndpoint->GetName() + "' has version < 2.13.");
executedParams->Set("start", now);
executedParams->Set("end", now);
Dictionary::Ptr executionsToBroadcast = new Dictionary(); if (origin->IsLocal()) {
executionsToBroadcast->Set(params->Get("source"), execution); ClusterEvents::ExecutedCommandAPIHandler(origin, executedParams);
} else {
Dictionary::Ptr executedMessage = new Dictionary();
executedMessage->Set("jsonrpc", "2.0");
executedMessage->Set("method", "event::ExecutedCommand");
executedMessage->Set("params", executedParams);
Dictionary::Ptr updateParams = new Dictionary(); listener->RelayMessage(nullptr, nullptr, executedMessage, true);
updateParams->Set("host", params->Get("host")); }
if (params->Contains("service"))
updateParams->Set("service", params->Get("service"));
updateParams->Set("executions", executionsToBroadcast);
Dictionary::Ptr updateMessage = new Dictionary(); return Empty;
updateMessage->Set("jsonrpc", "2.0"); }
updateMessage->Set("method", "event::UpdateExecutions");
updateMessage->Set("params", executionsToBroadcast);
listener->RelayMessage(nullptr, nullptr, updateMessage, true);
return Empty;
} }
} }
} }
Dictionary::Ptr execMessage = new Dictionary();
execMessage->Set("jsonrpc", "2.0");
execMessage->Set("method", "event::ExecuteCommand");
execMessage->Set("params", params);
listener->RelayMessage(origin, endpointZone, execMessage, true);
return Empty;
} }
Dictionary::Ptr execMessage = new Dictionary();
execMessage->Set("jsonrpc", "2.0");
execMessage->Set("method", "event::ExecuteCommand");
execMessage->Set("params", params);
listener->RelayMessage(origin, endpointZone, execMessage, true);
} else {
EnqueueCheck(origin, params);
} }
EnqueueCheck(origin, params);
return Empty; return Empty;
} }