icinga2/lib/icinga/checkable-check.cpp

500 lines
14 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* 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-16 21:18:53 +01:00
#include "icinga/service.h"
#include "icinga/checkcommand.h"
2013-03-17 20:19:29 +01:00
#include "icinga/icingaapplication.h"
#include "icinga/cib.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/convert.h"
#include "base/utility.h"
#include "base/exception.h"
#include "base/context.h"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
2013-07-30 22:38:33 +02:00
#include <boost/algorithm/string/replace.hpp>
using namespace icinga;
2014-04-03 15:36:13 +02:00
boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const String&)> Checkable::OnNewCheckResult;
boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const String&)> Checkable::OnStateChange;
boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationsRequested;
boost::signals2::signal<void (const Checkable::Ptr&, double, const String&)> Checkable::OnNextCheckChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnForceNextCheckChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnForceNextNotificationChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnEnableActiveChecksChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnEnablePassiveChecksChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnEnableNotificationsChanged;
boost::signals2::signal<void (const Checkable::Ptr&, bool, const String&)> Checkable::OnEnableFlappingChanged;
boost::signals2::signal<void (const Checkable::Ptr&, FlappingState)> Checkable::OnFlappingChanged;
CheckCommand::Ptr Checkable::GetCheckCommand(void) const
{
String command;
if (!GetOverrideCheckCommand().IsEmpty())
command = GetOverrideCheckCommand();
else
command = GetCheckCommandRaw();
return CheckCommand::GetByName(command);
}
2014-04-03 15:36:13 +02:00
void Checkable::SetCheckCommand(const CheckCommand::Ptr& command)
{
SetOverrideCheckCommand(command->GetName());
}
2014-04-03 15:36:13 +02:00
TimePeriod::Ptr Checkable::GetCheckPeriod(void) const
2013-03-13 16:04:53 +01:00
{
String tp;
if (!GetOverrideCheckPeriod().IsEmpty())
tp = GetOverrideCheckPeriod();
else
tp = GetCheckPeriodRaw();
return TimePeriod::GetByName(tp);
}
2014-04-03 15:36:13 +02:00
void Checkable::SetCheckPeriod(const TimePeriod::Ptr& tp)
{
SetOverrideCheckPeriod(tp->GetName());
2013-03-13 16:04:53 +01:00
}
2014-04-03 15:36:13 +02:00
double Checkable::GetCheckInterval(void) const
{
2013-10-26 09:41:45 +02:00
if (!GetOverrideCheckInterval().IsEmpty())
return GetOverrideCheckInterval();
else
2013-10-26 09:41:45 +02:00
return GetCheckIntervalRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetCheckInterval(double interval)
{
2013-10-26 09:41:45 +02:00
SetOverrideCheckInterval(interval);
}
2014-04-03 15:36:13 +02:00
double Checkable::GetRetryInterval(void) const
{
2013-10-26 09:41:45 +02:00
if (!GetOverrideRetryInterval().IsEmpty())
return GetOverrideRetryInterval();
else
2013-10-26 09:41:45 +02:00
return GetRetryIntervalRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetRetryInterval(double interval)
{
2013-10-26 09:41:45 +02:00
SetOverrideRetryInterval(interval);
}
2014-04-03 15:36:13 +02:00
void Checkable::SetSchedulingOffset(long offset)
{
2013-02-26 10:13:54 +01:00
m_SchedulingOffset = offset;
}
2014-04-03 15:36:13 +02:00
long Checkable::GetSchedulingOffset(void)
{
2013-02-26 10:13:54 +01:00
return m_SchedulingOffset;
}
2014-04-03 15:36:13 +02:00
void Checkable::SetNextCheck(double nextCheck, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetNextCheckRaw(nextCheck);
2013-11-10 16:53:57 +01:00
OnNextCheckChanged(GetSelf(), nextCheck, authority);
}
2014-04-03 15:36:13 +02:00
double Checkable::GetNextCheck(void)
{
2013-10-26 09:41:45 +02:00
return GetNextCheckRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::UpdateNextCheck(void)
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
double interval;
if (GetStateType() == StateTypeSoft && GetLastCheckResult() != NULL)
interval = GetRetryInterval();
else
interval = GetCheckInterval();
double now = Utility::GetTime();
double adj = 0;
if (interval > 1)
adj = fmod(now * 100 + GetSchedulingOffset(), interval * 100) / 100.0;
SetNextCheck(now - adj + interval);
}
2014-04-03 15:36:13 +02:00
bool Checkable::HasBeenChecked(void) const
{
return GetLastCheckResult() != NULL;
}
2014-04-03 15:36:13 +02:00
double Checkable::GetLastCheck(void) const
{
CheckResult::Ptr cr = GetLastCheckResult();
double schedule_end = -1;
2013-10-26 09:41:45 +02:00
if (cr)
schedule_end = cr->GetScheduleEnd();
return schedule_end;
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetEnableActiveChecks(void) const
{
2013-10-26 09:41:45 +02:00
if (!GetOverrideEnableActiveChecks().IsEmpty())
return GetOverrideEnableActiveChecks();
else
2013-10-26 09:41:45 +02:00
return GetEnableActiveChecksRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetEnableActiveChecks(bool enabled, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetOverrideEnableActiveChecks(enabled);
2013-08-28 11:12:20 +02:00
2013-11-10 16:53:57 +01:00
OnEnableActiveChecksChanged(GetSelf(), enabled, authority);
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetEnablePassiveChecks(void) const
{
2013-10-26 09:41:45 +02:00
if (!GetOverrideEnablePassiveChecks().IsEmpty())
return GetOverrideEnablePassiveChecks();
else
2013-10-26 09:41:45 +02:00
return GetEnablePassiveChecksRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetEnablePassiveChecks(bool enabled, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetOverrideEnablePassiveChecks(enabled);
2013-08-28 11:12:20 +02:00
2013-11-10 16:53:57 +01:00
OnEnablePassiveChecksChanged(GetSelf(), enabled, authority);
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetForceNextCheck(void) const
{
2013-10-26 09:41:45 +02:00
return GetForceNextCheckRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetForceNextCheck(bool forced, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetForceNextCheckRaw(forced);
2013-08-28 11:12:20 +02:00
2013-11-10 16:53:57 +01:00
OnForceNextCheckChanged(GetSelf(), forced, authority);
}
2014-04-03 15:36:13 +02:00
int Checkable::GetMaxCheckAttempts(void) const
{
if (!GetOverrideMaxCheckAttempts().IsEmpty())
return GetOverrideMaxCheckAttempts();
else
return GetMaxCheckAttemptsRaw();
}
2014-04-03 15:36:13 +02:00
void Checkable::SetMaxCheckAttempts(int attempts)
{
SetOverrideMaxCheckAttempts(attempts);
}
2014-04-03 15:36:13 +02:00
void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const String& authority)
{
{
ObjectLock olock(this);
m_CheckRunning = false;
}
2013-03-19 16:20:13 +01:00
double now = Utility::GetTime();
if (cr->GetScheduleStart() == 0)
cr->SetScheduleStart(now);
2013-03-19 16:20:13 +01:00
if (cr->GetScheduleEnd() == 0)
cr->SetScheduleEnd(now);
2013-03-19 16:20:13 +01:00
if (cr->GetExecutionStart() == 0)
cr->SetExecutionStart(now);
2013-03-19 16:20:13 +01:00
if (cr->GetExecutionEnd() == 0)
cr->SetExecutionEnd(now);
2013-03-19 16:20:13 +01:00
if (authority.IsEmpty())
cr->SetCheckSource(IcingaApplication::GetInstance()->GetNodeName());
2013-03-06 11:03:50 +01:00
bool reachable = IsReachable();
bool notification_reachable = IsReachable(DependencyNotification);
2013-03-06 11:03:50 +01:00
ASSERT(!OwnsLock());
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
CheckResult::Ptr old_cr = GetLastCheckResult();
2014-04-03 15:36:13 +02:00
ServiceState old_state = GetStateRaw();
2013-03-07 12:04:20 +01:00
StateType old_stateType = GetStateType();
2013-10-26 09:41:45 +02:00
long old_attempt = GetCheckAttempt();
2013-02-24 01:10:34 +01:00
bool recovery;
if (old_cr && cr->GetExecutionStart() < old_cr->GetExecutionStart())
return;
2013-03-25 18:36:15 +01:00
/* The ExecuteCheck function already sets the old state, but we need to do it again
* in case this was a passive check result. */
2014-04-03 15:36:13 +02:00
SetLastStateRaw(old_state);
2013-03-07 12:04:20 +01:00
SetLastStateType(old_stateType);
2013-03-19 13:04:30 +01:00
SetLastReachable(reachable);
2013-03-07 12:04:20 +01:00
long attempt = 1;
if (!old_cr) {
SetStateType(StateTypeHard);
} else if (cr->GetState() == StateOK) {
if (old_state == StateOK && old_stateType == StateTypeSoft)
SetStateType(StateTypeHard); // SOFT OK -> HARD OK
2013-02-24 01:10:34 +01:00
recovery = true;
2013-07-18 17:04:09 +02:00
ResetNotificationNumbers();
SetLastStateOK(Utility::GetTime());
} else {
2013-03-19 13:04:30 +01:00
if (old_attempt >= GetMaxCheckAttempts()) {
SetStateType(StateTypeHard);
2014-04-03 15:36:13 +02:00
} else if (old_stateType == StateTypeSoft || old_state == StateOK) {
SetStateType(StateTypeSoft);
2013-03-19 13:04:30 +01:00
attempt = old_attempt + 1;
} else {
attempt = old_attempt;
}
2013-02-24 01:10:34 +01:00
recovery = false;
switch (cr->GetState()) {
case StateOK:
/* Nothing to do here. */
break;
case StateWarning:
SetLastStateWarning(Utility::GetTime());
break;
case StateCritical:
SetLastStateCritical(Utility::GetTime());
break;
case StateUnknown:
SetLastStateUnknown(Utility::GetTime());
break;
}
}
2013-07-18 18:28:23 +02:00
if (!reachable)
SetLastStateUnreachable(Utility::GetTime());
2013-10-26 09:41:45 +02:00
SetCheckAttempt(attempt);
2014-04-03 15:36:13 +02:00
ServiceState new_state = cr->GetState();
SetStateRaw(new_state);
2014-04-03 15:36:13 +02:00
bool stateChange = (old_state != new_state);
2013-06-21 10:20:29 +02:00
if (stateChange) {
SetLastStateChange(now);
/* remove acknowledgements */
if (GetAcknowledgement() == AcknowledgementNormal ||
2014-04-03 15:36:13 +02:00
(GetAcknowledgement() == AcknowledgementSticky && new_state == StateOK)) {
ClearAcknowledgement();
}
2014-04-03 15:36:13 +02:00
/* reschedule direct parents */
BOOST_FOREACH(const Checkable::Ptr& parent, GetParents()) {
if (parent.get() == this)
continue;
2013-03-04 15:52:42 +01:00
ObjectLock olock(parent);
parent->SetNextCheck(Utility::GetTime());
}
}
bool remove_acknowledgement_comments = false;
if (GetAcknowledgement() == AcknowledgementNone)
remove_acknowledgement_comments = true;
bool hardChange = (GetStateType() == StateTypeHard && old_stateType == StateTypeSoft);
2014-04-03 15:36:13 +02:00
if (stateChange && old_stateType == StateTypeHard && GetStateType() == StateTypeHard)
2013-03-21 11:37:34 +01:00
hardChange = true;
2013-10-26 09:41:45 +02:00
if (GetVolatile())
hardChange = true;
if (hardChange) {
2014-04-03 15:36:13 +02:00
SetLastHardStateRaw(new_state);
2013-03-02 09:07:47 +01:00
SetLastHardStateChange(now);
}
2013-03-02 09:07:47 +01:00
2014-04-03 15:36:13 +02:00
if (new_state != StateOK)
TriggerDowntimes();
2014-04-03 15:36:13 +02:00
Checkable::UpdateStatistics(cr);
2013-03-18 12:55:41 +01:00
bool in_downtime = IsInDowntime();
bool send_notification = hardChange && notification_reachable && !in_downtime && !IsAcknowledged();
2013-03-18 12:55:41 +01:00
if (!old_cr)
send_notification = false; /* Don't send notifications for the initial state change */
if (old_state == StateOK && old_stateType == StateTypeSoft)
send_notification = false; /* Don't send notifications for SOFT-OK -> HARD-OK. */
2013-10-26 09:41:45 +02:00
bool send_downtime_notification = (GetLastInDowntime() != in_downtime);
SetLastInDowntime(in_downtime);
2013-03-06 11:03:50 +01:00
2013-03-02 09:07:47 +01:00
olock.Unlock();
if (remove_acknowledgement_comments)
RemoveCommentsByType(CommentAcknowledgement);
Dictionary::Ptr vars_after = make_shared<Dictionary>();
2014-04-03 15:36:13 +02:00
vars_after->Set("state", new_state);
2013-03-19 13:04:30 +01:00
vars_after->Set("state_type", GetStateType());
2013-10-26 09:41:45 +02:00
vars_after->Set("attempt", GetCheckAttempt());
2013-03-19 13:04:30 +01:00
vars_after->Set("reachable", reachable);
if (old_cr)
cr->SetVarsBefore(old_cr->GetVarsAfter());
2013-03-19 13:04:30 +01:00
cr->SetVarsAfter(vars_after);
2013-03-19 13:04:30 +01:00
olock.Lock();
SetLastCheckResult(cr);
2013-06-21 10:20:29 +02:00
bool was_flapping, is_flapping;
was_flapping = IsFlapping();
if (GetStateType() == StateTypeHard)
UpdateFlappingStatus(stateChange);
2013-06-21 10:20:29 +02:00
is_flapping = IsFlapping();
olock.Unlock();
2014-04-03 15:36:13 +02:00
// Log(LogDebug, "icinga", "Flapping: Checkable " + GetName() +
// " was: " + Convert::ToString(was_flapping) +
// " is: " + Convert::ToString(is_flapping) +
// " threshold: " + Convert::ToString(GetFlappingThreshold()) +
// "% current: " + Convert::ToString(GetFlappingCurrent()) + "%.");
2013-11-10 16:53:57 +01:00
OnNewCheckResult(GetSelf(), cr, authority);
/* signal status updates to for example db_ido */
OnStateChanged(GetSelf(), authority);
2013-03-02 09:07:47 +01:00
2013-11-10 16:53:57 +01:00
if (hardChange)
OnStateChange(GetSelf(), cr, StateTypeHard, authority);
else if (stateChange)
OnStateChange(GetSelf(), cr, StateTypeSoft, authority);
2013-09-25 18:01:08 +02:00
if (GetStateType() == StateTypeSoft || hardChange || recovery)
ExecuteEventHandler();
2013-03-18 12:55:41 +01:00
if (send_downtime_notification)
OnNotificationsRequested(GetSelf(), in_downtime ? NotificationDowntimeStart : NotificationDowntimeEnd, cr, "", "");
2013-03-18 12:55:41 +01:00
if (!was_flapping && is_flapping) {
OnNotificationsRequested(GetSelf(), NotificationFlappingStart, cr, "", "");
2014-04-03 15:36:13 +02:00
Log(LogDebug, "icinga", "Flapping: Checkable " + GetName() + " started flapping (" + Convert::ToString(GetFlappingThreshold()) + "% < " + Convert::ToString(GetFlappingCurrent()) + "%).");
OnFlappingChanged(GetSelf(), FlappingStarted);
} else if (was_flapping && !is_flapping) {
OnNotificationsRequested(GetSelf(), NotificationFlappingEnd, cr, "", "");
2014-04-03 15:36:13 +02:00
Log(LogDebug, "icinga", "Flapping: Checkable " + GetName() + " stopped flapping (" + Convert::ToString(GetFlappingThreshold()) + "% >= " + Convert::ToString(GetFlappingCurrent()) + "%).");
OnFlappingChanged(GetSelf(), FlappingStopped);
} else if (send_notification)
OnNotificationsRequested(GetSelf(), recovery ? NotificationRecovery : NotificationProblem, cr, "", "");
}
2014-04-03 15:36:13 +02:00
void Checkable::ExecuteCheck(void)
{
2014-04-03 15:36:13 +02:00
CONTEXT("Executing check for object '" + GetName() + "'");
ASSERT(!OwnsLock());
2013-02-24 01:10:34 +01:00
UpdateNextCheck();
2013-03-19 14:13:58 +01:00
bool reachable = IsReachable();
2013-03-06 11:03:50 +01:00
{
ObjectLock olock(this);
/* don't run another check if there is one pending */
2013-03-25 18:36:15 +01:00
if (m_CheckRunning)
2013-03-06 11:03:50 +01:00
return;
m_CheckRunning = true;
2014-04-03 15:36:13 +02:00
SetLastStateRaw(GetStateRaw());
SetLastStateType(GetLastStateType());
2013-03-19 14:13:58 +01:00
SetLastReachable(reachable);
}
/* keep track of scheduling info in case the check type doesn't provide its own information */
2013-11-15 14:26:38 +01:00
double scheduled_start = GetNextCheck();
double before_check = Utility::GetTime();
2013-02-24 01:10:34 +01:00
2014-04-03 15:36:13 +02:00
Checkable::Ptr self = GetSelf();
2013-03-04 15:52:42 +01:00
CheckResult::Ptr result = make_shared<CheckResult>();
2013-03-25 18:36:15 +01:00
result->SetScheduleStart(scheduled_start);
result->SetExecutionStart(before_check);
GetCheckCommand()->Execute(GetSelf(), result);
}
2014-04-03 15:36:13 +02:00
void Checkable::UpdateStatistics(const CheckResult::Ptr& cr)
{
time_t ts = cr->GetScheduleEnd();
if (cr->GetActive())
CIB::UpdateActiveChecksStatistics(ts, 1);
else
CIB::UpdatePassiveChecksStatistics(ts, 1);
}
2013-02-24 01:10:34 +01:00
2014-04-03 15:36:13 +02:00
double Checkable::CalculateExecutionTime(const CheckResult::Ptr& cr)
2013-02-24 01:10:34 +01:00
{
if (!cr)
return 0;
2013-02-24 01:10:34 +01:00
return cr->GetExecutionEnd() - cr->GetExecutionStart();
2013-02-24 01:10:34 +01:00
}
2014-04-03 15:36:13 +02:00
double Checkable::CalculateLatency(const CheckResult::Ptr& cr)
2013-02-24 01:10:34 +01:00
{
if (!cr)
return 0;
2013-02-24 01:10:34 +01:00
double latency = (cr->GetScheduleEnd() - cr->GetScheduleStart()) - CalculateExecutionTime(cr);
if (latency < 0)
latency = 0;
return latency;
2013-02-24 01:10:34 +01:00
}