icinga2/components/delegation/delegationcomponent.cpp

207 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. *
******************************************************************************/
#include "i2-delegation.h"
2012-06-20 16:52:56 +02:00
#include <algorithm>
2012-06-14 13:21:40 +02:00
using namespace icinga;
string DelegationComponent::GetName(void) const
{
return "delegation";
}
void DelegationComponent::Start(void)
{
2012-06-15 19:32:41 +02:00
m_AllServices = boost::make_shared<ConfigObject::Set>(ConfigObject::GetAllObjects(), ConfigObject::MakeTypePredicate("service"));
2012-06-16 03:42:54 +02:00
m_AllServices->OnObjectAdded.connect(boost::bind(&DelegationComponent::NewServiceHandler, this, _2));
m_AllServices->OnObjectCommitted.connect(boost::bind(&DelegationComponent::NewServiceHandler, this, _2));
m_AllServices->OnObjectRemoved.connect(boost::bind(&DelegationComponent::RemovedServiceHandler, this, _2));
2012-06-14 13:21:40 +02:00
m_AllServices->Start();
2012-06-15 19:32:41 +02:00
m_DelegationTimer = boost::make_shared<Timer>();
2012-06-14 16:31:38 +02: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();
2012-06-15 19:32:41 +02:00
m_DelegationEndpoint = boost::make_shared<VirtualEndpoint>();
2012-06-14 13:21:40 +02:00
m_DelegationEndpoint->RegisterPublication("checker::AssignService");
m_DelegationEndpoint->RegisterPublication("checker::RevokeService");
GetEndpointManager()->RegisterEndpoint(m_DelegationEndpoint);
}
void DelegationComponent::Stop(void)
{
EndpointManager::Ptr mgr = GetEndpointManager();
if (mgr)
mgr->UnregisterEndpoint(m_DelegationEndpoint);
}
void DelegationComponent::NewServiceHandler(const Service& object)
2012-06-14 13:21:40 +02:00
{
2012-06-16 03:42:54 +02:00
AssignService(object);
2012-06-14 13:21:40 +02:00
}
void DelegationComponent::RemovedServiceHandler(const Service& object)
2012-06-14 13:21:40 +02:00
{
2012-06-16 03:42:54 +02:00
RevokeService(object);
2012-06-14 13:21:40 +02:00
}
void DelegationComponent::AssignService(const Service& service)
2012-06-14 13:21:40 +02:00
{
RequestMessage request;
request.SetMethod("checker::AssignService");
MessagePart params;
params.SetProperty("service", service.GetConfigObject()->GetProperties());
2012-06-14 13:21:40 +02:00
request.SetParams(params);
2012-06-20 10:46:18 +02:00
Application::Log(LogDebug, "delegation", "Trying to delegate service '" + service.GetName() + "'");
2012-06-14 16:09:04 +02:00
2012-06-14 13:21:40 +02:00
GetEndpointManager()->SendAPIMessage(m_DelegationEndpoint, request,
2012-06-16 03:42:54 +02:00
boost::bind(&DelegationComponent::AssignServiceResponseHandler, this, service, _2, _5));
2012-06-14 13:21:40 +02:00
}
void DelegationComponent::AssignServiceResponseHandler(Service& service, const Endpoint::Ptr& sender, bool timedOut)
2012-06-14 13:21:40 +02:00
{
2012-06-16 03:42:54 +02:00
if (timedOut) {
2012-06-20 10:46:18 +02:00
Application::Log(LogDebug, "delegation", "Service delegation for service '" + service.GetName() + "' timed out.");
2012-06-14 16:39:14 +02:00
} else {
service.SetChecker(sender->GetIdentity());
2012-06-20 10:46:18 +02:00
Application::Log(LogDebug, "delegation", "Service delegation for service '" + service.GetName() + "' was successful.");
2012-06-14 16:39:14 +02:00
}
2012-06-14 13:21:40 +02:00
}
void DelegationComponent::RevokeService(const Service& service)
2012-06-14 13:21:40 +02:00
{
}
void DelegationComponent::RevokeServiceResponseHandler(Service& service, const Endpoint::Ptr& sender, bool timedOut)
2012-06-14 13:21:40 +02:00
{
}
2012-06-20 16:52:56 +02:00
vector<Endpoint::Ptr> DelegationComponent::GetCheckerCandidates(const Service& service) const
{
vector<Endpoint::Ptr> candidates;
EndpointManager::Iterator it;
for (it = GetEndpointManager()->Begin(); it != GetEndpointManager()->End(); it++)
candidates.push_back(it->second);
return candidates;
}
2012-06-15 19:32:41 +02:00
void DelegationComponent::DelegationTimerHandler(void)
2012-06-14 16:31:38 +02:00
{
2012-06-20 16:52:56 +02:00
map<Endpoint::Ptr, int> histogram;
EndpointManager::Iterator eit;
for (eit = GetEndpointManager()->Begin(); eit != GetEndpointManager()->End(); eit++) {
histogram[eit->second] = 0;
}
/* nothing to do if we have no checkers */
if (histogram.size() == 0)
return;
vector<Service> services;
/* build "checker -> service count" histogram */
2012-06-14 16:31:38 +02:00
ConfigObject::Set::Iterator it;
for (it = m_AllServices->Begin(); it != m_AllServices->End(); it++) {
Service service = *it;
2012-06-14 16:31:38 +02:00
2012-06-20 16:52:56 +02:00
services.push_back(service);
string checker = service.GetChecker();
if (checker.empty())
continue;
Endpoint::Ptr endpoint = GetEndpointManager()->GetEndpointByIdentity(checker);
if (!endpoint)
continue;
histogram[endpoint]++;
}
std::random_shuffle(services.begin(), services.end());
long delegated = 0;
/* re-assign services */
vector<Service>::iterator sit;
for (sit = services.begin(); sit != services.end(); it++) {
Service service = *sit;
string checker = service.GetChecker();
2012-06-20 16:52:56 +02:00
Endpoint::Ptr oldEndpoint;
if (!checker.empty())
oldEndpoint = GetEndpointManager()->GetEndpointByIdentity(checker);
vector<Endpoint::Ptr> candidates = GetCheckerCandidates(service);
long avg_services = 0;
vector<Endpoint::Ptr>::iterator cit;
for (cit = candidates.begin(); cit != candidates.end(); cit++)
avg_services += histogram[*cit];
avg_services /= candidates.size();
long overflow_tolerance = candidates.size() * 2;
/* don't re-assign service if the checker is still valid
* and doesn't have too many services */
if (oldEndpoint && find(candidates.begin(), candidates.end(), oldEndpoint) != candidates.end() &&
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.empty()) {
service.SetChecker("");
if (oldEndpoint)
histogram[oldEndpoint]--;
}
/* find a new checker for the service */
for (cit = candidates.begin(); cit != candidates.end(); cit++) {
Endpoint::Ptr newEndpoint = *cit;
/* does this checker already have too many services */
if (histogram[newEndpoint] > avg_services)
continue;
service.SetChecker(newEndpoint->GetIdentity());
histogram[newEndpoint]++;
delegated++;
}
assert(!service.GetChecker().empty());
2012-06-14 16:31:38 +02:00
}
2012-06-19 12:23:52 +02:00
2012-06-20 10:46:18 +02:00
stringstream msgbuf;
2012-06-20 16:52:56 +02:00
msgbuf << "Re-delegated " << delegated << " services";
2012-06-20 10:46:18 +02:00
Application::Log(LogInformation, "delegation", msgbuf.str());
2012-06-14 13:21:40 +02:00
}
EXPORT_COMPONENT(delegation, DelegationComponent);