2012-06-14 11:23:25 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2012-06-14 11:23:25 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "checker/checkercomponent.h"
|
2013-10-08 11:57:35 +02:00
|
|
|
#include "icinga/icingaapplication.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-08-20 11:06:04 +02:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
2013-08-20 11:06:04 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-14 11:23:25 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-12 13:45:54 +01:00
|
|
|
REGISTER_TYPE(CheckerComponent);
|
|
|
|
|
2012-06-14 11:23:25 +02:00
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
2012-08-14 10:53:04 +02:00
|
|
|
|
2013-09-13 07:49:12 +02:00
|
|
|
DynamicObject::OnStarted.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
|
|
|
|
DynamicObject::OnStopped.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
|
|
|
|
DynamicObject::OnAuthorityChanged.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
|
2012-08-14 10:53:04 +02:00
|
|
|
|
2013-01-22 12:44:23 +01:00
|
|
|
Service::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
|
2012-08-03 23:03:58 +02:00
|
|
|
|
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
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
m_ResultTimer = 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();
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
|
2013-09-13 07:49:12 +02:00
|
|
|
ObjectHandler(service);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Stop(void)
|
|
|
|
{
|
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-08-30 10:19:32 +02:00
|
|
|
Utility::SetThreadName("Check Scheduler");
|
|
|
|
|
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-03-16 21:18:53 +01:00
|
|
|
typedef boost::multi_index::nth_index<ServiceSet, 1>::type CheckTimeView;
|
2013-02-21 16:12:50 +01:00
|
|
|
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);
|
2012-08-04 13:49:25 +02:00
|
|
|
|
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();
|
2013-02-26 10:58:32 +01:00
|
|
|
Service::Ptr service = *it;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-10-18 14:10:31 +02:00
|
|
|
if (!service->HasAuthority("checker")) {
|
|
|
|
m_IdleServices.erase(service);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
double wait = service->GetNextCheck() - Utility::GetTime();
|
2013-02-11 13:05:08 +01:00
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
if (wait > 0) {
|
|
|
|
/* Wait for the next check. */
|
2013-10-18 13:46:22 +02:00
|
|
|
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);
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2013-09-10 09:26:46 +02:00
|
|
|
bool forced = service->GetForceNextCheck();
|
2013-03-13 16:04:53 +01:00
|
|
|
bool check = true;
|
2013-09-12 10:03:48 +02:00
|
|
|
|
2013-09-10 09:26:46 +02:00
|
|
|
if (!forced) {
|
2013-10-08 11:57:35 +02:00
|
|
|
if (!service->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableChecks()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': active checks are disabled");
|
2013-03-13 16:04:53 +01:00
|
|
|
check = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimePeriod::Ptr tp = service->GetCheckPeriod();
|
|
|
|
|
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': not in check_period");
|
2013-03-13 16:04:53 +01:00
|
|
|
check = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reschedule the service if checks are disabled */
|
|
|
|
if (!check) {
|
2013-10-18 14:10:31 +02:00
|
|
|
service->UpdateNextCheck();
|
2013-01-22 16:01:08 +01:00
|
|
|
|
2013-10-18 14:10:31 +02:00
|
|
|
m_IdleServices.insert(service);
|
2013-01-22 16:01:08 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
continue;
|
2013-01-22 16:01:08 +01:00
|
|
|
}
|
|
|
|
|
2013-03-06 13:01:51 +01:00
|
|
|
m_PendingServices.insert(service);
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
2013-09-10 09:26:46 +02:00
|
|
|
if (forced) {
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(service);
|
|
|
|
service->SetForceNextCheck(false);
|
|
|
|
}
|
2013-01-23 15:25:00 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
|
2012-06-17 20:35:56 +02:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
CheckerComponent::Ptr self = GetSelf();
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(&CheckerComponent::ExecuteCheckHelper, self, service));
|
2013-03-06 13:01:51 +01:00
|
|
|
|
|
|
|
lock.lock();
|
2012-10-15 08:52:31 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
void CheckerComponent::ExecuteCheckHelper(const Service::Ptr& service)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
try {
|
|
|
|
service->ExecuteCheck();
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + boost::diagnostic_information(ex));
|
|
|
|
}
|
|
|
|
|
2013-03-27 16:26:56 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-27 16:26:56 +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);
|
2013-09-13 07:49:12 +02:00
|
|
|
|
|
|
|
if (service->IsActive() && service->HasAuthority("checker"))
|
|
|
|
m_IdleServices.insert(service);
|
|
|
|
|
2013-03-27 16:26:56 +01:00
|
|
|
m_CV.notify_all();
|
|
|
|
}
|
2012-07-15 17:29:59 +02:00
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2012-06-20 15:23:31 +02:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
void CheckerComponent::ResultTimerHandler(void)
|
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "checker", "ResultTimerHandler entered.");
|
2012-06-22 07:24:50 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream 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();
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "checker", msgbuf.str());
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2013-09-13 07:49:12 +02:00
|
|
|
void CheckerComponent::ObjectHandler(const DynamicObject::Ptr& object)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
if (object->GetType() != DynamicType::GetByName("Service"))
|
|
|
|
return;
|
2013-02-21 16:12:50 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Service::Ptr service = static_pointer_cast<Service>(object);
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2013-09-13 07:49:12 +02:00
|
|
|
if (object->IsActive() && object->HasAuthority("checker")) {
|
|
|
|
if (m_PendingServices.find(service) != m_PendingServices.end())
|
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2013-09-13 07:49:12 +02:00
|
|
|
m_IdleServices.insert(service);
|
|
|
|
} else {
|
|
|
|
m_IdleServices.erase(service);
|
|
|
|
m_PendingServices.erase(service);
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
m_CV.notify_all();
|
2012-07-17 12:57:21 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 12:44:23 +01: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 */
|
2013-03-16 21:18:53 +01:00
|
|
|
typedef boost::multi_index::nth_index<ServiceSet, 0>::type ServiceView;
|
2013-02-21 16:12:50 +01:00
|
|
|
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-01-22 12:44:23 +01:00
|
|
|
|
2013-02-21 16:12:50 +01:00
|
|
|
idx.erase(service);
|
|
|
|
idx.insert(service);
|
|
|
|
m_CV.notify_all();
|
2013-01-22 12:44:23 +01:00
|
|
|
}
|