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();
|
|
|
|
|
|
|
|
if (!listener) {
|
|
|
|
cr->SetOutput("No API listener is configured for this instance.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
Value raw_command = commandObj->GetCommandLine();
|
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
MacroProcessor::ResolverList resolvers;
|
|
|
|
if (service)
|
2017-11-30 08:19:58 +01:00
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
resolvers.emplace_back("host", host);
|
|
|
|
resolvers.emplace_back("command", commandObj);
|
|
|
|
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()) {
|
|
|
|
cr->SetOutput("Macro 'cluster_zone' must be set.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-31 10:40:54 +02:00
|
|
|
if (connected) {
|
2014-05-25 17:10:57 +02:00
|
|
|
cr->SetState(ServiceOK);
|
2015-09-25 14:23:42 +02:00
|
|
|
cr->SetOutput("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) {
|
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
cr->SetOutput("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));
|
|
|
|
}
|
2018-07-31 10:40:54 +02:00
|
|
|
} else {
|
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag));
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
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)
|
|
|
|
}));
|
2015-02-24 09:14:17 +01:00
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
}
|