icinga2/components/checker/checkercomponent.cpp

220 lines
6.6 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;
REGISTER_TYPE(CheckerComponent);
CheckerComponent::CheckerComponent(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
{ }
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-02 09:07:47 +01:00
/* 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;
2013-03-15 18:21:29 +01:00
m_Thread = boost::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-02 09:07:47 +01:00
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
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-03-02 09:07:47 +01:00
double wait = service->GetNextCheck() - Utility::GetTime();
2013-02-18 14:40:24 +01:00
if (wait > 0) {
/* 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);
2013-03-13 16:04:53 +01:00
bool check = true;
2013-03-13 16:04:53 +01:00
if (!service->GetForceNextCheck()) {
if (!service->GetEnableActiveChecks()) {
Logger::Write(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': active checks are disabled");
check = false;
}
TimePeriod::Ptr tp = service->GetCheckPeriod();
if (tp && !tp->IsInside(Utility::GetTime())) {
Logger::Write(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': not in check_period");
check = false;
}
}
/* reschedule the service if checks are disabled */
if (!check) {
2013-03-02 09:07:47 +01:00
service->UpdateNextCheck();
2013-03-02 09:07:47 +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-03-02 09:07:47 +01:00
idx.insert(service);
2013-03-02 09:07:47 +01:00
continue;
}
m_IdleServices.erase(service);
m_PendingServices.insert(service);
lock.unlock();
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(service);
service->SetForceNextCheck(false);
}
Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
2012-06-17 20:35:56 +02:00
try {
2013-03-02 09:07:47 +01:00
CheckerComponent::Ptr self = GetSelf();
2013-03-04 15:52:42 +01:00
service->BeginExecuteCheck(boost::bind(&CheckerComponent::CheckCompletedHandler, self, service));
} catch (const exception& ex) {
Logger::Write(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + diagnostic_information(ex));
}
lock.lock();
}
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
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-26 10:13:54 +01:00
String checker = service->GetCurrentChecker();
2012-06-14 16:09:04 +02:00
2013-03-02 09:07:47 +01:00
if (checker == EndpointManager::GetInstance()->GetIdentity() || 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);
/* 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();
}