summaryrefslogtreecommitdiffstats
path: root/lib/methods/clusterchecktask.cpp
blob: 6ce28cac4406b5e3d70023c6f4e65621dc748fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "methods/clusterchecktask.hpp"
#include "remote/apilistener.hpp"
#include "remote/endpoint.hpp"
#include "icinga/cib.hpp"
#include "icinga/service.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/checkcommand.hpp"
#include "base/application.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/utility.hpp"
#include "base/function.hpp"
#include "base/configtype.hpp"
#include <boost/algorithm/string/join.hpp>

using namespace icinga;

REGISTER_FUNCTION_NONCONST(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");

void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
	const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
	REQUIRE_NOT_NULL(checkable);
	REQUIRE_NOT_NULL(cr);

	if (resolvedMacros && !useResolvedMacros)
		return;

	CheckCommand::Ptr command = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
	String commandName = command->GetName();

	ApiListener::Ptr listener = ApiListener::GetInstance();
	if (!listener) {
		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;
	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")) + ")";

		state = ServiceCritical;
	} else {
		output += " OK: " + Convert::ToString(numConnEndpoints) + " endpoints are connected.";
		output += "\n(" + FormatArray(status->Get("conn_endpoints")) + ")";

		state = 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::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)
{
	bool first = true;
	String str;

	if (arr) {
		ObjectLock olock(arr);
		for (const Value& value : arr) {
			if (first)
				first = false;
			else
				str += ", ";

			str += Convert::ToString(value);
		}
	}

	return str;
}