2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/checkable.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "icinga/checkable-ti.cpp"
|
2015-04-20 14:16:19 +02:00
|
|
|
#include "icinga/host.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2014-12-18 15:43:01 +01:00
|
|
|
#include "base/exception.hpp"
|
2019-07-02 11:23:16 +02:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include <boost/thread/once.hpp>
|
2014-04-03 15:36:13 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2016-08-08 14:14:45 +02:00
|
|
|
REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
|
2016-08-10 12:28:41 +02:00
|
|
|
INITIALIZE_ONCE(&Checkable::StaticInitialize);
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2020-11-11 17:43:30 +01:00
|
|
|
const std::map<String, int> Checkable::m_FlappingStateFilterMap ({
|
|
|
|
{"OK", FlappingStateFilterOk},
|
|
|
|
{"Warning", FlappingStateFilterWarning},
|
|
|
|
{"Critical", FlappingStateFilterCritical},
|
|
|
|
{"Unknown", FlappingStateFilterUnknown},
|
|
|
|
{"Up", FlappingStateFilterOk},
|
|
|
|
{"Down", FlappingStateFilterCritical},
|
|
|
|
});
|
|
|
|
|
2019-12-04 15:19:03 +01:00
|
|
|
boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
|
|
|
|
boost::signals2::signal<void (const Checkable::Ptr&, const String&, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
|
2019-12-04 16:06:04 +01:00
|
|
|
boost::signals2::signal<void (const Checkable::Ptr&, double)> Checkable::OnFlappingChange;
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2019-07-02 11:23:16 +02:00
|
|
|
static Timer::Ptr l_CheckablesFireSuppressedNotifications;
|
2020-08-26 15:48:04 +02:00
|
|
|
static Timer::Ptr l_CleanDeadlinedExecutions;
|
2019-07-02 11:23:16 +02:00
|
|
|
|
2020-07-13 16:54:37 +02:00
|
|
|
thread_local std::function<void(const Value& commandLine, const ProcessResult&)> Checkable::ExecuteCommandProcessFinishedHandler;
|
2019-07-02 11:23:16 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Checkable::StaticInitialize()
|
2016-08-10 12:28:41 +02:00
|
|
|
{
|
|
|
|
/* fixed downtime start */
|
2021-01-18 14:29:05 +01:00
|
|
|
Downtime::OnDowntimeStarted.connect([](const Downtime::Ptr& downtime) { Checkable::NotifyFixedDowntimeStart(downtime); });
|
2016-08-10 12:28:41 +02:00
|
|
|
/* flexible downtime start */
|
2021-01-18 14:29:05 +01:00
|
|
|
Downtime::OnDowntimeTriggered.connect([](const Downtime::Ptr& downtime) { Checkable::NotifyFlexibleDowntimeStart(downtime); });
|
2016-08-10 12:28:41 +02:00
|
|
|
/* fixed/flexible downtime end */
|
2021-01-18 14:29:05 +01:00
|
|
|
Downtime::OnDowntimeRemoved.connect([](const Downtime::Ptr& downtime) { Checkable::NotifyDowntimeEnd(downtime); });
|
2016-08-10 12:28:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Checkable::Checkable()
|
2015-03-02 09:58:29 +01:00
|
|
|
{
|
|
|
|
SetSchedulingOffset(Utility::Random());
|
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2020-11-11 17:43:30 +01:00
|
|
|
void Checkable::OnConfigLoaded()
|
|
|
|
{
|
|
|
|
ObjectImpl<Checkable>::OnConfigLoaded();
|
|
|
|
|
|
|
|
SetFlappingIgnoreStatesFilter(FilterArrayToInt(GetFlappingIgnoreStates(), m_FlappingStateFilterMap, ~0));
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Checkable::OnAllConfigLoaded()
|
2016-08-14 22:10:30 +02:00
|
|
|
{
|
|
|
|
ObjectImpl<Checkable>::OnAllConfigLoaded();
|
|
|
|
|
|
|
|
Endpoint::Ptr endpoint = GetCommandEndpoint();
|
|
|
|
|
|
|
|
if (endpoint) {
|
|
|
|
Zone::Ptr checkableZone = static_pointer_cast<Zone>(GetZone());
|
|
|
|
|
2018-12-20 13:59:28 +01:00
|
|
|
if (checkableZone) {
|
|
|
|
Zone::Ptr cmdZone = endpoint->GetZone();
|
|
|
|
|
|
|
|
if (cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
|
|
|
|
"Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
|
2019-09-23 09:41:08 +02:00
|
|
|
"Checkable with command endpoint requires a zone. Please check the troubleshooting documentation."));
|
2016-08-14 22:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Checkable::Start(bool runtimeCreated)
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
2020-03-05 15:54:17 +01:00
|
|
|
{
|
|
|
|
auto cr (GetLastCheckResult());
|
|
|
|
|
|
|
|
if (GetLastCheckStarted() > (cr ? cr->GetExecutionEnd() : 0.0)) {
|
|
|
|
SetNextCheck(GetLastCheckStarted());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 15:50:45 +02:00
|
|
|
if (GetNextCheck() < now + 60) {
|
|
|
|
double delta = std::min(GetCheckInterval(), 60.0);
|
|
|
|
delta *= (double)std::rand() / RAND_MAX;
|
|
|
|
SetNextCheck(now + delta);
|
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Checkable>::Start(runtimeCreated);
|
2019-07-02 11:23:16 +02:00
|
|
|
|
|
|
|
static boost::once_flag once = BOOST_ONCE_INIT;
|
|
|
|
|
|
|
|
boost::call_once(once, []() {
|
2023-03-21 10:30:15 +01:00
|
|
|
l_CheckablesFireSuppressedNotifications = Timer::Create();
|
2019-07-02 11:23:16 +02:00
|
|
|
l_CheckablesFireSuppressedNotifications->SetInterval(5);
|
2022-01-28 15:15:44 +01:00
|
|
|
l_CheckablesFireSuppressedNotifications->OnTimerExpired.connect(&Checkable::FireSuppressedNotificationsTimer);
|
2019-07-02 11:23:16 +02:00
|
|
|
l_CheckablesFireSuppressedNotifications->Start();
|
2020-08-26 15:48:04 +02:00
|
|
|
|
2023-03-21 10:30:15 +01:00
|
|
|
l_CleanDeadlinedExecutions = Timer::Create();
|
2020-08-26 15:48:04 +02:00
|
|
|
l_CleanDeadlinedExecutions->SetInterval(300);
|
|
|
|
l_CleanDeadlinedExecutions->OnTimerExpired.connect(&Checkable::CleanDeadlinedExecutions);
|
|
|
|
l_CleanDeadlinedExecutions->Start();
|
2019-07-02 11:23:16 +02:00
|
|
|
});
|
2014-04-03 15:36:13 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 23:53:08 +02:00
|
|
|
void Checkable::AddGroup(const String& name)
|
|
|
|
{
|
2021-02-02 10:16:04 +01:00
|
|
|
std::unique_lock<std::mutex> lock(m_CheckableMutex);
|
2014-05-01 23:53:08 +02:00
|
|
|
|
2015-04-20 14:16:19 +02:00
|
|
|
Array::Ptr groups;
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *host = dynamic_cast<Host *>(this);
|
2015-04-20 14:16:19 +02:00
|
|
|
|
|
|
|
if (host)
|
|
|
|
groups = host->GetGroups();
|
|
|
|
else
|
|
|
|
groups = static_cast<Service *>(this)->GetGroups();
|
2014-05-01 23:53:08 +02:00
|
|
|
|
2014-05-02 00:38:46 +02:00
|
|
|
if (groups && groups->Contains(name))
|
|
|
|
return;
|
|
|
|
|
2014-05-01 23:53:08 +02:00
|
|
|
if (!groups)
|
2014-11-08 21:17:16 +01:00
|
|
|
groups = new Array();
|
2014-05-01 23:53:08 +02:00
|
|
|
|
|
|
|
groups->Add(name);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
AcknowledgementType Checkable::GetAcknowledgement()
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
|
2014-04-03 15:36:13 +02:00
|
|
|
|
|
|
|
if (avalue != AcknowledgementNone) {
|
|
|
|
double expiry = GetAcknowledgementExpiry();
|
|
|
|
|
|
|
|
if (expiry != 0 && expiry < Utility::GetTime()) {
|
|
|
|
avalue = AcknowledgementNone;
|
2019-11-28 17:46:12 +01:00
|
|
|
ClearAcknowledgement("");
|
2014-04-03 15:36:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return avalue;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Checkable::IsAcknowledged() const
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
2017-02-09 15:32:10 +01:00
|
|
|
return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
|
2014-04-03 15:36:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 15:19:03 +01:00
|
|
|
void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double changeTime, double expiry, const MessageOrigin::Ptr& origin)
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
2015-04-27 10:59:53 +02:00
|
|
|
SetAcknowledgementRaw(type);
|
|
|
|
SetAcknowledgementExpiry(expiry);
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2016-06-07 12:44:12 +02:00
|
|
|
if (notify && !IsPaused())
|
2017-11-30 08:36:35 +01:00
|
|
|
OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2019-08-06 13:28:13 +02:00
|
|
|
Log(LogInformation, "Checkable")
|
|
|
|
<< "Acknowledgement set for checkable '" << GetName() << "'.";
|
|
|
|
|
2019-12-04 15:19:03 +01:00
|
|
|
OnAcknowledgementSet(this, author, comment, type, notify, persistent, changeTime, expiry, origin);
|
|
|
|
|
|
|
|
SetAcknowledgementLastChange(changeTime);
|
2014-04-03 15:36:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 15:19:03 +01:00
|
|
|
void Checkable::ClearAcknowledgement(const String& removedBy, double changeTime, const MessageOrigin::Ptr& origin)
|
2014-04-03 15:36:13 +02:00
|
|
|
{
|
2019-12-05 11:54:33 +01:00
|
|
|
ObjectLock oLock (this);
|
|
|
|
|
2020-05-15 12:02:51 +02:00
|
|
|
bool wasAcked = GetAcknowledgementRaw() != AcknowledgementNone;
|
2019-12-05 11:54:33 +01:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
SetAcknowledgementRaw(AcknowledgementNone);
|
|
|
|
SetAcknowledgementExpiry(0);
|
|
|
|
|
2019-08-06 13:28:13 +02:00
|
|
|
Log(LogInformation, "Checkable")
|
|
|
|
<< "Acknowledgement cleared for checkable '" << GetName() << "'.";
|
|
|
|
|
2019-12-05 11:54:33 +01:00
|
|
|
if (wasAcked) {
|
2019-12-04 15:19:03 +01:00
|
|
|
OnAcknowledgementCleared(this, removedBy, changeTime, origin);
|
|
|
|
|
|
|
|
SetAcknowledgementLastChange(changeTime);
|
2019-12-05 11:54:33 +01:00
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Endpoint::Ptr Checkable::GetCommandEndpoint() const
|
2014-11-13 11:23:57 +01:00
|
|
|
{
|
|
|
|
return Endpoint::GetByName(GetCommandEndpointRaw());
|
|
|
|
}
|
2014-12-05 12:59:57 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int Checkable::GetSeverity() const
|
2017-02-09 15:32:10 +01:00
|
|
|
{
|
|
|
|
/* overridden in Host/Service class. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:34:59 +02:00
|
|
|
bool Checkable::GetProblem() const
|
|
|
|
{
|
2019-12-03 16:56:43 +01:00
|
|
|
auto cr (GetLastCheckResult());
|
|
|
|
|
|
|
|
return cr && !IsStateOK(cr->GetState());
|
2019-04-09 11:34:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Checkable::GetHandled() const
|
|
|
|
{
|
|
|
|
return GetProblem() && (IsInDowntime() || IsAcknowledged());
|
|
|
|
}
|
|
|
|
|
2019-09-24 17:54:20 +02:00
|
|
|
Timestamp Checkable::GetNextUpdate() const
|
|
|
|
{
|
|
|
|
auto cr (GetLastCheckResult());
|
2020-01-31 11:49:47 +01:00
|
|
|
double interval, latency;
|
2019-09-24 17:54:20 +02:00
|
|
|
|
2020-02-11 12:49:40 +01:00
|
|
|
// TODO: Document this behavior.
|
2019-09-24 17:54:20 +02:00
|
|
|
if (cr) {
|
2020-01-28 10:27:02 +01:00
|
|
|
interval = GetEnableActiveChecks() && GetProblem() && GetStateType() == StateTypeSoft ? GetRetryInterval() : GetCheckInterval();
|
2020-01-31 11:49:47 +01:00
|
|
|
latency = cr->GetExecutionEnd() - cr->GetScheduleStart();
|
2019-09-24 17:54:20 +02:00
|
|
|
} else {
|
2020-01-31 11:49:47 +01:00
|
|
|
interval = GetCheckInterval();
|
|
|
|
latency = 0.0;
|
2019-09-24 17:54:20 +02:00
|
|
|
}
|
2020-01-31 11:49:47 +01:00
|
|
|
|
2020-02-11 12:49:40 +01:00
|
|
|
return (GetEnableActiveChecks() ? GetNextCheck() : (cr ? cr->GetExecutionEnd() : Application::GetStartTime()) + interval) + interval + 2 * latency;
|
2019-09-24 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 12:28:41 +02:00
|
|
|
void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
|
|
|
|
{
|
|
|
|
if (!downtime->GetFixed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
NotifyDowntimeInternal(downtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
|
|
|
|
{
|
|
|
|
if (downtime->GetFixed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
NotifyDowntimeInternal(downtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
|
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = downtime->GetCheckable();
|
|
|
|
|
|
|
|
if (!checkable->IsPaused())
|
2017-11-30 08:36:35 +01:00
|
|
|
OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
|
2016-08-10 12:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
|
|
|
|
{
|
2021-07-06 12:50:44 +02:00
|
|
|
/* don't send notifications for downtimes which never triggered */
|
|
|
|
if (!downtime->IsTriggered())
|
2016-08-10 17:14:10 +02:00
|
|
|
return;
|
|
|
|
|
2016-08-10 12:28:41 +02:00
|
|
|
Checkable::Ptr checkable = downtime->GetCheckable();
|
|
|
|
|
|
|
|
if (!checkable->IsPaused())
|
2017-11-30 08:36:35 +01:00
|
|
|
OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
|
2016-08-10 12:28:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
|
2014-12-05 12:59:57 +01:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
|
2014-11-30 23:32:13 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (lvalue() <= 0)
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
|
2014-12-05 12:59:57 +01:00
|
|
|
}
|
2017-02-07 13:27:27 +01:00
|
|
|
|
2019-01-28 17:33:05 +01:00
|
|
|
void Checkable::ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
|
|
|
|
{
|
|
|
|
ObjectImpl<Checkable>::ValidateRetryInterval(lvalue, utils);
|
|
|
|
|
|
|
|
if (lvalue() <= 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "retry_interval" }, "Interval must be greater than 0."));
|
|
|
|
}
|
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
|
2017-02-07 13:27:27 +01:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
|
2017-02-07 13:27:27 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (lvalue() <= 0)
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));
|
2017-02-07 13:27:27 +01:00
|
|
|
}
|
2020-08-26 15:48:04 +02:00
|
|
|
|
|
|
|
void Checkable::CleanDeadlinedExecutions(const Timer * const&)
|
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
Dictionary::Ptr executions;
|
|
|
|
Dictionary::Ptr execution;
|
|
|
|
|
|
|
|
for (auto& host : ConfigType::GetObjectsByType<Host>()) {
|
|
|
|
executions = host->GetExecutions();
|
|
|
|
if (executions) {
|
|
|
|
for (const String& key : executions->GetKeys()) {
|
|
|
|
execution = executions->Get(key);
|
|
|
|
if (execution->Contains("deadline") && now > execution->Get("deadline")) {
|
|
|
|
executions->Remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& service : ConfigType::GetObjectsByType<Service>()) {
|
|
|
|
executions = service->GetExecutions();
|
|
|
|
if (executions) {
|
|
|
|
for (const String& key : executions->GetKeys()) {
|
|
|
|
execution = executions->Get(key);
|
|
|
|
if (execution->Contains("deadline") && now > execution->Get("deadline")) {
|
|
|
|
executions->Remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 16:41:02 +02:00
|
|
|
}
|