2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-02-07 08:03:53 -05:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "methods/clusterchecktask.hpp"
|
|
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
|
#include "icinga/cib.hpp"
|
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
2019-07-10 04:15:15 -04:00
|
|
|
#include "icinga/checkcommand.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/application.hpp"
|
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
|
#include "base/utility.hpp"
|
2015-01-21 02:47:45 -05:00
|
|
|
#include "base/function.hpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-02-07 08:03:53 -05:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2025-05-22 11:56:16 -04:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc, "checkable:cr:producer:resolvedMacros:useResolvedMacros");
|
2014-02-07 08:03:53 -05:00
|
|
|
|
2014-11-13 05:23:57 -05:00
|
|
|
void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2025-05-22 11:56:16 -04:00
|
|
|
const WaitGroup::Ptr& producer, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2014-02-07 08:03:53 -05:00
|
|
|
{
|
2018-02-21 07:42:58 -05:00
|
|
|
REQUIRE_NOT_NULL(checkable);
|
|
|
|
|
REQUIRE_NOT_NULL(cr);
|
2018-01-30 05:26:07 -05:00
|
|
|
|
2014-12-01 07:19:07 -05:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-08-03 02:07:32 -04:00
|
|
|
CheckCommand::Ptr command = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
|
2020-07-20 10:29:26 -04:00
|
|
|
String commandName = command->GetName();
|
2019-07-10 04:15:15 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
|
if (!listener) {
|
2020-07-20 10:29:26 -04:00
|
|
|
String output = "No API listener is configured for this instance.";
|
2020-11-23 10:39:24 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
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);
|
2025-05-22 11:56:16 -04:00
|
|
|
checkable->ProcessCheckResult(cr, producer);
|
2020-07-20 10:29:26 -04:00
|
|
|
}
|
2020-11-23 10:39:24 -05:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
return;
|
2014-02-07 08:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
std::pair<Dictionary::Ptr, Dictionary::Ptr> stats = listener->GetStatus();
|
2014-02-18 04:53:44 -05:00
|
|
|
Dictionary::Ptr status = stats.first;
|
2018-01-18 09:18:50 -05:00
|
|
|
int numConnEndpoints = status->Get("num_conn_endpoints");
|
|
|
|
|
int numNotConnEndpoints = status->Get("num_not_conn_endpoints");
|
|
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
ServiceState state;
|
2018-01-18 09:18:50 -05:00
|
|
|
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")) + ")";
|
2014-02-13 13:23:38 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
state = ServiceCritical;
|
2014-05-20 06:28:50 -04:00
|
|
|
} else {
|
2018-01-18 09:18:50 -05:00
|
|
|
output += " OK: " + Convert::ToString(numConnEndpoints) + " endpoints are connected.";
|
|
|
|
|
output += "\n(" + FormatArray(status->Get("conn_endpoints")) + ")";
|
|
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
state = ServiceOK;
|
2014-02-07 08:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 10:29:26 -04: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;
|
2018-01-18 09:18:50 -05:00
|
|
|
|
2020-07-20 10:29:26 -04:00
|
|
|
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);
|
|
|
|
|
|
2025-05-22 11:56:16 -04:00
|
|
|
checkable->ProcessCheckResult(cr, producer);
|
2020-07-20 10:29:26 -04:00
|
|
|
}
|
2014-02-07 08:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 13:23:38 -05:00
|
|
|
String ClusterCheckTask::FormatArray(const Array::Ptr& arr)
|
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
|
|
|
|
String str;
|
|
|
|
|
|
|
|
|
|
if (arr) {
|
|
|
|
|
ObjectLock olock(arr);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Value& value : arr) {
|
2014-02-13 13:23:38 -05:00
|
|
|
if (first)
|
|
|
|
|
first = false;
|
|
|
|
|
else
|
2014-05-26 03:39:14 -04:00
|
|
|
str += ", ";
|
2014-02-13 13:23:38 -05:00
|
|
|
|
|
|
|
|
str += Convert::ToString(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
}
|