icinga2/components/checker/checkercomponent.cpp

222 lines
6.8 KiB
C++
Raw Normal View History

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-19 12:23:52 +02:00
m_CheckTimer->SetInterval(5);
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();
2012-06-19 09:38:20 +02:00
CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask, NagiosCheckTask::FlushQueue, NagiosCheckTask::GetFinishedTasks);
2012-06-14 11:23:25 +02:00
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);
Application::Log(LogDebug, "checker", "CheckTimerHandler entered.");
long tasks = 0;
2012-06-19 09:38:20 +02:00
while (!m_Services.empty()) {
2012-06-14 11:23:25 +02:00
Service service = m_Services.top();
2012-06-19 09:38:20 +02:00
if (service.GetNextCheck() > now)
2012-06-14 11:23:25 +02:00
break;
m_Services.pop();
// 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->Enqueue();
tasks++;
2012-06-14 11:23:25 +02:00
}
Application::Log(LogDebug, "checker", "CheckTimerHandler: past loop.");
CheckTask::FlushQueue();
stringstream msgbuf;
msgbuf << "CheckTimerHandler: created " << tasks << " tasks";
Application::Log(LogDebug, "checker", msgbuf.str());
2012-06-14 11:23:25 +02:00
}
2012-06-17 20:35:56 +02:00
void CheckerComponent::ResultTimerHandler(void)
{
Application::Log(LogDebug, "checker", "ResultTimerHandler entered.");
2012-06-19 09:38:20 +02:00
time_t now;
time(&now);
long min_latency = -1, max_latency = 0, avg_latency = 0, results = 0;
2012-06-19 09:38:20 +02:00
vector<CheckTask::Ptr> finishedTasks = CheckTask::GetFinishedTasks();
2012-06-17 20:35:56 +02:00
2012-06-19 09:38:20 +02:00
for (vector<CheckTask::Ptr>::iterator it = finishedTasks.begin(); it != finishedTasks.end(); it++) {
CheckTask::Ptr task = *it;
2012-06-17 20:35:56 +02:00
2012-06-18 00:14:34 +02:00
Service service = task->GetService();
2012-06-17 20:35:56 +02:00
CheckResult result = task->GetResult();
// Application::Log(LogInformation, "checker", "Got result! Plugin output: " + result.Output);
long latency = result.EndTime - result.StartTime;
avg_latency += latency;
if (min_latency == -1 || latency < min_latency)
min_latency = latency;
if (latency > max_latency)
max_latency = latency;
results++;
2012-06-18 00:14:34 +02:00
2012-06-19 09:38:20 +02:00
service.SetNextCheck(now + service.GetCheckInterval());
2012-06-18 00:14:34 +02:00
m_Services.push(service);
2012-06-17 20:35:56 +02:00
}
stringstream msgbuf;
msgbuf << "ResultTimerHandler: " << results << " results; latency: avg=" << avg_latency / (results ? results : 1) << ", min=" << min_latency << ", max: " << max_latency;
Application::Log(LogDebug, "checker", msgbuf.str());
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(&params))
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(&params))
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-19 09:38:20 +02:00
// TODO: take care of services that are currently being checked
2012-06-18 00:14:34 +02:00
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);