Run ExecutedCommandAPIHandler in case of command not found and if source is set

This commit is contained in:
Mattia Codato 2020-07-22 17:22:50 +02:00
parent 4e3a38f320
commit e690eaf3c4

View File

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