2012-06-14 05:23:25 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
2014-03-18 20:02:29 -04:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-06-14 05:23:25 -04: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 15:19:29 -04:00
|
|
|
#include "checker/checkercomponent.h"
|
2013-10-08 05:57:35 -04:00
|
|
|
#include "icinga/icingaapplication.h"
|
2013-11-10 11:47:47 -05:00
|
|
|
#include "icinga/cib.h"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
|
#include "base/objectlock.h"
|
2013-08-20 05:06:04 -04:00
|
|
|
#include "base/utility.h"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include "base/logger_fwd.h"
|
2013-11-20 15:55:14 -05:00
|
|
|
#include "base/exception.h"
|
2014-03-10 13:01:26 -04:00
|
|
|
#include "base/convert.h"
|
2014-02-17 10:34:18 -05:00
|
|
|
#include "base/statsfunction.h"
|
2013-08-20 05:06:04 -04:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-14 05:23:25 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2013-03-12 08:45:54 -04:00
|
|
|
REGISTER_TYPE(CheckerComponent);
|
2014-02-18 04:53:44 -05:00
|
|
|
|
2014-02-17 10:34:18 -05:00
|
|
|
REGISTER_STATSFUNCTION(CheckerComponentStats, &CheckerComponent::StatsFunc);
|
|
|
|
|
|
|
|
|
|
Value CheckerComponent::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
|
|
|
|
|
{
|
2014-02-18 04:53:44 -05:00
|
|
|
Dictionary::Ptr nodes = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CheckerComponent::Ptr& checker, DynamicType::GetObjects<CheckerComponent>()) {
|
|
|
|
|
unsigned long idle = checker->GetIdleServices();
|
|
|
|
|
unsigned long pending = checker->GetPendingServices();
|
|
|
|
|
|
|
|
|
|
Dictionary::Ptr stats = make_shared<Dictionary>();
|
|
|
|
|
stats->Set("idle", idle);
|
|
|
|
|
stats->Set("pending", pending);
|
|
|
|
|
|
|
|
|
|
nodes->Set(checker->GetName(), stats);
|
|
|
|
|
|
|
|
|
|
String perfdata_prefix = "checkercomponent_" + checker->GetName() + "_";
|
2014-03-10 13:01:26 -04:00
|
|
|
perfdata->Set(perfdata_prefix + "idle", Convert::ToDouble(idle));
|
|
|
|
|
perfdata->Set(perfdata_prefix + "pending", Convert::ToDouble(pending));
|
2014-02-18 04:53:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status->Set("checkercomponent", nodes);
|
2014-02-17 10:34:18 -05:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-03-12 08:45:54 -04:00
|
|
|
|
2013-11-28 14:56:21 -05:00
|
|
|
void CheckerComponent::OnConfigLoaded(void)
|
2012-06-14 05:23:25 -04:00
|
|
|
{
|
2013-09-13 01:49:12 -04: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 04:53:04 -04:00
|
|
|
|
2013-01-22 06:44:23 -05:00
|
|
|
Service::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
|
2013-11-28 14:56:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
|
{
|
|
|
|
|
DynamicObject::Start();
|
2012-08-03 17:03:58 -04:00
|
|
|
|
2013-02-19 17:02:08 -05:00
|
|
|
m_Stopped = false;
|
|
|
|
|
|
2013-03-15 13:21:29 -04:00
|
|
|
m_Thread = boost::thread(boost::bind(&CheckerComponent::CheckThreadProc, this));
|
2012-06-14 05:23:25 -04:00
|
|
|
|
2013-11-06 02:51:56 -05:00
|
|
|
m_ResultTimer = 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();
|
2012-06-14 05:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckerComponent::Stop(void)
|
|
|
|
|
{
|
2014-03-17 04:04:19 -04:00
|
|
|
Log(LogInformation, "checker", "Checker stopped.");
|
|
|
|
|
|
2013-02-19 17:02:08 -05:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
m_Stopped = true;
|
|
|
|
|
m_CV.notify_all();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 04:04:19 -04:00
|
|
|
m_ResultTimer->Stop();
|
2013-02-19 17:02:08 -05:00
|
|
|
m_Thread.join();
|
2012-06-14 05:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-02-18 08:40:24 -05:00
|
|
|
void CheckerComponent::CheckThreadProc(void)
|
2012-06-14 05:23:25 -04:00
|
|
|
{
|
2013-08-30 04:19:32 -04:00
|
|
|
Utility::SetThreadName("Check Scheduler");
|
|
|
|
|
|
2013-02-23 19:10:34 -05:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-17 13:14:34 -05:00
|
|
|
|
2013-02-23 19:10:34 -05:00
|
|
|
for (;;) {
|
2013-03-16 16:18:53 -04:00
|
|
|
typedef boost::multi_index::nth_index<ServiceSet, 1>::type CheckTimeView;
|
2013-02-21 10:12:50 -05:00
|
|
|
CheckTimeView& idx = boost::get<1>(m_IdleServices);
|
2013-02-17 13:14:34 -05:00
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
while (idx.begin() == idx.end() && !m_Stopped)
|
|
|
|
|
m_CV.wait(lock);
|
2012-08-04 07:49:25 -04:00
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
if (m_Stopped)
|
|
|
|
|
break;
|
2013-02-19 17:02:08 -05:00
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
CheckTimeView::iterator it = idx.begin();
|
2013-02-26 04:58:32 -05:00
|
|
|
Service::Ptr service = *it;
|
2013-02-17 13:14:34 -05:00
|
|
|
|
2013-10-18 08:10:31 -04:00
|
|
|
if (!service->HasAuthority("checker")) {
|
|
|
|
|
m_IdleServices.erase(service);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 03:07:47 -05:00
|
|
|
double wait = service->GetNextCheck() - Utility::GetTime();
|
2013-02-11 07:05:08 -05:00
|
|
|
|
2013-02-18 08:40:24 -05:00
|
|
|
if (wait > 0) {
|
|
|
|
|
/* Wait for the next check. */
|
2013-10-18 07:46:22 -04:00
|
|
|
m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
|
2013-02-18 08:40:24 -05:00
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
m_IdleServices.erase(service);
|
2012-06-17 16:46:40 -04:00
|
|
|
|
2013-09-10 03:26:46 -04:00
|
|
|
bool forced = service->GetForceNextCheck();
|
2013-03-13 11:04:53 -04:00
|
|
|
bool check = true;
|
2013-09-12 04:03:48 -04:00
|
|
|
|
2013-09-10 03:26:46 -04:00
|
|
|
if (!forced) {
|
2014-02-27 05:05:55 -05:00
|
|
|
if (!service->IsReachable(DependencyCheckExecution)) {
|
|
|
|
|
Log(LogDebug, "icinga", "Skipping check for service '" + service->GetName() + "': Dependency failed.");
|
|
|
|
|
check = false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-08 05:57:35 -04:00
|
|
|
if (!service->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableChecks()) {
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': active checks are disabled");
|
2013-03-13 11:04:53 -04:00
|
|
|
check = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimePeriod::Ptr tp = service->GetCheckPeriod();
|
|
|
|
|
|
|
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogDebug, "checker", "Skipping check for service '" + service->GetName() + "': not in check_period");
|
2013-03-13 11:04:53 -04:00
|
|
|
check = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* reschedule the service if checks are disabled */
|
|
|
|
|
if (!check) {
|
2013-11-28 14:55:10 -05:00
|
|
|
m_IdleServices.insert(service);
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
2013-10-18 08:10:31 -04:00
|
|
|
service->UpdateNextCheck();
|
2013-01-22 10:01:08 -05:00
|
|
|
|
2013-11-28 14:55:10 -05:00
|
|
|
lock.lock();
|
2013-01-22 10:01:08 -05:00
|
|
|
|
2013-03-02 03:07:47 -05:00
|
|
|
continue;
|
2013-01-22 10:01:08 -05:00
|
|
|
}
|
|
|
|
|
|
2013-03-06 07:01:51 -05:00
|
|
|
m_PendingServices.insert(service);
|
|
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
2013-09-10 03:26:46 -04:00
|
|
|
if (forced) {
|
2013-03-04 09:52:42 -05:00
|
|
|
ObjectLock olock(service);
|
|
|
|
|
service->SetForceNextCheck(false);
|
|
|
|
|
}
|
2013-01-23 09:25:00 -05:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
|
2012-06-17 14:35:56 -04:00
|
|
|
|
2013-03-25 13:36:15 -04:00
|
|
|
CheckerComponent::Ptr self = GetSelf();
|
2013-11-15 06:18:40 -05:00
|
|
|
m_Pool.Post(boost::bind(&CheckerComponent::ExecuteCheckHelper, self, service));
|
2013-03-06 07:01:51 -05:00
|
|
|
|
|
|
|
|
lock.lock();
|
2012-10-15 02:52:31 -04:00
|
|
|
}
|
2012-06-14 05:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-03-25 13:36:15 -04:00
|
|
|
void CheckerComponent::ExecuteCheckHelper(const Service::Ptr& service)
|
2012-06-17 14:35:56 -04:00
|
|
|
{
|
2013-03-25 13:36:15 -04:00
|
|
|
try {
|
|
|
|
|
service->ExecuteCheck();
|
|
|
|
|
} catch (const std::exception& ex) {
|
2013-11-20 15:55:14 -05:00
|
|
|
Log(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + DiagnosticInformation(ex));
|
2013-03-25 13:36:15 -04:00
|
|
|
}
|
|
|
|
|
|
2013-03-27 11:26:56 -04:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-18 08:40:24 -05:00
|
|
|
|
2013-03-27 11:26:56 -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. */
|
|
|
|
|
CheckerComponent::ServiceSet::iterator it;
|
|
|
|
|
it = m_PendingServices.find(service);
|
|
|
|
|
if (it != m_PendingServices.end()) {
|
|
|
|
|
m_PendingServices.erase(it);
|
2013-09-13 01:49:12 -04:00
|
|
|
|
|
|
|
|
if (service->IsActive() && service->HasAuthority("checker"))
|
|
|
|
|
m_IdleServices.insert(service);
|
|
|
|
|
|
2013-03-27 11:26:56 -04:00
|
|
|
m_CV.notify_all();
|
|
|
|
|
}
|
2012-07-15 11:29:59 -04:00
|
|
|
}
|
2012-06-25 08:13:24 -04:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
|
2012-07-13 15:00:54 -04:00
|
|
|
}
|
2012-06-20 09:23:31 -04:00
|
|
|
|
2012-07-13 15:00:54 -04:00
|
|
|
void CheckerComponent::ResultTimerHandler(void)
|
|
|
|
|
{
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogDebug, "checker", "ResultTimerHandler entered.");
|
2012-06-22 01:24:50 -04:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-17 13:14:34 -05:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
2013-11-10 11:47:47 -05:00
|
|
|
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_IdleServices.size() << "; Checks/s: " << CIB::GetActiveChecksStatistics(5) / 5.0;
|
2013-02-17 13:14:34 -05:00
|
|
|
}
|
|
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
Log(LogInformation, "checker", msgbuf.str());
|
2012-06-17 14:35:56 -04:00
|
|
|
}
|
|
|
|
|
|
2013-09-13 01:49:12 -04:00
|
|
|
void CheckerComponent::ObjectHandler(const DynamicObject::Ptr& object)
|
2012-06-14 05:23:25 -04:00
|
|
|
{
|
2013-08-20 05:06:04 -04:00
|
|
|
if (object->GetType() != DynamicType::GetByName("Service"))
|
|
|
|
|
return;
|
2013-02-21 10:12:50 -05:00
|
|
|
|
2013-08-20 05:06:04 -04:00
|
|
|
Service::Ptr service = static_pointer_cast<Service>(object);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-06-14 10:09:04 -04:00
|
|
|
|
2013-09-13 01:49:12 -04:00
|
|
|
if (object->IsActive() && object->HasAuthority("checker")) {
|
|
|
|
|
if (m_PendingServices.find(service) != m_PendingServices.end())
|
|
|
|
|
return;
|
2012-06-14 10:09:04 -04:00
|
|
|
|
2013-09-13 01:49:12 -04:00
|
|
|
m_IdleServices.insert(service);
|
|
|
|
|
} else {
|
|
|
|
|
m_IdleServices.erase(service);
|
|
|
|
|
m_PendingServices.erase(service);
|
|
|
|
|
}
|
2013-08-20 05:06:04 -04:00
|
|
|
|
2013-02-18 08:40:24 -05:00
|
|
|
m_CV.notify_all();
|
2012-07-17 06:57:21 -04:00
|
|
|
}
|
2012-06-14 05:23:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-01-22 06:44:23 -05:00
|
|
|
void CheckerComponent::NextCheckChangedHandler(const Service::Ptr& service)
|
|
|
|
|
{
|
2013-02-21 10:12:50 -05: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 16:18:53 -04:00
|
|
|
typedef boost::multi_index::nth_index<ServiceSet, 0>::type ServiceView;
|
2013-02-21 10:12:50 -05:00
|
|
|
ServiceView& idx = boost::get<0>(m_IdleServices);
|
2013-02-17 13:14:34 -05:00
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
ServiceView::iterator it = idx.find(service);
|
|
|
|
|
if (it == idx.end())
|
|
|
|
|
return;
|
2013-01-22 06:44:23 -05:00
|
|
|
|
2013-02-21 10:12:50 -05:00
|
|
|
idx.erase(service);
|
|
|
|
|
idx.insert(service);
|
|
|
|
|
m_CV.notify_all();
|
2013-01-22 06:44:23 -05:00
|
|
|
}
|
2014-02-18 04:53:44 -05:00
|
|
|
|
|
|
|
|
unsigned long CheckerComponent::GetIdleServices(void)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
|
|
return m_IdleServices.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long CheckerComponent::GetPendingServices(void)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
|
|
return m_PendingServices.size();
|
|
|
|
|
}
|