icinga2/components/checker/checkercomponent.cpp

180 lines
6.2 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;
void CheckerComponent::Start(void)
{
2012-07-02 11:07:54 +02:00
m_Endpoint = boost::make_shared<VirtualEndpoint>();
m_Endpoint->RegisterTopicHandler("checker::AssignService",
2012-06-16 03:42:54 +02:00
boost::bind(&CheckerComponent::AssignServiceRequestHandler, this, _2, _3));
2012-07-02 11:07:54 +02:00
m_Endpoint->RegisterTopicHandler("checker::ClearServices",
2012-06-16 03:42:54 +02:00
boost::bind(&CheckerComponent::ClearServicesRequestHandler, this, _2, _3));
2012-07-02 11:07:54 +02:00
m_Endpoint->RegisterPublication("checker::CheckResult");
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
2012-06-14 11:23:25 +02:00
2012-06-15 19:32:41 +02:00
m_CheckTimer = boost::make_shared<Timer>();
m_CheckTimer->SetInterval(1);
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();
NagiosCheckTask::Register();
NullCheckTask::Register();
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-27 18:43:34 +02:00
EndpointManager::Ptr mgr = EndpointManager::GetInstance();
2012-06-14 11:23:25 +02:00
2012-06-14 13:21:40 +02:00
if (mgr)
2012-07-02 11:07:54 +02:00
mgr->UnregisterEndpoint(m_Endpoint);
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
{
2012-07-10 12:21:19 +02:00
Logger::Write(LogDebug, "checker", "CheckTimerHandler entered.");
double now = Utility::GetTime();
long tasks = 0;
2012-06-19 09:38:20 +02:00
while (!m_Services.empty()) {
Service::Ptr service = m_Services.top();
2012-06-14 11:23:25 +02:00
if (service->GetNextCheck() > now)
2012-06-14 11:23:25 +02:00
break;
m_Services.pop();
Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
2012-06-17 20:35:56 +02:00
m_PendingServices.insert(service);
2012-07-14 15:59:59 +02:00
vector<Variant> arguments;
arguments.push_back(service);
2012-07-14 15:59:59 +02:00
ScriptTask::Ptr task;
task = service->InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, _1));
2012-07-14 15:59:59 +02:00
assert(task); /* TODO: gracefully handle missing hooks */
service->SetTag("current_task", task);
tasks++;
2012-06-14 11:23:25 +02:00
}
2012-07-10 12:21:19 +02:00
Logger::Write(LogDebug, "checker", "CheckTimerHandler: past loop.");
stringstream msgbuf;
msgbuf << "CheckTimerHandler: created " << tasks << " tasks";
2012-07-10 12:21:19 +02:00
Logger::Write(LogInformation, "checker", msgbuf.str());
2012-06-14 11:23:25 +02:00
}
void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service, const ScriptTask::Ptr& task)
2012-06-17 20:35:56 +02:00
{
service->RemoveTag("current_task");
2012-07-15 17:29:59 +02:00
try {
Variant vresult = task->GetResult();
2012-07-15 17:29:59 +02:00
if (vresult.IsObjectType<Dictionary>()) {
CheckResult result = CheckResult(static_cast<Dictionary::Ptr>(vresult));
2012-07-14 16:49:21 +02:00
2012-07-15 17:29:59 +02:00
RequestMessage rm;
rm.SetMethod("checker::CheckResult");
2012-07-14 15:59:59 +02:00
CheckResultMessage params;
params.SetService(service->GetName());
2012-07-15 17:29:59 +02:00
params.SetCheckResult(result);
2012-07-15 17:29:59 +02:00
rm.SetParams(params);
2012-06-18 00:14:34 +02:00
2012-07-15 17:29:59 +02:00
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
}
} catch (const exception& ex) {
stringstream msgbuf;
msgbuf << "Exception occured during check for service '"
<< service->GetName() << "': " << ex.what();
2012-07-15 17:29:59 +02:00
Logger::Write(LogWarning, "checker", msgbuf.str());
2012-07-14 16:49:21 +02:00
}
2012-06-20 10:46:18 +02:00
/* figure out when the next check is for this service; the local
* cibsync component should've already done this as part of processing
* the CheckResult message, but lets do it again to be sure */
service->UpdateNextCheck();
2012-07-15 17:29:59 +02:00
/* remove the service from the list of pending services; if it's not in the
* list this was a manual (i.e. forced) check and we must not re-add the
* service to the services list because it's already there. */
set<Service::Ptr>::iterator it;
it = m_PendingServices.find(service);
if (it != m_PendingServices.end()) {
m_PendingServices.erase(it);
2012-07-15 17:29:59 +02:00
m_Services.push(service);
}
Logger::Write(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
}
2012-06-20 15:23:31 +02:00
void CheckerComponent::ResultTimerHandler(void)
{
Logger::Write(LogDebug, "checker", "ResultTimerHandler entered.");
stringstream msgbuf;
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_Services.size();
Logger::Write(LogInformation, "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
string service;
if (!params.Get("service", &service))
2012-06-15 19:32:41 +02:00
return;
2012-06-14 16:09:04 +02:00
if (!Service::Exists(service)) {
Logger::Write(LogWarning, "checker", "Ignoring delegation request for unknown service '" + service + "'.");
return;
}
2012-06-14 16:09:04 +02:00
Service::Ptr object = Service::GetByName(service);
m_Services.push(object);
2012-06-15 19:32:41 +02:00
Logger::Write(LogDebug, "checker", "Accepted delegation for service '" + service + "'");
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-07-10 12:21:19 +02:00
Logger::Write(LogInformation, "checker", "Clearing service delegations.");
2012-06-21 12:51:50 +02:00
/* clear the services lists */
2012-06-15 19:32:41 +02:00
m_Services = ServiceQueue();
2012-06-21 12:51:50 +02:00
m_PendingServices.clear();
2012-06-14 11:23:25 +02:00
}
EXPORT_COMPONENT(checker, CheckerComponent);