1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "methods/clusterchecktask.hpp"
21 #include "remote/apilistener.hpp"
22 #include "remote/endpoint.hpp"
23 #include "icinga/cib.hpp"
24 #include "icinga/service.hpp"
25 #include "icinga/icingaapplication.hpp"
26 #include "base/application.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/convert.hpp"
29 #include "base/utility.hpp"
30 #include "base/function.hpp"
31 #include "base/configtype.hpp"
32 #include <boost/algorithm/string/join.hpp>
33 #include <boost/foreach.hpp>
35 using namespace icinga;
37 REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc);
39 void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
40 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
42 if (resolvedMacros && !useResolvedMacros)
45 ApiListener::Ptr listener = ApiListener::GetInstance();
48 cr->SetOutput("No API listener is configured for this instance.");
49 cr->SetState(ServiceUnknown);
50 checkable->ProcessCheckResult(cr);
54 std::pair<Dictionary::Ptr, Dictionary::Ptr> stats = listener->GetStatus();
56 Dictionary::Ptr status = stats.first;
58 /* use feature stats perfdata */
59 std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
60 cr->SetPerformanceData(feature_stats.second);
62 String connected_endpoints = FormatArray(status->Get("conn_endpoints"));
63 String not_connected_endpoints = FormatArray(status->Get("not_conn_endpoints"));
65 if (status->Get("num_not_conn_endpoints") > 0) {
66 cr->SetState(ServiceCritical);
67 cr->SetOutput("Icinga 2 Cluster Problem: " + Convert::ToString(status->Get("num_not_conn_endpoints")) +
68 " Endpoints (" + not_connected_endpoints + ") not connected.");
70 cr->SetState(ServiceOK);
71 cr->SetOutput("Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(status->Get("num_conn_endpoints")) +
72 " (" + connected_endpoints + ").");
75 checkable->ProcessCheckResult(cr);
78 String ClusterCheckTask::FormatArray(const Array::Ptr& arr)
84 ObjectLock olock(arr);
85 BOOST_FOREACH(const Value& value, arr) {
91 str += Convert::ToString(value);