2014-05-25 17:10:57 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-05-25 17:10:57 +02: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 "methods/clusterzonechecktask.hpp"
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
2015-02-24 09:14:17 +01:00
|
|
|
#include "icinga/perfdatavalue.hpp"
|
2014-05-25 17:10:57 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "remote/zone.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2015-02-24 09:27:37 +01:00
|
|
|
#include "base/utility.hpp"
|
2014-05-25 17:10:57 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_SCRIPTFUNCTION(ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc);
|
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2014-05-25 17:10:57 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
|
|
|
|
Value raw_command = commandObj->GetCommandLine();
|
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
MacroProcessor::ResolverList resolvers;
|
|
|
|
if (service)
|
|
|
|
resolvers.push_back(std::make_pair("service", service));
|
|
|
|
resolvers.push_back(std::make_pair("host", host));
|
|
|
|
resolvers.push_back(std::make_pair("command", commandObj));
|
|
|
|
resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
|
|
|
|
|
2014-12-01 13:19:07 +01:00
|
|
|
String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
|
|
|
|
NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
|
|
|
|
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
if (zoneName.IsEmpty()) {
|
|
|
|
cr->SetOutput("Macro 'cluster_zone' must be set.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Zone::Ptr zone = Zone::GetByName(zoneName);
|
|
|
|
|
|
|
|
if (!zone) {
|
|
|
|
cr->SetOutput("Zone '" + zoneName + "' does not exist.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool connected = false;
|
2015-02-24 09:14:17 +01:00
|
|
|
double lag = 0;
|
2014-05-25 17:10:57 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) {
|
2015-02-24 09:14:17 +01:00
|
|
|
double eplag = Utility::GetTime() - endpoint->GetRemoteLogPosition();
|
|
|
|
|
2015-03-03 14:40:05 +01:00
|
|
|
if (endpoint->IsConnected())
|
2014-05-25 17:10:57 +02:00
|
|
|
connected = true;
|
2015-03-03 14:40:05 +01:00
|
|
|
|
|
|
|
if ((endpoint->GetSyncing() || !endpoint->IsConnected()) && eplag > lag)
|
|
|
|
lag = eplag;
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
cr->SetState(ServiceCritical);
|
2015-02-24 09:27:37 +01:00
|
|
|
cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(lag));
|
2014-05-25 17:10:57 +02:00
|
|
|
} else {
|
|
|
|
cr->SetState(ServiceOK);
|
2015-02-24 09:27:37 +01:00
|
|
|
cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(lag));
|
2014-05-25 17:10:57 +02:00
|
|
|
}
|
|
|
|
|
2015-02-24 09:14:17 +01:00
|
|
|
Array::Ptr perfdata = new Array();
|
|
|
|
perfdata->Add(new PerfdataValue("slave_lag", lag));
|
|
|
|
cr->SetPerformanceData(perfdata);
|
|
|
|
|
2014-05-25 17:10:57 +02:00
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
}
|