Unify code to check if command exists

This commit is contained in:
Mattia Codato 2020-07-22 14:16:41 +02:00
parent 96dc349240
commit 4e3a38f320
1 changed files with 36 additions and 48 deletions

View File

@ -225,61 +225,49 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
String command = params->Get("command"); String command = params->Get("command");
String command_type = params->Get("command_type"); String command_type = params->Get("command_type");
if (command_type == "check_command") { if (command_type == "check_command" && !CheckCommand::GetByName(command) ||
if (!CheckCommand::GetByName(command)) { command_type == "event_command" && !EventCommand::GetByName(command) ||
ServiceState state = ServiceUnknown; command_type == "notification_command" && !NotificationCommand::GetByName(command)) {
String output = "Check command '" + command + "' does not exist."; ServiceState state = ServiceUnknown;
double now = Utility::GetTime(); String output = command_type + " '" + command + "' does not exist.";
double now = Utility::GetTime();
if (params->Contains("source")) {
Dictionary::Ptr executedParams = new Dictionary();
executedParams->Set("execution", params->Get("source"));
executedParams->Set("host", params->Get("host"));
if (params->Contains("service"))
executedParams->Set("service", params->Get("service"));
executedParams->Set("exit", state);
executedParams->Set("output", output);
executedParams->Set("start", now);
executedParams->Set("end", now);
if (origin->IsLocal()) { if (params->Contains("source")) {
ClusterEvents::ExecutedCommandAPIHandler(origin, executedParams); Dictionary::Ptr executedParams = new Dictionary();
} else { executedParams->Set("execution", params->Get("source"));
Dictionary::Ptr executedMessage = new Dictionary(); executedParams->Set("host", params->Get("host"));
executedMessage->Set("jsonrpc", "2.0"); if (params->Contains("service"))
executedMessage->Set("method", "event::ExecutedCommand"); executedParams->Set("service", params->Get("service"));
executedMessage->Set("params", executedParams); executedParams->Set("exit", state);
executedParams->Set("output", output);
executedParams->Set("start", now);
executedParams->Set("end", now);
listener->SyncSendMessage(sourceEndpoint, executedMessage); if (origin->IsLocal()) {
} ClusterEvents::ExecutedCommandAPIHandler(origin, executedParams);
} else { } else {
CheckResult::Ptr cr = new CheckResult(); Dictionary::Ptr executedMessage = new Dictionary();
cr->SetState(state); executedMessage->Set("jsonrpc", "2.0");
cr->SetOutput(output); executedMessage->Set("method", "event::ExecutedCommand");
cr->SetScheduleStart(now); executedMessage->Set("params", executedParams);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now); listener->SyncSendMessage(sourceEndpoint, executedMessage);
cr->SetExecutionEnd(now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
listener->SyncSendMessage(sourceEndpoint, message);
} }
return; } else {
} CheckResult::Ptr cr = new CheckResult();
} else if (command_type == "event_command") { cr->SetState(state);
if (!EventCommand::GetByName(command)) { cr->SetOutput(output);
Log(LogWarning, "ClusterEvents") cr->SetScheduleStart(now);
<< "Event command '" << command << "' does not exist."; cr->SetScheduleEnd(now);
return; cr->SetExecutionStart(now);
} cr->SetExecutionEnd(now);
} else if (command_type == "notification_command") { Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
if (!NotificationCommand::GetByName(command)) { listener->SyncSendMessage(sourceEndpoint, message);
Log(LogWarning, "ClusterEvents")
<< "Notification command '" << command << "' does not exist.";
return;
} }
return;
} }
attrs->Set(command_type, params->Get("command")); attrs->Set(command_type, command);
attrs->Set("command_endpoint", sourceEndpoint->GetName()); attrs->Set("command_endpoint", sourceEndpoint->GetName());
Deserialize(host, attrs, false, FAConfig); Deserialize(host, attrs, false, FAConfig);