icinga2/components/delegation/delegationcomponent.cpp

115 lines
4.4 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"
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);
Application::Log(LogInformation, "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) {
Application::Log(LogInformation, "delegation", "Service delegation for service '" + service.GetName() + "' timed out.");
2012-06-14 16:39:14 +02:00
} else {
service.SetChecker(sender->GetIdentity());
Application::Log(LogInformation, "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-15 19:32:41 +02:00
void DelegationComponent::DelegationTimerHandler(void)
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
string checker = service.GetChecker();
if (!checker.empty() && GetEndpointManager()->GetEndpointByIdentity(checker))
2012-06-14 16:31:38 +02:00
continue;
AssignService(service);
2012-06-14 16:31:38 +02:00
}
2012-06-14 13:21:40 +02:00
}
EXPORT_COMPONENT(delegation, DelegationComponent);