2012-06-14 11:23:25 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "checker/checkercomponent.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "icinga/cib.hpp"
|
2014-09-17 15:38:39 +02:00
|
|
|
#include "icinga/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/statsfunction.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);
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-02-17 16:34:18 +01:00
|
|
|
REGISTER_STATSFUNCTION(CheckerComponentStats, &CheckerComponent::StatsFunc);
|
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void CheckerComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr nodes = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const CheckerComponent::Ptr& checker, DynamicType::GetObjectsByType<CheckerComponent>()) {
|
2014-04-03 15:36:13 +02:00
|
|
|
unsigned long idle = checker->GetIdleCheckables();
|
|
|
|
unsigned long pending = checker->GetPendingCheckables();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr stats = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
stats->Set("idle", idle);
|
|
|
|
stats->Set("pending", pending);
|
|
|
|
|
|
|
|
nodes->Set(checker->GetName(), stats);
|
|
|
|
|
|
|
|
String perfdata_prefix = "checkercomponent_" + checker->GetName() + "_";
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue(perfdata_prefix + "idle", Convert::ToDouble(idle)));
|
|
|
|
perfdata->Add(new PerfdataValue(perfdata_prefix + "pending", Convert::ToDouble(pending)));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("checkercomponent", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
2013-03-12 13:45:54 +01:00
|
|
|
|
2015-03-03 09:14:15 +01:00
|
|
|
CheckerComponent::CheckerComponent(void)
|
|
|
|
: m_Stopped(false)
|
|
|
|
{ }
|
|
|
|
|
2013-11-28 20:56:21 +01:00
|
|
|
void CheckerComponent::OnConfigLoaded(void)
|
2012-06-14 11:23:25 +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));
|
2014-05-09 11:32:24 +02:00
|
|
|
DynamicObject::OnPaused.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
|
|
|
|
DynamicObject::OnResumed.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
|
2012-08-14 10:53:04 +02:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Checkable::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
|
2013-11-28 20:56:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Start();
|
2012-08-03 23:03:58 +02:00
|
|
|
|
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
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ResultTimer = new 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)
|
|
|
|
{
|
2014-05-28 13:03:44 +02:00
|
|
|
Log(LogInformation, "CheckerComponent", "Checker stopped.");
|
2014-03-17 09:04:19 +01:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Stopped = true;
|
|
|
|
m_CV.notify_all();
|
|
|
|
}
|
|
|
|
|
2014-03-17 09:04:19 +01:00
|
|
|
m_ResultTimer->Stop();
|
2013-02-19 23:02:08 +01:00
|
|
|
m_Thread.join();
|
2014-04-16 15:01:31 +02:00
|
|
|
|
|
|
|
DynamicObject::Stop();
|
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 (;;) {
|
2014-04-03 15:36:13 +02:00
|
|
|
typedef boost::multi_index::nth_index<CheckableSet, 1>::type CheckTimeView;
|
|
|
|
CheckTimeView& idx = boost::get<1>(m_IdleCheckables);
|
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();
|
2014-04-05 19:02:45 +02:00
|
|
|
Checkable::Ptr checkable = *it;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
double wait = checkable->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;
|
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
m_IdleCheckables.erase(checkable);
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
bool forced = checkable->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) {
|
2014-04-05 19:02:45 +02:00
|
|
|
if (!checkable->IsReachable(DependencyCheckExecution)) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "CheckerComponent")
|
|
|
|
<< "Skipping check for object '" << checkable->GetName() << "': Dependency failed.";
|
2014-02-27 11:05:55 +01:00
|
|
|
check = false;
|
|
|
|
}
|
|
|
|
|
2014-04-17 11:29:47 +02:00
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2014-05-01 21:57:58 +02:00
|
|
|
if (host && !service && (!checkable->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableHostChecks())) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "CheckerComponent")
|
|
|
|
<< "Skipping check for host '" << host->GetName() << "': active host checks are disabled";
|
2014-04-17 11:29:47 +02:00
|
|
|
check = false;
|
|
|
|
}
|
2014-05-01 21:57:58 +02:00
|
|
|
if (host && service && (!checkable->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableServiceChecks())) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "CheckerComponent")
|
|
|
|
<< "Skipping check for service '" << service->GetName() << "': active service checks are disabled";
|
2013-03-13 16:04:53 +01:00
|
|
|
check = false;
|
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
TimePeriod::Ptr tp = checkable->GetCheckPeriod();
|
2013-03-13 16:04:53 +01:00
|
|
|
|
|
|
|
if (tp && !tp->IsInside(Utility::GetTime())) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "CheckerComponent")
|
|
|
|
<< "Skipping check for object '" << checkable->GetName() << "': not in check_period";
|
2013-03-13 16:04:53 +01:00
|
|
|
check = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
/* reschedule the checkable if checks are disabled */
|
2013-03-13 16:04:53 +01:00
|
|
|
if (!check) {
|
2014-04-05 19:02:45 +02:00
|
|
|
m_IdleCheckables.insert(checkable);
|
2013-11-28 20:55:10 +01:00
|
|
|
lock.unlock();
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
checkable->UpdateNextCheck();
|
2013-01-22 16:01:08 +01:00
|
|
|
|
2013-11-28 20:55:10 +01:00
|
|
|
lock.lock();
|
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
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
m_PendingCheckables.insert(checkable);
|
2013-03-06 13:01:51 +01:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
2013-09-10 09:26:46 +02:00
|
|
|
if (forced) {
|
2014-04-05 19:02:45 +02:00
|
|
|
ObjectLock olock(checkable);
|
|
|
|
checkable->SetForceNextCheck(false);
|
2013-03-04 15:52:42 +01:00
|
|
|
}
|
2013-01-23 15:25:00 +01:00
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "CheckerComponent")
|
|
|
|
<< "Executing check for '" << checkable->GetName() << "'";
|
2012-06-17 20:35:56 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&CheckerComponent::ExecuteCheckHelper, CheckerComponent::Ptr(this), checkable));
|
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
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
void CheckerComponent::ExecuteCheckHelper(const Checkable::Ptr& checkable)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
try {
|
2014-04-05 19:02:45 +02:00
|
|
|
checkable->ExecuteCheck();
|
2013-03-25 18:36:15 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-11-08 21:17:16 +01:00
|
|
|
CheckResult::Ptr cr = new CheckResult();
|
2014-04-22 19:02:45 +02:00
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
|
|
|
|
String output = "Exception occured while checking '" + checkable->GetName() + "': " + DiagnosticInformation(ex);
|
|
|
|
cr->SetOutput(output);
|
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
cr->SetScheduleStart(now);
|
|
|
|
cr->SetScheduleEnd(now);
|
|
|
|
cr->SetExecutionStart(now);
|
|
|
|
cr->SetExecutionEnd(now);
|
|
|
|
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
|
|
|
|
Log(LogCritical, "checker", output);
|
2013-03-25 18:36:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:26:56 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
/* remove the object from the list of pending objects; if it's not in the
|
2013-03-27 16:26:56 +01:00
|
|
|
* list this was a manual (i.e. forced) check and we must not re-add the
|
2014-04-05 19:02:45 +02:00
|
|
|
* object to the list because it's already there. */
|
2014-04-03 15:36:13 +02:00
|
|
|
CheckerComponent::CheckableSet::iterator it;
|
2014-04-05 19:02:45 +02:00
|
|
|
it = m_PendingCheckables.find(checkable);
|
2014-04-03 15:36:13 +02:00
|
|
|
if (it != m_PendingCheckables.end()) {
|
|
|
|
m_PendingCheckables.erase(it);
|
2013-09-13 07:49:12 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
if (checkable->IsActive())
|
2014-04-05 19:02:45 +02:00
|
|
|
m_IdleCheckables.insert(checkable);
|
2013-09-13 07:49:12 +02:00
|
|
|
|
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
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "CheckerComponent")
|
|
|
|
<< "Check finished for object '" << checkable->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
|
|
|
std::ostringstream msgbuf;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
msgbuf << "Pending checkables: " << m_PendingCheckables.size() << "; Idle checkables: " << m_IdleCheckables.size() << "; Checks/s: "
|
|
|
|
<< (CIB::GetActiveHostChecksStatistics(5) + CIB::GetActiveServiceChecksStatistics(5)) / 5.0;
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 13:03:44 +02:00
|
|
|
Log(LogNotice, "CheckerComponent", 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
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!Type::GetByName("Checkable")->IsAssignableFrom(object->GetReflectionType()))
|
2013-08-20 11:06:04 +02:00
|
|
|
return;
|
2013-02-21 16:12:50 +01:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2015-02-09 08:50:17 +01:00
|
|
|
Zone::Ptr zone = Zone::GetByName(checkable->GetZoneName());
|
2014-05-03 20:02:22 +02:00
|
|
|
bool same_zone = (!zone || Zone::GetLocalZone() == zone);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2014-05-09 11:32:24 +02:00
|
|
|
if (object->IsActive() && !object->IsPaused() && same_zone) {
|
2014-04-05 19:02:45 +02:00
|
|
|
if (m_PendingCheckables.find(checkable) != m_PendingCheckables.end())
|
2013-09-13 07:49:12 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
m_IdleCheckables.insert(checkable);
|
2013-09-13 07:49:12 +02:00
|
|
|
} else {
|
2014-04-05 19:02:45 +02:00
|
|
|
m_IdleCheckables.erase(checkable);
|
|
|
|
m_PendingCheckables.erase(checkable);
|
2013-09-13 07:49:12 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
void CheckerComponent::NextCheckChangedHandler(const Checkable::Ptr& checkable)
|
2013-01-22 12:44:23 +01:00
|
|
|
{
|
2013-02-21 16:12:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
/* remove and re-insert the object from the set in order to force an index update */
|
2014-04-03 15:36:13 +02:00
|
|
|
typedef boost::multi_index::nth_index<CheckableSet, 0>::type CheckableView;
|
|
|
|
CheckableView& idx = boost::get<0>(m_IdleCheckables);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
CheckableView::iterator it = idx.find(checkable);
|
2013-02-21 16:12:50 +01:00
|
|
|
if (it == idx.end())
|
|
|
|
return;
|
2013-01-22 12:44:23 +01:00
|
|
|
|
2014-04-05 19:02:45 +02:00
|
|
|
idx.erase(checkable);
|
|
|
|
idx.insert(checkable);
|
2013-02-21 16:12:50 +01:00
|
|
|
m_CV.notify_all();
|
2013-01-22 12:44:23 +01:00
|
|
|
}
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
unsigned long CheckerComponent::GetIdleCheckables(void)
|
2014-02-18 10:53:44 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
return m_IdleCheckables.size();
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
unsigned long CheckerComponent::GetPendingCheckables(void)
|
2014-02-18 10:53:44 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
return m_PendingCheckables.size();
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|