icinga2/components/checker/checkercomponent.cpp

237 lines
6.9 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;
2013-02-24 01:10:34 +01:00
REGISTER_COMPONENT("checker", CheckerComponent);
2012-06-14 11:23:25 +02:00
void CheckerComponent::Start(void)
{
m_Endpoint = Endpoint::MakeEndpoint("checker", false);
2012-08-14 10:53:04 +02:00
2013-03-01 12:07:52 +01:00
{
ObjectLock olock(m_Endpoint);
/* dummy registration so the delegation module knows this is a checker
TODO: figure out a better way for this */
m_Endpoint->RegisterSubscription("checker");
}
2012-08-14 10:53:04 +02:00
Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1));
Service::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
2013-02-19 23:02:08 +01:00
m_Stopped = false;
m_Thread = thread(boost::bind(&CheckerComponent::CheckThreadProc, this));
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)
{
2013-03-01 12:07:52 +01:00
{
ObjectLock olock(m_Endpoint);
m_Endpoint->Unregister();
}
2013-02-19 23:02:08 +01:00
{
boost::mutex::scoped_lock lock(m_Mutex);
m_Stopped = true;
m_CV.notify_all();
}
m_Thread.join();
2012-06-14 11:23:25 +02:00
}
2013-02-18 14:40:24 +01:00
void CheckerComponent::CheckThreadProc(void)
2012-06-14 11:23:25 +02:00
{
2013-02-24 01:10:34 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-02-17 19:14:34 +01:00
2013-02-24 01:10:34 +01:00
for (;;) {
2013-02-21 16:12:50 +01:00
typedef nth_index<ServiceSet, 1>::type CheckTimeView;
CheckTimeView& idx = boost::get<1>(m_IdleServices);
2013-02-17 19:14:34 +01:00
2013-02-21 16:12:50 +01:00
while (idx.begin() == idx.end() && !m_Stopped)
m_CV.wait(lock);
2013-02-21 16:12:50 +01:00
if (m_Stopped)
break;
2013-02-19 23:02:08 +01:00
2013-02-21 16:12:50 +01:00
CheckTimeView::iterator it = idx.begin();
Service::Ptr service = *it;
2013-02-17 19:14:34 +01:00
2013-03-01 12:07:52 +01:00
ObjectLock olock(service); /* also required for the key extractor. */
if (!service->IsRegistered()) {
2013-02-21 16:12:50 +01:00
idx.erase(it);
continue;
2013-02-18 14:40:24 +01:00
}
2013-02-17 19:14:34 +01:00
2013-02-18 14:40:24 +01:00
double wait;
2013-02-17 19:14:34 +01:00
2013-02-18 14:40:24 +01:00
{
ObjectLock olock(service);
wait = service->GetNextCheck() - Utility::GetTime();
}
2013-02-18 14:40:24 +01:00
if (wait > 0) {
2013-02-21 16:12:50 +01:00
/* Release the object lock. */
olock.Unlock();
2013-02-18 14:40:24 +01:00
/* Make sure the service we just examined can be destroyed while we're waiting. */
service.reset();
/* Wait for the next check. */
2013-02-19 23:02:08 +01:00
if (!m_Stopped)
m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
2013-02-18 14:40:24 +01:00
continue;
}
2013-02-21 16:12:50 +01:00
m_IdleServices.erase(service);
/* reschedule the service if checks are currently disabled
* for it and this is not a forced check */
if (!service->GetEnableActiveChecks()) {
if (!service->GetForceNextCheck()) {
Logger::Write(LogDebug, "checker", "Ignoring service check for disabled service: " + service->GetName());
service->UpdateNextCheck();
2013-02-21 16:12:50 +01:00
typedef nth_index<ServiceSet, 1>::type CheckTimeView;
CheckTimeView& idx = boost::get<1>(m_IdleServices);
2013-02-17 19:14:34 +01:00
2013-02-21 16:12:50 +01:00
idx.insert(service);
continue;
}
}
service->SetForceNextCheck(false);
Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
2012-06-17 20:35:56 +02:00
2013-02-21 16:12:50 +01:00
m_IdleServices.erase(service);
m_PendingServices.insert(service);
try {
2013-02-24 01:10:34 +01:00
olock.Unlock();
2013-03-01 12:07:52 +01:00
CheckerComponent::Ptr self;
{
ObjectLock olock(this);
self = GetSelf();
}
Service::BeginExecuteCheck(service, boost::bind(&CheckerComponent::CheckCompletedHandler, static_cast<CheckerComponent::Ptr>(self), service));
} catch (const exception& ex) {
2013-02-24 01:10:34 +01:00
olock.Lock();
Logger::Write(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + diagnostic_information(ex));
}
}
2012-06-14 11:23:25 +02:00
}
void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service)
2012-06-17 20:35:56 +02:00
{
2013-02-21 16:12:50 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-02-18 14:40:24 +01:00
ObjectLock olock(service); /* required for the key extractor */
2013-02-21 16:12:50 +01: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. */
CheckerComponent::ServiceSet::iterator it;
it = m_PendingServices.find(service);
if (it != m_PendingServices.end()) {
m_PendingServices.erase(it);
m_IdleServices.insert(service);
m_CV.notify_all();
2012-07-15 17:29:59 +02:00
}
2013-02-18 14:40:24 +01:00
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;
2013-02-17 19:14:34 +01:00
{
boost::mutex::scoped_lock lock(m_Mutex);
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_IdleServices.size();
}
Logger::Write(LogInformation, "checker", msgbuf.str());
2012-06-17 20:35:56 +02:00
}
void CheckerComponent::CheckerChangedHandler(const Service::Ptr& service)
2012-06-14 11:23:25 +02:00
{
2013-02-21 16:12:50 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-02-18 14:40:24 +01:00
ObjectLock olock(service); /* also required for the key extractor */
2013-02-26 10:13:54 +01:00
String checker = service->GetCurrentChecker();
2012-06-14 16:09:04 +02:00
2013-03-01 12:07:52 +01:00
EndpointManager::Ptr em = EndpointManager::GetInstance();
String identity;
{
ObjectLock elock(em);
identity = em->GetIdentity();
}
if (checker == identity || Endpoint::GetByName(checker) == m_Endpoint) {
if (m_PendingServices.find(service) != m_PendingServices.end())
return;
2012-06-14 16:09:04 +02:00
m_IdleServices.insert(service);
2013-02-18 14:40:24 +01:00
m_CV.notify_all();
} else {
m_IdleServices.erase(service);
m_PendingServices.erase(service);
2013-02-18 14:40:24 +01:00
m_CV.notify_all();
}
2012-06-14 11:23:25 +02:00
}
void CheckerComponent::NextCheckChangedHandler(const Service::Ptr& service)
{
2013-02-21 16:12:50 +01:00
boost::mutex::scoped_lock lock(m_Mutex);
ObjectLock olock(service); /* required for the key extractor */
2013-02-21 16:12:50 +01:00
/* remove and re-insert the service from the set in order to force an index update */
typedef nth_index<ServiceSet, 0>::type ServiceView;
ServiceView& idx = boost::get<0>(m_IdleServices);
2013-02-17 19:14:34 +01:00
2013-02-21 16:12:50 +01:00
ServiceView::iterator it = idx.find(service);
if (it == idx.end())
return;
2013-02-21 16:12:50 +01:00
idx.erase(service);
idx.insert(service);
m_CV.notify_all();
}