icinga2/components/cluster/clusterchecktask.cpp

92 lines
3.5 KiB
C++
Raw Normal View History

2014-02-07 14:03:53 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
******************************************************************************/
#include "cluster/clusterchecktask.h"
2014-02-07 14:03:53 +01:00
#include "cluster/clusterlistener.h"
#include "remote/endpoint.h"
2014-02-07 14:03:53 +01:00
#include "icinga/cib.h"
#include "icinga/service.h"
#include "icinga/icingaapplication.h"
#include "base/application.h"
#include "base/objectlock.h"
#include "base/convert.h"
#include "base/utility.h"
#include "base/scriptfunction.h"
#include "base/dynamictype.h"
#include <boost/algorithm/string/join.hpp>
using namespace icinga;
REGISTER_SCRIPTFUNCTION(ClusterCheck, &ClusterCheckTask::ScriptFunc);
void ClusterCheckTask::ScriptFunc(const Service::Ptr& service, const CheckResult::Ptr& cr)
2014-02-07 14:03:53 +01:00
{
/* fetch specific cluster status */
std::pair<Dictionary::Ptr, Dictionary::Ptr> stats;
2014-02-07 14:03:53 +01:00
BOOST_FOREACH(const ClusterListener::Ptr& cluster_listener, DynamicType::GetObjects<ClusterListener>()) {
/* XXX there's only one cluster listener */
stats = cluster_listener->GetClusterStatus();
2014-02-07 14:03:53 +01:00
}
Dictionary::Ptr status = stats.first;
/* use feature stats perfdata */
std::pair<Dictionary::Ptr, Dictionary::Ptr> feature_stats = CIB::GetFeatureStats();
Dictionary::Ptr perfdata = feature_stats.second;
String connected_endpoints = FormatArray(status->Get("conn_endpoints"));
String not_connected_endpoints = FormatArray(status->Get("not_conn_endpoints"));
2014-02-07 14:03:53 +01:00
ServiceState state = StateOK;
String output = "Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(status->Get("num_conn_endpoints")) + " (" +
connected_endpoints + ").";
2014-02-07 14:03:53 +01:00
if (status->Get("num_not_conn_endpoints") > 0) {
2014-02-07 14:03:53 +01:00
state = StateCritical;
output = "Icinga 2 Cluster Problem: " + Convert::ToString(status->Get("num_not_conn_endpoints")) +
" Endpoints (" + not_connected_endpoints + ") not connected.";
2014-02-07 14:03:53 +01:00
}
cr->SetOutput(output);
cr->SetPerformanceData(perfdata);
2014-02-07 14:03:53 +01:00
cr->SetState(state);
service->ProcessCheckResult(cr);
2014-02-07 14:03:53 +01:00
}
String ClusterCheckTask::FormatArray(const Array::Ptr& arr)
{
bool first = true;
String str;
if (arr) {
ObjectLock olock(arr);
BOOST_FOREACH(const Value& value, arr) {
if (first)
first = false;
else
str += ",";
str += Convert::ToString(value);
}
}
return str;
}