icinga2/components/delegation/delegationcomponent.cpp

222 lines
6.8 KiB
C++
Raw Normal View History

2012-06-14 13:21:40 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "delegation/delegationcomponent.h"
#include "remoting/endpointmanager.h"
2013-03-16 21:18:53 +01:00
#include "base/objectlock.h"
#include "base/logger_fwd.h"
2012-06-20 16:52:56 +02:00
#include <algorithm>
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include <boost/foreach.hpp>
2013-03-18 11:02:18 +01:00
#include <boost/tuple/tuple.hpp>
2012-06-14 13:21:40 +02:00
using namespace icinga;
REGISTER_TYPE(DelegationComponent);
DelegationComponent::DelegationComponent(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
{ }
2012-06-14 13:21:40 +02:00
void DelegationComponent::Start(void)
{
2012-06-15 19:32:41 +02:00
m_DelegationTimer = boost::make_shared<Timer>();
2013-03-01 12:07:52 +01:00
m_DelegationTimer->SetInterval(30);
2012-06-15 19:32:41 +02:00
m_DelegationTimer->OnTimerExpired.connect(boost::bind(&DelegationComponent::DelegationTimerHandler, this));
2012-06-14 16:31:38 +02:00
m_DelegationTimer->Start();
m_DelegationTimer->Reschedule(Utility::GetTime() + 10);
2012-06-27 18:43:34 +02:00
}
bool DelegationComponent::IsEndpointChecker(const Endpoint::Ptr& endpoint)
{
2012-08-14 10:53:04 +02:00
return (endpoint->HasSubscription("checker"));
2012-06-14 13:21:40 +02:00
}
2013-03-16 21:18:53 +01:00
std::set<Endpoint::Ptr> DelegationComponent::GetCheckerCandidates(const Service::Ptr& service) const
2012-06-20 16:52:56 +02:00
{
2013-03-16 21:18:53 +01:00
std::set<Endpoint::Ptr> candidates;
2013-02-18 14:40:24 +01:00
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object);
String myIdentity = EndpointManager::GetInstance()->GetIdentity();
/* ignore local-only endpoints (unless this is a local-only instance) */
if (endpoint->IsLocal() && !myIdentity.IsEmpty())
continue;
/* ignore disconnected endpoints */
if (!endpoint->IsConnected() && endpoint->GetName() != myIdentity)
continue;
/* ignore endpoints that aren't running the checker component */
2012-06-27 18:43:34 +02:00
if (!IsEndpointChecker(endpoint))
continue;
/* ignore endpoints that aren't allowed to check this service */
if (!service->IsAllowedChecker(endpoint->GetName()))
2012-06-21 00:10:10 +02:00
continue;
2013-02-18 14:40:24 +01:00
candidates.insert(endpoint);
2012-06-21 00:10:10 +02:00
}
2012-06-20 16:52:56 +02:00
return candidates;
}
2012-06-15 19:32:41 +02:00
void DelegationComponent::DelegationTimerHandler(void)
2012-06-14 16:31:38 +02:00
{
2013-03-16 21:18:53 +01:00
std::map<Endpoint::Ptr, int> histogram;
2012-06-20 16:52:56 +02:00
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object);
2013-02-18 23:44:24 +01:00
histogram[endpoint] = 0;
}
2012-06-20 16:52:56 +02:00
2013-03-16 21:18:53 +01:00
std::vector<Service::Ptr> services;
2012-06-20 16:52:56 +02:00
2013-02-18 23:44:24 +01:00
/* build "checker -> service count" histogram */
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
2012-06-14 16:31:38 +02:00
2013-02-18 23:44:24 +01:00
if (!service)
continue;
2012-06-20 16:52:56 +02:00
2013-02-18 23:44:24 +01:00
services.push_back(service);
2012-06-20 16:52:56 +02:00
2013-02-26 10:13:54 +01:00
String checker = service->GetCurrentChecker();
2013-03-02 09:07:47 +01:00
2013-02-18 23:44:24 +01:00
if (checker.IsEmpty())
continue;
2013-02-18 14:40:24 +01:00
2013-02-18 23:44:24 +01:00
Endpoint::Ptr endpoint = Endpoint::GetByName(checker);
2013-02-27 15:23:25 +01:00
if (!endpoint)
continue;
2013-02-18 23:44:24 +01:00
histogram[endpoint]++;
2012-06-20 16:52:56 +02:00
}
2012-06-21 00:10:10 +02:00
int delegated = 0;
2012-06-20 16:52:56 +02:00
/* re-assign services */
BOOST_FOREACH(const Service::Ptr& service, services) {
2013-02-26 10:13:54 +01:00
String checker = service->GetCurrentChecker();
2012-06-20 16:52:56 +02:00
2013-02-27 15:23:25 +01:00
Endpoint::Ptr oldEndpoint = Endpoint::GetByName(checker);
2012-06-20 16:52:56 +02:00
2013-03-16 21:18:53 +01:00
std::set<Endpoint::Ptr> candidates = GetCheckerCandidates(service);
2012-06-20 16:52:56 +02:00
2012-06-21 12:51:50 +02:00
int avg_services = 0, overflow_tolerance = 0;
2013-03-16 21:18:53 +01:00
std::vector<Endpoint::Ptr>::iterator cit;
2012-06-20 16:52:56 +02:00
2013-03-06 15:41:13 +01:00
if (!candidates.empty()) {
2013-03-27 19:02:51 +01:00
#ifdef _DEBUG
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
msgbuf << "Service: " << service->GetName() << ", candidates: " << candidates.size();
2013-03-16 21:18:53 +01:00
Log(LogDebug, "delegation", msgbuf.str());
2013-03-27 19:02:51 +01:00
#endif /* _DEBUG */
2012-06-21 12:51:50 +02:00
2012-07-16 22:00:50 +02:00
BOOST_FOREACH(const Endpoint::Ptr& candidate, candidates) {
avg_services += histogram[candidate];
}
2012-06-21 12:51:50 +02:00
avg_services /= candidates.size();
overflow_tolerance = candidates.size() * 2;
}
2012-06-20 16:52:56 +02:00
/* don't re-assign service if the checker is still valid
* and doesn't have too many services */
2013-02-18 14:40:24 +01:00
2012-06-22 08:30:36 +02:00
if (oldEndpoint && oldEndpoint->IsConnected() &&
2013-02-18 14:40:24 +01:00
candidates.find(oldEndpoint) != candidates.end() &&
2012-06-20 16:52:56 +02:00
histogram[oldEndpoint] <= avg_services + overflow_tolerance)
2012-06-14 16:31:38 +02:00
continue;
2012-06-20 16:52:56 +02:00
/* clear the service's current checker */
if (!checker.IsEmpty()) {
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(service);
service->SetCurrentChecker("");
}
2012-06-20 16:52:56 +02:00
if (oldEndpoint)
histogram[oldEndpoint]--;
}
/* find a new checker for the service */
2012-07-16 22:00:50 +02:00
BOOST_FOREACH(const Endpoint::Ptr& candidate, candidates) {
2012-06-20 16:52:56 +02:00
/* does this checker already have too many services */
2012-07-16 22:00:50 +02:00
if (histogram[candidate] > avg_services)
2012-06-20 16:52:56 +02:00
continue;
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(service);
service->SetCurrentChecker(candidate->GetName());
}
2012-07-16 22:00:50 +02:00
histogram[candidate]++;
2012-06-20 16:52:56 +02:00
/* reschedule the service; this avoids "check floods"
* when a lot of services are re-assigned that haven't
* been checked recently. */
service->UpdateNextCheck();
2012-06-20 16:52:56 +02:00
delegated++;
2012-06-21 13:12:16 +02:00
break;
2012-06-20 16:52:56 +02:00
}
2013-03-06 15:41:13 +01:00
if (candidates.empty()) {
if (service->GetState() != StateUncheckable && service->GetEnableActiveChecks()) {
Dictionary::Ptr cr = boost::make_shared<Dictionary>();
double now = Utility::GetTime();
cr->Set("schedule_start", now);
cr->Set("schedule_end", now);
cr->Set("execution_start", now);
cr->Set("execution_end", now);
cr->Set("state", StateUncheckable);
cr->Set("output", "No checker is available for this service.");
service->ProcessCheckResult(cr);
2013-03-16 21:18:53 +01:00
Log(LogWarning, "delegation", "Can't delegate service: " + service->GetName());
}
continue;
}
ASSERT(!service->GetCurrentChecker().IsEmpty());
2012-06-14 16:31:38 +02:00
}
2012-06-19 12:23:52 +02:00
2012-07-16 22:00:50 +02:00
Endpoint::Ptr endpoint;
int count;
2013-03-18 11:02:18 +01:00
BOOST_FOREACH(boost::tie(endpoint, count), histogram) {
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
msgbuf << "histogram: " << endpoint->GetName() << " - " << count;
2013-03-16 21:18:53 +01:00
Log(LogInformation, "delegation", msgbuf.str());
}
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
2012-06-21 17:39:16 +02:00
msgbuf << "Updated delegations for " << delegated << " services";
2013-03-16 21:18:53 +01:00
Log(LogInformation, "delegation", msgbuf.str());
2012-06-14 13:21:40 +02:00
}