Send event::ExecutedCommand where necessary

This commit is contained in:
Mattia Codato 2020-07-21 15:51:57 +02:00
parent 2e8b492f29
commit 05b1beb2ff
1 changed files with 103 additions and 25 deletions

View File

@ -231,11 +231,42 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
if (command_type == "check_command") { if (command_type == "check_command") {
if (!CheckCommand::GetByName(command)) { if (!CheckCommand::GetByName(command)) {
CheckResult::Ptr cr = new CheckResult(); ServiceState state = ServiceUnknown;
cr->SetState(ServiceUnknown); String output = "Check command '" + command + "' does not exist.";
cr->SetOutput("Check command '" + command + "' does not exist."); double now = Utility::GetTime();
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
listener->SyncSendMessage(sourceEndpoint, message); 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()) {
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);
}
} else {
CheckResult::Ptr cr = new CheckResult();
cr->SetState(state);
cr->SetOutput(output);
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
listener->SyncSendMessage(sourceEndpoint, message);
}
return; return;
} }
} else if (command_type == "event_command") { } else if (command_type == "event_command") {
@ -265,20 +296,43 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
try { try {
host->ExecuteRemoteCheck(macros); host->ExecuteRemoteCheck(macros);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
CheckResult::Ptr cr = new CheckResult();
cr->SetState(ServiceUnknown);
String output = "Exception occurred while checking '" + host->GetName() + "': " + DiagnosticInformation(ex); String output = "Exception occurred while checking '" + host->GetName() + "': " + DiagnosticInformation(ex);
cr->SetOutput(output); ServiceState state = ServiceUnknown;
double now = Utility::GetTime(); double now = Utility::GetTime();
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr); if (params->Contains("source")) {
listener->SyncSendMessage(sourceEndpoint, message); 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);
}
} else {
CheckResult::Ptr cr = new CheckResult();
cr->SetState(state);
cr->SetOutput(output);
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
listener->SyncSendMessage(sourceEndpoint, message);
}
Log(LogCritical, "checker", output); Log(LogCritical, "checker", output);
} }
@ -315,17 +369,41 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
+ "' and user '" + user->GetName() + "' using command '" + command + "': " + "' and user '" + user->GetName() + "' using command '" + command + "': "
+ DiagnosticInformation(ex, false); + DiagnosticInformation(ex, false);
double now = Utility::GetTime(); double now = Utility::GetTime();
ServiceState state = ServiceUnknown;
CheckResult::Ptr cr = new CheckResult(); if (params->Contains("source")) {
cr->SetState(ServiceUnknown); Dictionary::Ptr executedParams = new Dictionary();
cr->SetOutput(output); executedParams->Set("execution", params->Get("source"));
cr->SetScheduleStart(now); executedParams->Set("host", params->Get("host"));
cr->SetScheduleEnd(now); if (params->Contains("service"))
cr->SetExecutionStart(now); executedParams->Set("service", params->Get("service"));
cr->SetExecutionEnd(now); executedParams->Set("exit", state);
executedParams->Set("output", output);
executedParams->Set("start", now);
executedParams->Set("end", now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr); if (origin->IsLocal()) {
listener->SyncSendMessage(sourceEndpoint, message); 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);
}
} else {
CheckResult::Ptr cr = new CheckResult();
cr->SetState(state);
cr->SetOutput(output);
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
listener->SyncSendMessage(sourceEndpoint, message);
}
Log(LogCritical, "checker", output); Log(LogCritical, "checker", output);
} }