icinga2/components/checker/checkercomponent.cpp

195 lines
6.5 KiB
C++
Raw Normal View History

2012-06-14 05:23:25 -04: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 07:21:40 -04:00
return "checker";
2012-06-14 05:23:25 -04:00
}
void CheckerComponent::Start(void)
{
2012-07-02 05:07:54 -04:00
m_Endpoint = boost::make_shared<VirtualEndpoint>();
m_Endpoint->RegisterTopicHandler("checker::AssignService",
2012-06-15 21:42:54 -04:00
boost::bind(&CheckerComponent::AssignServiceRequestHandler, this, _2, _3));
2012-07-02 05:07:54 -04:00
m_Endpoint->RegisterTopicHandler("checker::ClearServices",
2012-06-15 21:42:54 -04:00
boost::bind(&CheckerComponent::ClearServicesRequestHandler, this, _2, _3));
2012-07-02 05:07:54 -04:00
m_Endpoint->RegisterPublication("checker::CheckResult");
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
2012-06-14 05:23:25 -04:00
2012-06-15 13:32:41 -04:00
m_CheckTimer = boost::make_shared<Timer>();
m_CheckTimer->SetInterval(1);
2012-06-15 13:32:41 -04:00
m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
2012-06-14 05:23:25 -04:00
m_CheckTimer->Start();
NagiosCheckTask::Register();
NullCheckTask::Register();
2012-06-14 05:23:25 -04:00
2012-06-17 14:35:56 -04:00
m_ResultTimer = boost::make_shared<Timer>();
2012-06-17 17:10:03 -04:00
m_ResultTimer->SetInterval(5);
2012-06-17 14:35:56 -04:00
m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
m_ResultTimer->Start();
CIB::RequireInformation(CIB_Configuration);
CIB::RequireInformation(CIB_ServiceStatus);
2012-06-14 05:23:25 -04:00
}
void CheckerComponent::Stop(void)
{
2012-06-27 12:43:34 -04:00
EndpointManager::Ptr mgr = EndpointManager::GetInstance();
2012-06-14 05:23:25 -04:00
2012-06-14 07:21:40 -04:00
if (mgr)
2012-07-02 05:07:54 -04:00
mgr->UnregisterEndpoint(m_Endpoint);
2012-06-14 05:23:25 -04:00
}
2012-06-15 13:32:41 -04:00
void CheckerComponent::CheckTimerHandler(void)
2012-06-14 05:23:25 -04:00
{
time_t now;
time(&now);
2012-07-10 06:21:19 -04:00
Logger::Write(LogDebug, "checker", "CheckTimerHandler entered.");
long tasks = 0;
2012-06-19 03:38:20 -04:00
while (!m_Services.empty()) {
2012-06-14 05:23:25 -04:00
Service service = m_Services.top();
2012-06-19 03:38:20 -04:00
if (service.GetNextCheck() > now)
2012-06-14 05:23:25 -04:00
break;
m_Services.pop();
2012-07-10 06:21:19 -04:00
Logger::Write(LogDebug, "checker", "Executing service check for '" + service.GetName() + "'");
2012-06-17 14:35:56 -04:00
m_PendingServices.insert(service.GetConfigObject());
2012-07-14 09:59:59 -04:00
vector<Variant> arguments;
arguments.push_back(service.GetConfigObject());
ScriptTask::Ptr task;
2012-07-16 02:19:51 -04:00
task = service.InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, _1));
2012-07-14 09:59:59 -04:00
assert(task); /* TODO: gracefully handle missing hooks */
service.SetTag("current_task", task);
tasks++;
2012-06-14 05:23:25 -04:00
}
2012-07-10 06:21:19 -04:00
Logger::Write(LogDebug, "checker", "CheckTimerHandler: past loop.");
stringstream msgbuf;
msgbuf << "CheckTimerHandler: created " << tasks << " tasks";
2012-07-10 06:21:19 -04:00
Logger::Write(LogInformation, "checker", msgbuf.str());
2012-06-14 05:23:25 -04:00
}
void CheckerComponent::CheckCompletedHandler(Service service, const ScriptTask::Ptr& task)
2012-06-17 14:35:56 -04:00
{
service.RemoveTag("current_task");
/* figure out when the next check is for this service */
service.UpdateNextCheck();
2012-07-15 11:29:59 -04:00
try {
Variant vresult = task->GetResult();
2012-07-15 11:29:59 -04:00
if (vresult.IsObjectType<Dictionary>()) {
CheckResult result = CheckResult(static_cast<Dictionary::Ptr>(vresult));
2012-07-14 10:49:21 -04:00
2012-07-15 11:29:59 -04:00
/* update service state */
service.ApplyCheckResult(result);
2012-07-14 09:59:59 -04:00
2012-07-15 11:29:59 -04:00
RequestMessage rm;
rm.SetMethod("checker::CheckResult");
2012-07-14 09:59:59 -04:00
2012-07-15 11:29:59 -04:00
ServiceStatusMessage params;
params.SetService(service.GetName());
params.SetState(service.GetState());
params.SetStateType(service.GetStateType());
params.SetCurrentCheckAttempt(service.GetCurrentCheckAttempt());
params.SetNextCheck(service.GetNextCheck());
params.SetCheckResult(result);
2012-07-15 11:29:59 -04:00
rm.SetParams(params);
2012-06-17 18:14:34 -04:00
2012-07-15 11:29:59 -04:00
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
}
} catch (const exception& ex) {
stringstream msgbuf;
msgbuf << "Exception occured during check for service '"
<< service.GetName() << "': " << ex.what();
Logger::Write(LogWarning, "checker", msgbuf.str());
2012-07-14 10:49:21 -04:00
}
2012-06-20 04:46:18 -04:00
2012-07-15 11:29:59 -04: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<ConfigObject::Ptr>::iterator it;
it = m_PendingServices.find(service.GetConfigObject());
if (it != m_PendingServices.end()) {
m_PendingServices.erase(it);
2012-07-15 11:29:59 -04:00
m_Services.push(service);
}
2012-07-14 10:49:21 -04:00
Logger::Write(LogDebug, "checker", "Check finished for service '" + service.GetName() + "'");
}
2012-06-20 09:23:31 -04: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 14:35:56 -04:00
}
2012-06-15 21:42:54 -04:00
void CheckerComponent::AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
2012-06-14 05:23:25 -04:00
{
2012-06-14 10:09:04 -04:00
MessagePart params;
2012-06-15 21:42:54 -04:00
if (!request.GetParams(&params))
2012-06-15 13:32:41 -04:00
return;
2012-06-14 10:09:04 -04:00
string service;
if (!params.Get("service", &service))
2012-06-15 13:32:41 -04:00
return;
2012-06-14 10:09:04 -04:00
ConfigObject::Ptr object = ConfigObject::GetObject("service", service);
2012-06-14 10:09:04 -04:00
if (!object) {
Logger::Write(LogWarning, "checker", "Ignoring delegation request for unknown service '" + service + "'.");
return;
}
2012-06-14 10:09:04 -04:00
m_Services.push(object);
2012-06-15 13:32:41 -04:00
Logger::Write(LogDebug, "checker", "Accepted delegation for service '" + service + "'");
2012-06-14 05:23:25 -04:00
}
2012-06-15 21:42:54 -04:00
void CheckerComponent::ClearServicesRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
2012-06-14 05:23:25 -04:00
{
2012-07-10 06:21:19 -04:00
Logger::Write(LogInformation, "checker", "Clearing service delegations.");
2012-06-21 06:51:50 -04:00
/* clear the services lists */
2012-06-15 13:32:41 -04:00
m_Services = ServiceQueue();
2012-06-21 06:51:50 -04:00
m_PendingServices.clear();
2012-06-14 05:23:25 -04:00
}
EXPORT_COMPONENT(checker, CheckerComponent);