2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
#include "methods/clusterzonechecktask.hpp"
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "remote/zone.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2015-02-24 09:27:37 +01:00
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
|
2014-05-25 17:10:57 +02:00
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2014-05-25 17:10:57 +02:00
|
|
|
{
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(checkable);
|
|
|
|
REQUIRE_NOT_NULL(cr);
|
2018-01-30 11:26:07 +01:00
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
2020-08-03 08:07:32 +02:00
|
|
|
CheckCommand::Ptr command = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
|
2020-07-20 16:29:26 +02:00
|
|
|
String commandName = command->GetName();
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
if (!listener) {
|
2020-07-20 16:29:26 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-11-23 16:39:24 +01:00
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:15:15 +02:00
|
|
|
Value raw_command = command->GetCommandLine();
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
MacroProcessor::ResolverList resolvers;
|
2020-11-23 16:39:24 +01:00
|
|
|
|
2020-07-31 14:21:09 +02:00
|
|
|
if (MacroResolver::OverrideMacros)
|
|
|
|
resolvers.emplace_back("override", MacroResolver::OverrideMacros);
|
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
if (service)
|
2017-11-30 08:19:58 +01:00
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
resolvers.emplace_back("host", host);
|
2019-07-10 10:15:15 +02:00
|
|
|
resolvers.emplace_back("command", command);
|
2017-11-30 08:19:58 +01:00
|
|
|
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
|
2014-05-25 17:10:57 +02:00
|
|
|
|
2014-12-01 13:19:07 +01:00
|
|
|
String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
|
2017-12-19 15:50:05 +01:00
|
|
|
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2014-12-01 13:19:07 +01:00
|
|
|
|
2016-06-24 10:17:45 +02:00
|
|
|
String missingLagWarning;
|
|
|
|
String missingLagCritical;
|
|
|
|
|
|
|
|
double lagWarning = MacroProcessor::ResolveMacros("$cluster_lag_warning$", resolvers, checkable->GetLastCheckResult(),
|
2017-12-19 15:50:05 +01:00
|
|
|
&missingLagWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2016-06-24 10:17:45 +02:00
|
|
|
|
|
|
|
double lagCritical = MacroProcessor::ResolveMacros("$cluster_lag_critical$", resolvers, checkable->GetLastCheckResult(),
|
2017-12-19 15:50:05 +01:00
|
|
|
&missingLagCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2016-06-24 10:17:45 +02:00
|
|
|
|
2014-12-01 13:19:07 +01:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
if (zoneName.IsEmpty()) {
|
2020-07-20 16:29:26 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-11-23 16:39:24 +01:00
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Zone::Ptr zone = Zone::GetByName(zoneName);
|
|
|
|
|
|
|
|
if (!zone) {
|
2020-07-20 16:29:26 +02:00
|
|
|
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);
|
|
|
|
}
|
2014-05-25 17:10:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool connected = false;
|
2015-09-25 14:23:42 +02:00
|
|
|
double zoneLag = 0;
|
2014-05-25 17:10:57 +02:00
|
|
|
|
2017-11-13 16:30:29 +01:00
|
|
|
double lastMessageSent = 0;
|
|
|
|
double lastMessageReceived = 0;
|
|
|
|
double messagesSentPerSecond = 0;
|
|
|
|
double messagesReceivedPerSecond = 0;
|
|
|
|
double bytesSentPerSecond = 0;
|
|
|
|
double bytesReceivedPerSecond = 0;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Endpoint::Ptr& endpoint : zone->GetEndpoints()) {
|
2015-10-22 10:52:38 +02:00
|
|
|
if (endpoint->GetConnected())
|
2014-05-25 17:10:57 +02:00
|
|
|
connected = true;
|
2015-03-03 14:40:05 +01:00
|
|
|
|
2015-09-25 14:23:42 +02:00
|
|
|
double eplag = ApiListener::CalculateZoneLag(endpoint);
|
|
|
|
|
|
|
|
if (eplag > 0 && eplag > zoneLag)
|
|
|
|
zoneLag = eplag;
|
2017-11-13 16:30:29 +01:00
|
|
|
|
|
|
|
if (endpoint->GetLastMessageSent() > lastMessageSent)
|
|
|
|
lastMessageSent = endpoint->GetLastMessageSent();
|
|
|
|
|
|
|
|
if (endpoint->GetLastMessageReceived() > lastMessageReceived)
|
|
|
|
lastMessageReceived = endpoint->GetLastMessageReceived();
|
|
|
|
|
|
|
|
messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
|
|
|
|
messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
|
|
|
|
bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
|
|
|
|
bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:29:26 +02:00
|
|
|
ServiceState state;
|
|
|
|
String output;
|
2020-11-23 16:39:24 +01:00
|
|
|
|
2018-07-31 10:40:54 +02:00
|
|
|
if (connected) {
|
2020-07-20 16:29:26 +02:00
|
|
|
state = ServiceOK;
|
|
|
|
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag);
|
2018-07-31 10:41:14 +02:00
|
|
|
|
|
|
|
/* Check whether the thresholds have been resolved and compare them */
|
|
|
|
if (missingLagCritical.IsEmpty() && zoneLag > lagCritical) {
|
2020-07-20 16:29:26 +02:00
|
|
|
state = ServiceCritical;
|
|
|
|
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
|
|
|
|
+ " greater than critical threshold: " + Utility::FormatDuration(lagCritical);
|
2018-07-31 10:41:14 +02:00
|
|
|
} else if (missingLagWarning.IsEmpty() && zoneLag > lagWarning) {
|
2020-07-20 16:29:26 +02:00
|
|
|
state = ServiceWarning;
|
|
|
|
output = "Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
|
|
|
|
+ " greater than warning threshold: " + Utility::FormatDuration(lagWarning);
|
2018-07-31 10:41:14 +02:00
|
|
|
}
|
2018-07-31 10:40:54 +02:00
|
|
|
} else {
|
2020-07-20 16:29:26 +02:00
|
|
|
state = ServiceCritical;
|
|
|
|
output = "Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag);
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:29:26 +02:00
|
|
|
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->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);
|
|
|
|
}
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|