icinga2/lib/checker/checkercomponent.cpp

345 lines
10 KiB
C++
Raw Normal View History

2012-06-14 11:23:25 +02:00
/******************************************************************************
* Icinga 2 *
2018-10-18 09:27:04 +02:00
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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"
2018-01-18 13:50:38 +01:00
#include "checker/checkercomponent-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "icinga/icingaapplication.hpp"
#include "icinga/cib.hpp"
#include "remote/apilistener.hpp"
#include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
#include "base/objectlock.hpp"
#include "base/utility.hpp"
#include "base/perfdatavalue.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"
2012-06-14 11:23:25 +02:00
using namespace icinga;
REGISTER_TYPE(CheckerComponent);
REGISTER_STATSFUNCTION(CheckerComponent, &CheckerComponent::StatsFunc);
void CheckerComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
{
DictionaryData nodes;
for (const CheckerComponent::Ptr& checker : ConfigType::GetObjectsByType<CheckerComponent>()) {
2014-04-03 15:36:13 +02:00
unsigned long idle = checker->GetIdleCheckables();
unsigned long pending = checker->GetPendingCheckables();
nodes.emplace_back(checker->GetName(), new Dictionary({
{ "idle", idle },
{ "pending", pending }
}));
String perfdata_prefix = "checkercomponent_" + checker->GetName() + "_";
perfdata->Add(new PerfdataValue(perfdata_prefix + "idle", Convert::ToDouble(idle)));
perfdata->Add(new PerfdataValue(perfdata_prefix + "pending", Convert::ToDouble(pending)));
}
status->Set("checkercomponent", new Dictionary(std::move(nodes)));
}
void CheckerComponent::OnConfigLoaded()
2012-06-14 11:23:25 +02:00
{
ConfigObject::OnActiveChanged.connect(std::bind(&CheckerComponent::ObjectHandler, this, _1));
ConfigObject::OnPausedChanged.connect(std::bind(&CheckerComponent::ObjectHandler, this, _1));
2012-08-14 10:53:04 +02:00
Checkable::OnNextCheckChanged.connect(std::bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
}
void CheckerComponent::Start(bool runtimeCreated)
{
ObjectImpl<CheckerComponent>::Start(runtimeCreated);
Log(LogInformation, "CheckerComponent")
<< "'" << GetName() << "' started.";
2017-11-21 12:12:58 +01:00
m_Thread = std::thread(std::bind(&CheckerComponent::CheckThreadProc, this));
2012-06-14 11:23:25 +02:00
m_ResultTimer = new Timer();
2012-06-17 23:10:03 +02:00
m_ResultTimer->SetInterval(5);
m_ResultTimer->OnTimerExpired.connect(std::bind(&CheckerComponent::ResultTimerHandler, this));
2012-06-17 20:35:56 +02:00
m_ResultTimer->Start();
2012-06-14 11:23:25 +02:00
}
void CheckerComponent::Stop(bool runtimeRemoved)
2012-06-14 11:23:25 +02:00
{
Log(LogInformation, "CheckerComponent")
<< "'" << GetName() << "' stopped.";
2013-02-19 23:02:08 +01:00
{
boost::mutex::scoped_lock lock(m_Mutex);
m_Stopped = true;
m_CV.notify_all();
}
m_ResultTimer->Stop();
2013-02-19 23:02:08 +01:00
m_Thread.join();
2014-04-16 15:01:31 +02:00
ObjectImpl<CheckerComponent>::Stop(runtimeRemoved);
2012-06-14 11:23:25 +02:00
}
void CheckerComponent::CheckThreadProc()
2012-06-14 11:23:25 +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);
2013-02-21 16:12:50 +01:00
if (m_Stopped)
break;
2013-02-19 23:02:08 +01:00
auto it = idx.begin();
CheckableScheduleInfo csi = *it;
2013-02-17 19:14:34 +01:00
double wait = csi.NextCheck - Utility::GetTime();
if (Checkable::GetPendingChecks() >= GetConcurrentChecks())
wait = 0.5;
if (wait > 0) {
2013-02-18 14:40:24 +01:00
/* Wait for the next check. */
Explicitly use long with boost::posix_time In file included from lib/base/base_unity.cpp:61: lib/base/timer.cpp:295:31: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') l_TimerCV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from lib/remote/remote_unity.cpp:19: lib/remote/eventqueue.cpp:111:30: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') if (!m_CV.timed_wait(lock, boost::posix_time::milliseconds(timeout * 1000))) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from lib/checker/checker_unity.cpp:1: lib/checker/checkercomponent.cpp:128:26: error: no matching conversion for functional-style cast from 'double' to 'boost::posix_time::milliseconds' (aka 'subsecond_duration<boost::posix_time::time_duration, 1000>') m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/boost/date_time/time_duration.hpp:270:30: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'double' to 'const boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000>' for 1st argument class BOOST_SYMBOL_VISIBLE subsecond_duration : public base_duration ^ /usr/local/include/boost/date_time/time_duration.hpp:270:30: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'double' to 'boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000>' for 1st argument /usr/local/include/boost/date_time/time_duration.hpp:286:59: note: candidate template ignored: disabled by 'enable_if' [with T = double] typename boost::enable_if<boost::is_integral<T>, void>::type* = 0) : ^
2018-04-15 06:02:42 +02:00
m_CV.timed_wait(lock, boost::posix_time::milliseconds(long(wait * 1000)));
2013-02-18 14:40:24 +01:00
continue;
}
Checkable::Ptr checkable = csi.Object;
2014-04-05 19:02:45 +02:00
m_IdleCheckables.erase(checkable);
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
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.";
check = false;
}
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
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";
check = false;
}
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 '" << tp->GetName() << "'";
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) {
m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
lock.unlock();
Log(LogDebug, "CheckerComponent")
<< "Checks for checkable '" << checkable->GetName() << "' are disabled. Rescheduling check.";
2014-04-05 19:02:45 +02:00
checkable->UpdateNextCheck();
lock.lock();
2013-03-02 09:07:47 +01:00
continue;
}
csi = GetCheckableScheduleInfo(checkable);
Log(LogDebug, "CheckerComponent")
<< "Scheduling info for checkable '" << checkable->GetName() << "' ("
<< Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", checkable->GetNextCheck()) << "): Object '"
<< csi.Object->GetName() << "', Next Check: "
<< Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", csi.NextCheck) << "(" << csi.NextCheck << ").";
m_PendingCheckables.insert(csi);
lock.unlock();
if (forced) {
2014-04-05 19:02:45 +02:00
ObjectLock olock(checkable);
checkable->SetForceNextCheck(false);
2013-03-04 15:52:42 +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
Checkable::IncreasePendingChecks();
Utility::QueueAsyncCallback(std::bind(&CheckerComponent::ExecuteCheckHelper, CheckerComponent::Ptr(this), checkable));
lock.lock();
}
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) {
CheckResult::Ptr cr = new CheckResult();
cr->SetState(ServiceUnknown);
String output = "Exception occurred 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
}
Checkable::DecreasePendingChecks();
{
boost::mutex::scoped_lock lock(m_Mutex);
/* remove the object from the list of pending objects; if it's not in the
* list this was a manual (i.e. forced) check and we must not re-add the
* object to the list because it's already there. */
auto it = m_PendingCheckables.find(checkable);
if (it != m_PendingCheckables.end()) {
m_PendingCheckables.erase(it);
if (checkable->IsActive())
m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
m_CV.notify_all();
}
}
Log(LogDebug, "CheckerComponent")
<< "Check finished for object '" << checkable->GetName() << "'";
}
2012-06-20 15:23:31 +02:00
void CheckerComponent::ResultTimerHandler()
{
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 checkables: " << m_PendingCheckables.size() << "; Idle checkables: " << m_IdleCheckables.size() << "; Checks/s: "
<< (CIB::GetActiveHostChecksStatistics(60) + CIB::GetActiveServiceChecksStatistics(60)) / 60.0;
2013-02-17 19:14:34 +01:00
}
Log(LogNotice, "CheckerComponent", msgbuf.str());
2012-06-17 20:35:56 +02:00
}
void CheckerComponent::ObjectHandler(const ConfigObject::Ptr& object)
2012-06-14 11:23:25 +02:00
{
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
2013-02-21 16:12:50 +01:00
if (!checkable)
return;
Zone::Ptr zone = Zone::GetByName(checkable->GetZoneName());
bool same_zone = (!zone || Zone::GetLocalZone() == zone);
{
boost::mutex::scoped_lock lock(m_Mutex);
2012-06-14 16:09:04 +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())
return;
2012-06-14 16:09:04 +02:00
m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
} else {
2014-04-05 19:02:45 +02:00
m_IdleCheckables.erase(checkable);
m_PendingCheckables.erase(checkable);
}
2013-02-18 14:40:24 +01:00
m_CV.notify_all();
}
2012-06-14 11:23:25 +02:00
}
CheckableScheduleInfo CheckerComponent::GetCheckableScheduleInfo(const Checkable::Ptr& checkable)
{
CheckableScheduleInfo csi;
csi.Object = checkable;
csi.NextCheck = checkable->GetNextCheck();
return csi;
}
2014-04-05 19:02:45 +02:00
void CheckerComponent::NextCheckChangedHandler(const Checkable::Ptr& checkable)
{
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
auto it = idx.find(checkable);
2013-02-21 16:12:50 +01:00
if (it == idx.end())
return;
2014-04-05 19:02:45 +02:00
idx.erase(checkable);
CheckableScheduleInfo csi = GetCheckableScheduleInfo(checkable);
idx.insert(csi);
2013-02-21 16:12:50 +01:00
m_CV.notify_all();
}
unsigned long CheckerComponent::GetIdleCheckables()
{
boost::mutex::scoped_lock lock(m_Mutex);
2014-04-03 15:36:13 +02:00
return m_IdleCheckables.size();
}
unsigned long CheckerComponent::GetPendingCheckables()
{
boost::mutex::scoped_lock lock(m_Mutex);
2014-04-03 15:36:13 +02:00
return m_PendingCheckables.size();
}