2012-06-14 11:23:25 +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-checker.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
string CheckerComponent::GetName(void) const
|
|
|
|
{
|
2012-06-14 13:21:40 +02:00
|
|
|
return "checker";
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckerEndpoint = boost::make_shared<VirtualEndpoint>();
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckerEndpoint->RegisterTopicHandler("checker::AssignService",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&CheckerComponent::AssignServiceRequestHandler, this, _2, _3));
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckerEndpoint->RegisterTopicHandler("checker::RevokeService",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&CheckerComponent::RevokeServiceRequestHandler, this, _2, _3));
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckerEndpoint->RegisterTopicHandler("checker::ClearServices",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&CheckerComponent::ClearServicesRequestHandler, this, _2, _3));
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckerEndpoint->RegisterPublication("checker::CheckResult");
|
|
|
|
GetEndpointManager()->RegisterEndpoint(m_CheckerEndpoint);
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckTimer = boost::make_shared<Timer>();
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckTimer->SetInterval(10);
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckTimer->Start();
|
|
|
|
|
|
|
|
CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask);
|
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
m_ResultTimer = boost::make_shared<Timer>();
|
2012-06-17 23:10:03 +02:00
|
|
|
m_ResultTimer->SetInterval(5);
|
2012-06-17 20:35:56 +02:00
|
|
|
m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
|
|
|
|
m_ResultTimer->Start();
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Stop(void)
|
|
|
|
{
|
2012-06-14 13:21:40 +02:00
|
|
|
EndpointManager::Ptr mgr = GetEndpointManager();
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2012-06-14 13:21:40 +02:00
|
|
|
if (mgr)
|
|
|
|
mgr->UnregisterEndpoint(m_CheckerEndpoint);
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
void CheckerComponent::CheckTimerHandler(void)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
for (;;) {
|
2012-06-18 09:44:12 +02:00
|
|
|
if (m_Services.empty())
|
2012-06-18 00:14:34 +02:00
|
|
|
break;
|
|
|
|
|
2012-06-14 11:23:25 +02:00
|
|
|
Service service = m_Services.top();
|
|
|
|
|
2012-06-17 22:46:40 +02:00
|
|
|
if (service.GetNextCheck() > now || service.HasPendingCheck())
|
2012-06-14 11:23:25 +02:00
|
|
|
break;
|
|
|
|
|
2012-06-17 22:46:40 +02:00
|
|
|
m_Services.pop();
|
|
|
|
service.SetPendingCheck(true);
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogInformation, "checker", "Executing service check for '" + service.GetName() + "'");
|
2012-06-17 20:35:56 +02:00
|
|
|
|
|
|
|
CheckTask::Ptr task = CheckTask::CreateTask(service);
|
|
|
|
task->Execute();
|
|
|
|
m_PendingTasks.push_back(task);
|
2012-06-14 11:23:25 +02:00
|
|
|
|
|
|
|
service.SetNextCheck(now + service.GetCheckInterval());
|
|
|
|
}
|
|
|
|
|
2012-06-17 22:46:40 +02:00
|
|
|
AdjustCheckTimer();
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
void CheckerComponent::ResultTimerHandler(void)
|
|
|
|
{
|
|
|
|
vector<CheckTask::Ptr> unfinishedTasks;
|
|
|
|
|
|
|
|
for (vector<CheckTask::Ptr>::iterator it = m_PendingTasks.begin(); it != m_PendingTasks.end(); it++) {
|
|
|
|
CheckTask::Ptr task = *it;
|
|
|
|
|
|
|
|
if (!task->IsFinished()) {
|
|
|
|
unfinishedTasks.push_back(task);
|
2012-06-17 21:54:09 +02:00
|
|
|
continue;
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-18 00:14:34 +02:00
|
|
|
Service service = task->GetService();
|
|
|
|
service.SetPendingCheck(false);
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
CheckResult result = task->GetResult();
|
|
|
|
Application::Log(LogInformation, "checker", "Got result! Plugin output: " + result.Output);
|
2012-06-18 00:14:34 +02:00
|
|
|
|
|
|
|
m_Services.push(service);
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_PendingTasks = unfinishedTasks;
|
2012-06-17 22:46:40 +02:00
|
|
|
|
|
|
|
AdjustCheckTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::AdjustCheckTimer(void)
|
|
|
|
{
|
2012-06-18 09:44:12 +02:00
|
|
|
if (m_Services.empty())
|
2012-06-17 22:46:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* adjust next call time for the check timer */
|
|
|
|
Service service = m_Services.top();
|
2012-06-18 01:58:13 +02:00
|
|
|
m_CheckTimer->Reschedule(service.GetNextCheck());
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void CheckerComponent::AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-06-14 16:09:04 +02:00
|
|
|
MessagePart params;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (!request.GetParams(¶ms))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
|
|
|
MessagePart serviceMsg;
|
|
|
|
if (!params.GetProperty("service", &serviceMsg))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
ConfigObject::Ptr object = boost::make_shared<ConfigObject>(serviceMsg.GetDictionary());
|
2012-06-14 16:09:04 +02:00
|
|
|
Service service(object);
|
|
|
|
m_Services.push(service);
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogInformation, "checker", "Accepted delegation for service '" + service.GetName() + "'");
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-06-14 16:39:14 +02:00
|
|
|
/* force a service check */
|
|
|
|
m_CheckTimer->Reschedule(0);
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
string id;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (request.GetID(&id)) {
|
2012-06-15 19:32:41 +02:00
|
|
|
ResponseMessage rm;
|
|
|
|
rm.SetID(id);
|
|
|
|
|
|
|
|
MessagePart result;
|
|
|
|
rm.SetResult(result);
|
2012-06-16 03:42:54 +02:00
|
|
|
GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, sender, rm);
|
2012-06-15 19:32:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void CheckerComponent::RevokeServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-06-15 19:32:41 +02:00
|
|
|
{
|
|
|
|
MessagePart params;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (!request.GetParams(¶ms))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
string name;
|
|
|
|
if (!params.GetProperty("service", &name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
vector<Service> services;
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
while (!m_Services.empty()) {
|
|
|
|
Service service = m_Services.top();
|
|
|
|
|
|
|
|
if (service.GetName() == name)
|
|
|
|
continue;
|
|
|
|
|
2012-06-18 00:14:34 +02:00
|
|
|
if (service.HasPendingCheck()) // TODO: remember services that should be removed once their pending check is done
|
|
|
|
throw runtime_error("not yet implemented");
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
services.push_back(service);
|
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
vector<Service>::const_iterator it;
|
|
|
|
for (it = services.begin(); it != services.end(); it++)
|
|
|
|
m_Services.push(*it);
|
|
|
|
|
|
|
|
Application::Log(LogInformation, "checker", "Revoked delegation for service '" + name + "'");
|
|
|
|
|
|
|
|
string id;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (request.GetID(&id)) {
|
2012-06-15 19:32:41 +02:00
|
|
|
ResponseMessage rm;
|
|
|
|
rm.SetID(id);
|
|
|
|
|
|
|
|
MessagePart result;
|
|
|
|
rm.SetResult(result);
|
2012-06-16 03:42:54 +02:00
|
|
|
GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, sender, rm);
|
2012-06-15 19:32:41 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void CheckerComponent::ClearServicesRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogInformation, "checker", "Clearing service delegations.");
|
|
|
|
m_Services = ServiceQueue();
|
|
|
|
|
|
|
|
string id;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (request.GetID(&id)) {
|
2012-06-15 19:32:41 +02:00
|
|
|
ResponseMessage rm;
|
|
|
|
rm.SetID(id);
|
|
|
|
|
|
|
|
MessagePart result;
|
|
|
|
rm.SetResult(result);
|
2012-06-16 03:42:54 +02:00
|
|
|
GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, sender, rm);
|
2012-06-15 19:32:41 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(checker, CheckerComponent);
|