Use ExecuteCommandProcessFinishedHandler for all lib/methods/*task.cpp

This commit is contained in:
Mattia Codato 2020-07-20 16:29:26 +02:00
parent acc986afd0
commit 8758e58b92
8 changed files with 256 additions and 91 deletions

View File

@ -29,45 +29,69 @@ void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRe
return;
CheckCommand::Ptr command = checkable->GetCheckCommand();
cr->SetCommand(command->GetName());
String commandName = command->GetName();
ApiListener::Ptr listener = ApiListener::GetInstance();
if (!listener) {
cr->SetOutput("No API listener is configured for this instance.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "No API listener is configured for this instance.";
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = 126;
pr.Output = output;
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetOutput(output);
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
}
return;
}
std::pair<Dictionary::Ptr, Dictionary::Ptr> stats = listener->GetStatus();
Dictionary::Ptr status = stats.first;
/* use feature stats perfdata */
std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
cr->SetPerformanceData(feature_stats.second);
int numConnEndpoints = status->Get("num_conn_endpoints");
int numNotConnEndpoints = status->Get("num_not_conn_endpoints");
ServiceState state;
String output = "Icinga 2 Cluster";
if (numNotConnEndpoints > 0) {
output += " Problem: " + Convert::ToString(numNotConnEndpoints) + " endpoints are not connected.";
output += "\n(" + FormatArray(status->Get("not_conn_endpoints")) + ")";
cr->SetState(ServiceCritical);
state = ServiceCritical;
} else {
output += " OK: " + Convert::ToString(numConnEndpoints) + " endpoints are connected.";
output += "\n(" + FormatArray(status->Get("conn_endpoints")) + ")";
cr->SetState(ServiceOK);
state = ServiceOK;
}
cr->SetOutput(output);
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
checkable->ProcessCheckResult(cr);
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
/* use feature stats perfdata */
std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
cr->SetPerformanceData(feature_stats.second);
cr->SetCommand(commandName);
cr->SetState(state);
cr->SetOutput(output);
checkable->ProcessCheckResult(cr);
}
}
String ClusterCheckTask::FormatArray(const Array::Ptr& arr)

View File

@ -21,15 +21,32 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
REQUIRE_NOT_NULL(cr);
ApiListener::Ptr listener = ApiListener::GetInstance();
CheckCommand::Ptr command = checkable->GetCheckCommand();
String commandName = command->GetName();
if (!listener) {
cr->SetOutput("No API listener is configured for this instance.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "No API listener is configured for this instance.";
ServiceState state = ServiceUnknown;
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetCommand(commandName);
cr->SetOutput(output);
cr->SetState(state);
checkable->ProcessCheckResult(cr);
}
return;
}
CheckCommand::Ptr command = checkable->GetCheckCommand();
Value raw_command = command->GetCommandLine();
Host::Ptr host;
@ -58,21 +75,51 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
if (resolvedMacros && !useResolvedMacros)
return;
cr->SetCommand(command->GetName());
if (zoneName.IsEmpty()) {
cr->SetOutput("Macro 'cluster_zone' must be set.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "Macro 'cluster_zone' must be set.";
ServiceState state = ServiceUnknown;
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetCommand(commandName);
cr->SetOutput(output);
cr->SetState(state);
checkable->ProcessCheckResult(cr);
}
return;
}
Zone::Ptr zone = Zone::GetByName(zoneName);
if (!zone) {
cr->SetOutput("Zone '" + zoneName + "' does not exist.");
cr->SetState(ServiceUnknown);
checkable->ProcessCheckResult(cr);
String output = "Zone '" + zoneName + "' does not exist.";
ServiceState state = ServiceUnknown;
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetCommand(commandName);
cr->SetOutput(output);
cr->SetState(state);
checkable->ProcessCheckResult(cr);
}
return;
}
@ -107,34 +154,51 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
}
ServiceState state;
String output;
if (connected) {
cr->SetState(ServiceOK);
cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag));
state = ServiceOK;
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag);
/* Check whether the thresholds have been resolved and compare them */
if (missingLagCritical.IsEmpty() && zoneLag > lagCritical) {
cr->SetState(ServiceCritical);
cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
+ " greater than critical threshold: " + Utility::FormatDuration(lagCritical));
state = ServiceCritical;
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
+ " greater than critical threshold: " + Utility::FormatDuration(lagCritical);
} else if (missingLagWarning.IsEmpty() && zoneLag > lagWarning) {
cr->SetState(ServiceWarning);
cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
+ " greater than warning threshold: " + Utility::FormatDuration(lagWarning));
state = ServiceWarning;
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
+ " greater than warning threshold: " + Utility::FormatDuration(lagWarning);
}
} else {
cr->SetState(ServiceCritical);
cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag));
state = ServiceCritical;
output = "Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag);
}
cr->SetPerformanceData(new Array({
new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical),
new PerfdataValue("last_messages_sent", lastMessageSent),
new PerfdataValue("last_messages_received", lastMessageReceived),
new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond),
new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond),
new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond),
new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond)
}));
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
checkable->ProcessCheckResult(cr);
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetCommand(commandName);
cr->SetState(state);
cr->SetOutput(output);
cr->SetPerformanceData(new Array({
new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical),
new PerfdataValue("last_messages_sent", lastMessageSent),
new PerfdataValue("last_messages_received", lastMessageReceived),
new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond),
new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond),
new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond),
new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond)
}));
checkable->ProcessCheckResult(cr);
}
}

View File

@ -48,14 +48,26 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
std::pair<String, String> co = PluginUtility::ParseCheckOutput(dummyText);
double now = Utility::GetTime();
String commandName = command->GetName();
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(dummyState));
cr->SetExitStatus(dummyState);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
cr->SetCommand(command->GetName());
if (Checkable::ExecuteCommandProcessFinishedHandler) {
ProcessResult pr;
pr.PID = -1;
pr.Output = co.first;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = dummyState;
checkable->ProcessCheckResult(cr);
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(dummyState));
cr->SetExitStatus(dummyState);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
cr->SetCommand(commandName);
checkable->ProcessCheckResult(cr);
}
}

View File

@ -23,5 +23,19 @@ void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Check
if (resolvedMacros && !useResolvedMacros)
return;
BOOST_THROW_EXCEPTION(ScriptError("Test") << boost::errinfo_api_function("Test"));
ScriptError scriptError = ScriptError("Test") << boost::errinfo_api_function("Test");
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = scriptError.what();
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = 0;
Checkable::ExecuteCommandProcessFinishedHandler("", pr);
} else {
BOOST_THROW_EXCEPTION(ScriptError("Test") << boost::errinfo_api_function("Test"));
}
}

View File

@ -148,7 +148,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
cr->SetPerformanceData(perfdata);
cr->SetState(ServiceOK);
ServiceState state = ServiceOK;
String appVersion = Application::GetAppVersion();
@ -160,7 +160,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
if (lastReloadFailed > 0) {
output += "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed);
cr->SetState(ServiceWarning);
state =ServiceWarning;
}
/* Indicate a warning when the last synced config caused a stage validation error. */
@ -173,7 +173,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
output += "; Last zone sync stage validation failed at "
+ Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", validationResult->Get("ts"));
cr->SetState(ServiceWarning);
state = ServiceWarning;
}
}
@ -182,11 +182,25 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
/* Return an error if the version is less than specified (optional). */
if (missingIcingaMinVersion.IsEmpty() && !icingaMinVersion.IsEmpty() && Utility::CompareVersion(icingaMinVersion, parsedAppVersion) < 0) {
output += "; Minimum version " + icingaMinVersion + " is not installed.";
cr->SetState(ServiceCritical);
state = ServiceCritical;
}
cr->SetOutput(output);
cr->SetCommand(command->GetName());
String commandName = command->GetName();
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
checkable->ProcessCheckResult(cr);
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetState(state);
cr->SetOutput(output);
cr->SetCommand(commandName);
checkable->ProcessCheckResult(cr);
}
}

View File

@ -26,12 +26,25 @@ void NullCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResul
String output = "Hello from ";
output += IcingaApplication::GetInstance()->GetNodeName();
ServiceState state = ServiceOK;
cr->SetOutput(output);
cr->SetPerformanceData(new Array({
new PerfdataValue("time", Convert::ToDouble(Utility::GetTime()))
}));
cr->SetState(ServiceOK);
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
checkable->ProcessCheckResult(cr);
Checkable::ExecuteCommandProcessFinishedHandler("", pr);
} else {
cr->SetOutput(output);
cr->SetPerformanceData(new Array({
new PerfdataValue("time", Convert::ToDouble(Utility::GetTime()))
}));
cr->SetState(state);
checkable->ProcessCheckResult(cr);
}
}

View File

@ -31,22 +31,35 @@ void RandomCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
+ ". Icinga 2 has been running for " + Utility::FormatDuration(uptime)
+ ". Version: " + Application::GetAppVersion();
cr->SetOutput(output);
double random = Utility::Random() % 1000;
cr->SetPerformanceData(new Array({
new PerfdataValue("time", now),
new PerfdataValue("value", random),
new PerfdataValue("value_1m", random * 0.9),
new PerfdataValue("value_5m", random * 0.8),
new PerfdataValue("uptime", uptime),
}));
cr->SetState(static_cast<ServiceState>(Utility::Random() % 4));
CheckCommand::Ptr command = checkable->GetCheckCommand();
cr->SetCommand(command->GetName());
String commandName = command->GetName();
ServiceState state = static_cast<ServiceState>(Utility::Random() % 4);
checkable->ProcessCheckResult(cr);
if (Checkable::ExecuteCommandProcessFinishedHandler) {
double now = Utility::GetTime();
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = state;
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
cr->SetOutput(output);
double random = Utility::Random() % 1000;
cr->SetPerformanceData(new Array({
new PerfdataValue("time", now),
new PerfdataValue("value", random),
new PerfdataValue("value_1m", random * 0.9),
new PerfdataValue("value_5m", random * 0.8),
new PerfdataValue("uptime", uptime),
}));
cr->SetState(state);
cr->SetCommand(commandName);
checkable->ProcessCheckResult(cr);
}
}

View File

@ -42,13 +42,24 @@ void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
String output = "Slept for " + Convert::ToString(sleepTime) + " seconds.";
double now = Utility::GetTime();
CheckCommand::Ptr command = checkable->GetCheckCommand();
String commandName = command->GetName();
cr->SetOutput(output);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
if (Checkable::ExecuteCommandProcessFinishedHandler) {
ProcessResult pr;
pr.PID = -1;
pr.Output = output;
pr.ExecutionStart = now;
pr.ExecutionEnd = now;
pr.ExitStatus = 0;
CheckCommand::Ptr command = checkable->GetCheckCommand();
cr->SetCommand(command->GetName());
Checkable::ExecuteCommandProcessFinishedHandler("", pr);
} else {
cr->SetOutput(output);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
cr->SetCommand(commandName);
checkable->ProcessCheckResult(cr);
checkable->ProcessCheckResult(cr);
}
}