mirror of https://github.com/Icinga/icinga2.git
Code refactoring: create sendEventExecuteCommand function
This commit is contained in:
parent
e690eaf3c4
commit
6656afb259
|
@ -75,6 +75,31 @@ void ClusterEvents::EnqueueCheck(const MessageOrigin::Ptr& origin, const Diction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void sendEventExecuteCommand(const Dictionary::Ptr& params, const long& exit, const String& output,
|
||||||
|
const double& start, const double& end, const ApiListener::Ptr& listener, const MessageOrigin::Ptr& origin,
|
||||||
|
const Endpoint::Ptr& sourceEndpoint) {
|
||||||
|
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", exit);
|
||||||
|
executedParams->Set("output", output);
|
||||||
|
executedParams->Set("start", start);
|
||||||
|
executedParams->Set("end", end);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params) {
|
void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params) {
|
||||||
|
|
||||||
Endpoint::Ptr sourceEndpoint;
|
Endpoint::Ptr sourceEndpoint;
|
||||||
|
@ -131,26 +156,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
<< pr.ExitStatus << ", output: " << pr.Output;
|
<< pr.ExitStatus << ", output: " << pr.Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary::Ptr executedParams = new Dictionary();
|
sendEventExecuteCommand(params, pr.ExitStatus, pr.Output, pr.ExecutionStart, pr.ExecutionEnd, listener, origin, sourceEndpoint);
|
||||||
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", pr.ExitStatus);
|
|
||||||
executedParams->Set("output", pr.Output);
|
|
||||||
executedParams->Set("start", pr.ExecutionStart);
|
|
||||||
executedParams->Set("end", pr.ExecutionEnd);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,23 +167,8 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
String output = "Endpoint '" + Endpoint::GetLocalEndpoint()->GetName() + "' does not accept commands.";
|
String output = "Endpoint '" + Endpoint::GetLocalEndpoint()->GetName() + "' does not accept commands.";
|
||||||
|
|
||||||
if (params->Contains("source")) {
|
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"));
|
|
||||||
double now = Utility::GetTime();
|
double now = Utility::GetTime();
|
||||||
executedParams->Set("exit", 126);
|
sendEventExecuteCommand(params, 126, output, now, now, listener, origin, sourceEndpoint);
|
||||||
executedParams->Set("output", output);
|
|
||||||
executedParams->Set("start", now);
|
|
||||||
executedParams->Set("end", now);
|
|
||||||
|
|
||||||
Dictionary::Ptr executedMessage = new Dictionary();
|
|
||||||
executedMessage->Set("jsonrpc", "2.0");
|
|
||||||
executedMessage->Set("method", "event::ExecutedCommand");
|
|
||||||
executedMessage->Set("params", executedParams);
|
|
||||||
|
|
||||||
listener->SyncSendMessage(sourceEndpoint, executedMessage);
|
|
||||||
} else {
|
} else {
|
||||||
Host::Ptr host = new Host();
|
Host::Ptr host = new Host();
|
||||||
Dictionary::Ptr attrs = new Dictionary();
|
Dictionary::Ptr attrs = new Dictionary();
|
||||||
|
@ -232,34 +223,11 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
double now = Utility::GetTime();
|
double now = Utility::GetTime();
|
||||||
|
|
||||||
if (params->Contains("source")) {
|
if (params->Contains("source")) {
|
||||||
Dictionary::Ptr executedParams = new Dictionary();
|
sendEventExecuteCommand(params, state, output, now, now, listener, origin, sourceEndpoint);
|
||||||
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 {
|
} else {
|
||||||
CheckResult::Ptr cr = new CheckResult();
|
CheckResult::Ptr cr = new CheckResult();
|
||||||
cr->SetState(state);
|
cr->SetState(state);
|
||||||
cr->SetOutput(output);
|
cr->SetOutput(output);
|
||||||
cr->SetScheduleStart(now);
|
|
||||||
cr->SetScheduleEnd(now);
|
|
||||||
cr->SetExecutionStart(now);
|
|
||||||
cr->SetExecutionEnd(now);
|
|
||||||
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
|
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
|
||||||
listener->SyncSendMessage(sourceEndpoint, message);
|
listener->SyncSendMessage(sourceEndpoint, message);
|
||||||
}
|
}
|
||||||
|
@ -273,27 +241,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
if (params->Contains("source")) {
|
if (params->Contains("source")) {
|
||||||
double now = Utility::GetTime();
|
double now = Utility::GetTime();
|
||||||
ServiceState state = ServiceUnknown;
|
ServiceState state = ServiceUnknown;
|
||||||
|
sendEventExecuteCommand(params, state, output, now, now, listener, origin, sourceEndpoint);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -305,27 +253,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
if (params->Contains("source")) {
|
if (params->Contains("source")) {
|
||||||
double now = Utility::GetTime();
|
double now = Utility::GetTime();
|
||||||
ServiceState state = ServiceUnknown;
|
ServiceState state = ServiceUnknown;
|
||||||
|
sendEventExecuteCommand(params, state, output, now, now, listener, origin, sourceEndpoint);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -349,26 +277,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
double now = Utility::GetTime();
|
double now = Utility::GetTime();
|
||||||
|
|
||||||
if (params->Contains("source")) {
|
if (params->Contains("source")) {
|
||||||
Dictionary::Ptr executedParams = new Dictionary();
|
sendEventExecuteCommand(params, state, output, now, now, listener, origin, sourceEndpoint);
|
||||||
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 {
|
} else {
|
||||||
CheckResult::Ptr cr = new CheckResult();
|
CheckResult::Ptr cr = new CheckResult();
|
||||||
cr->SetState(state);
|
cr->SetState(state);
|
||||||
|
@ -420,26 +329,7 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
||||||
ServiceState state = ServiceUnknown;
|
ServiceState state = ServiceUnknown;
|
||||||
|
|
||||||
if (params->Contains("source")) {
|
if (params->Contains("source")) {
|
||||||
Dictionary::Ptr executedParams = new Dictionary();
|
sendEventExecuteCommand(params, state, output, now, now, listener, origin, sourceEndpoint);
|
||||||
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 {
|
} else {
|
||||||
CheckResult::Ptr cr = new CheckResult();
|
CheckResult::Ptr cr = new CheckResult();
|
||||||
cr->SetState(state);
|
cr->SetState(state);
|
||||||
|
|
Loading…
Reference in New Issue