]> granicus.if.org Git - icinga2/blob - lib/methods/clusterchecktask.cpp
Remove deprecated functions
[icinga2] / lib / methods / clusterchecktask.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
34
35 using namespace icinga;
36
37 REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc);
38
39 void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
40     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
41 {
42         if (resolvedMacros && !useResolvedMacros)
43                 return;
44
45         ApiListener::Ptr listener = ApiListener::GetInstance();
46
47         if (!listener) {
48                 cr->SetOutput("No API listener is configured for this instance.");
49                 cr->SetState(ServiceUnknown);
50                 checkable->ProcessCheckResult(cr);
51                 return;
52         }
53
54         std::pair<Dictionary::Ptr, Dictionary::Ptr> stats = listener->GetStatus();
55
56         Dictionary::Ptr status = stats.first;
57
58         /* use feature stats perfdata */
59         std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
60         cr->SetPerformanceData(feature_stats.second);
61
62         String connected_endpoints = FormatArray(status->Get("conn_endpoints"));
63         String not_connected_endpoints = FormatArray(status->Get("not_conn_endpoints"));
64
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.");
69         } else {
70                 cr->SetState(ServiceOK);
71                 cr->SetOutput("Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(status->Get("num_conn_endpoints")) +
72                     " (" + connected_endpoints + ").");
73         }
74
75         checkable->ProcessCheckResult(cr);
76 }
77
78 String ClusterCheckTask::FormatArray(const Array::Ptr& arr)
79 {
80         bool first = true;
81         String str;
82
83         if (arr) {
84                 ObjectLock olock(arr);
85                 BOOST_FOREACH(const Value& value, arr) {
86                         if (first)
87                                 first = false;
88                         else
89                                 str += ", ";
90
91                         str += Convert::ToString(value);
92                 }
93         }
94
95         return str;
96 }