2014-02-07 14:03:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2014-02-07 14:03:53 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02: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"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-02-07 14:03:53 +01:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2017-03-27 14:26:56 +02:00
|
|
|
REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
|
2014-02-07 14:03:53 +01:00
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2014-02-07 14:03:53 +01:00
|
|
|
{
|
2014-12-01 13:19:07 +01:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
|
|
|
|
2014-05-03 20:02:22 +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;
|
2014-02-07 14:03:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
std::pair<Dictionary::Ptr, Dictionary::Ptr> stats = listener->GetStatus();
|
|
|
|
|
2014-02-18 10:53:44 +01:00
|
|
|
Dictionary::Ptr status = stats.first;
|
|
|
|
|
|
|
|
/* use feature stats perfdata */
|
2014-09-17 15:38:39 +02:00
|
|
|
std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
|
2014-05-03 20:02:22 +02:00
|
|
|
cr->SetPerformanceData(feature_stats.second);
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-02-13 19:23:38 +01:00
|
|
|
String connected_endpoints = FormatArray(status->Get("conn_endpoints"));
|
|
|
|
String not_connected_endpoints = FormatArray(status->Get("not_conn_endpoints"));
|
|
|
|
|
|
|
|
if (status->Get("num_not_conn_endpoints") > 0) {
|
2014-05-03 20:02:22 +02:00
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
cr->SetOutput("Icinga 2 Cluster Problem: " + Convert::ToString(status->Get("num_not_conn_endpoints")) +
|
2017-12-19 15:50:05 +01:00
|
|
|
" Endpoints (" + not_connected_endpoints + ") not connected.");
|
2014-05-20 12:28:50 +02:00
|
|
|
} else {
|
|
|
|
cr->SetState(ServiceOK);
|
|
|
|
cr->SetOutput("Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(status->Get("num_conn_endpoints")) +
|
2017-12-19 15:50:05 +01:00
|
|
|
" (" + connected_endpoints + ").");
|
2014-02-07 14:03:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
checkable->ProcessCheckResult(cr);
|
2014-02-07 14:03:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:23:38 +01:00
|
|
|
String ClusterCheckTask::FormatArray(const Array::Ptr& arr)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
String str;
|
|
|
|
|
|
|
|
if (arr) {
|
|
|
|
ObjectLock olock(arr);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& value : arr) {
|
2014-02-13 19:23:38 +01:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
2014-05-26 09:39:14 +02:00
|
|
|
str += ", ";
|
2014-02-13 19:23:38 +01:00
|
|
|
|
|
|
|
str += Convert::ToString(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|