icinga2/lib/icinga/checkable-check.cpp

510 lines
15 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "icinga/checkable.hpp"
#include "icinga/service.hpp"
#include "icinga/host.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/icingaapplication.hpp"
#include "icinga/cib.hpp"
#include "icinga/clusterevents.hpp"
2014-05-25 16:23:35 +02:00
#include "remote/messageorigin.hpp"
#include "remote/apilistener.hpp"
2014-05-25 16:23:35 +02:00
#include "base/objectlock.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/convert.hpp"
#include "base/utility.hpp"
#include "base/context.hpp"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
using namespace icinga;
boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnNewCheckResult;
boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> Checkable::OnStateChange;
boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> Checkable::OnReachabilityChanged;
2014-04-03 15:36:13 +02:00
boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationsRequested;
CheckCommand::Ptr Checkable::GetCheckCommand(void) const
{
return dynamic_pointer_cast<CheckCommand>(NavigateCheckCommandRaw());
}
2014-04-03 15:36:13 +02:00
TimePeriod::Ptr Checkable::GetCheckPeriod(void) const
2013-03-13 16:04:53 +01:00
{
return TimePeriod::GetByName(GetCheckPeriodRaw());
}
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::UpdateNextCheck(void)
{
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;
}
void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
{
{
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 (!origin || origin->IsLocal()) {
Log(LogDebug, "Checkable")
<< "No origin or local origin for object '" << GetName()
<< "', setting " << IcingaApplication::GetInstance()->GetNodeName()
<< " as check_source.";
cr->SetCheckSource(IcingaApplication::GetInstance()->GetNodeName());
}
Endpoint::Ptr command_endpoint = GetCommandEndpoint();
/* override check source if command_endpoint was defined */
if (command_endpoint && !GetExtension("agent_check")) {
Log(LogDebug, "Checkable")
<< "command_endpoint found for object '" << GetName()
<< "', setting " << command_endpoint->GetName()
<< " as check_source.";
cr->SetCheckSource(command_endpoint->GetName());
}
/* agent checks go through the api */
if (command_endpoint && GetExtension("agent_check")) {
ApiListener::Ptr listener = ApiListener::GetInstance();
if (listener) {
/* send message back to its origin */
Dictionary::Ptr message = ClusterEvents::MakeCheckResultMessage(this, cr);
listener->SyncSendMessage(command_endpoint, message);
}
return;
}
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();
bool recovery = false;
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;
std::set<Checkable::Ptr> children = GetChildren();
if (!old_cr) {
SetStateType(StateTypeHard);
} else if (cr->GetState() == ServiceOK) {
if (old_state == ServiceOK && old_stateType == StateTypeSoft) {
SetStateType(StateTypeHard); // SOFT OK -> HARD OK
recovery = true;
}
if (old_state != ServiceOK)
recovery = true; // NOT OK -> SOFT/HARD OK
2013-07-18 17:04:09 +02:00
ResetNotificationNumbers();
SaveLastState(ServiceOK, Utility::GetTime());
/* update reachability for child objects in OK state */
if (!children.empty())
OnReachabilityChanged(this, cr, children, origin);
} else {
2013-03-19 13:04:30 +01:00
if (old_attempt >= GetMaxCheckAttempts()) {
SetStateType(StateTypeHard);
} else if (old_stateType == StateTypeSoft && old_state != ServiceOK) {
SetStateType(StateTypeSoft);
attempt = old_attempt + 1; //NOT-OK -> NOT-OK counter
} else if (old_state == ServiceOK) {
SetStateType(StateTypeSoft);
attempt = 1; //OK -> NOT-OK transition, reset the counter
2013-03-19 13:04:30 +01:00
} else {
attempt = old_attempt;
}
2013-02-24 01:10:34 +01:00
if (cr->GetState() != ServiceOK) {
SaveLastState(cr->GetState(), Utility::GetTime());
}
/* update reachability for child objects in NOT-OK state */
if (!children.empty())
OnReachabilityChanged(this, cr, children, origin);
}
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 ||
(GetAcknowledgement() == AcknowledgementSticky && new_state == ServiceOK)) {
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;
bool is_volatile = GetVolatile();
if (hardChange || is_volatile) {
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
if (new_state != ServiceOK)
TriggerDowntimes();
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(this);
CheckableType checkable_type = CheckableHost;
if (service)
checkable_type = CheckableService;
/* statistics for external tools */
Checkable::UpdateStatistics(cr, checkable_type);
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 == ServiceOK && old_stateType == StateTypeSoft)
send_notification = false; /* Don't send notifications for SOFT-OK -> HARD-OK. */
if (is_volatile && old_state == ServiceOK && new_state == ServiceOK)
send_notification = false; /* Don't send notifications for volatile OK -> OK changes. */
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 = new 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-10-19 17:52:17 +02:00
// Log(LogDebug, "Checkable")
// << "Flapping: Checkable " << GetName()
// << " was: " << (was_flapping)
// << " is: " << is_flapping)
// << " threshold: " << GetFlappingThreshold()
// << "% current: " + GetFlappingCurrent()) << "%.";
OnNewCheckResult(this, cr, origin);
/* signal status updates to for example db_ido */
OnStateChanged(this);
2013-03-02 09:07:47 +01:00
String old_state_str = (service ? Service::StateToString(old_state) : Host::StateToString(Host::CalculateState(old_state)));
String new_state_str = (service ? Service::StateToString(new_state) : Host::StateToString(Host::CalculateState(new_state)));
if (hardChange || is_volatile) {
OnStateChange(this, cr, StateTypeHard, origin);
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "State Change: Checkable " << GetName() << " hard state change from " << old_state_str << " to " << new_state_str << " detected." << (is_volatile ? " Checkable is volatile." : "");
} else if (stateChange) {
OnStateChange(this, cr, StateTypeSoft, origin);
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "State Change: Checkable " << GetName() << " soft state change from " << old_state_str << " to " << new_state_str << " detected.";
}
2013-09-25 18:01:08 +02:00
if (GetStateType() == StateTypeSoft || hardChange || recovery || is_volatile)
ExecuteEventHandler();
2013-03-18 12:55:41 +01:00
if (send_downtime_notification)
OnNotificationsRequested(this, in_downtime ? NotificationDowntimeStart : NotificationDowntimeEnd, cr, "", "");
2013-03-18 12:55:41 +01:00
if (!was_flapping && is_flapping) {
OnNotificationsRequested(this, NotificationFlappingStart, cr, "", "");
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "Flapping: Checkable " << GetName() << " started flapping (" << GetFlappingThreshold() << "% < " << GetFlappingCurrent() << "%).";
NotifyFlapping(origin);
} else if (was_flapping && !is_flapping) {
OnNotificationsRequested(this, NotificationFlappingEnd, cr, "", "");
2014-10-19 17:52:17 +02:00
Log(LogNotice, "Checkable")
<< "Flapping: Checkable " << GetName() << " stopped flapping (" << GetFlappingThreshold() << "% >= " << GetFlappingCurrent() << "%).";
NotifyFlapping(origin);
} else if (send_notification)
OnNotificationsRequested(this, recovery ? NotificationRecovery : NotificationProblem, cr, "", "");
}
bool Checkable::IsCheckPending(void) const
{
ObjectLock olock(this);
return m_CheckRunning;
}
void Checkable::ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros)
{
CONTEXT("Executing remote check for object '" + GetName() + "'");
double scheduled_start = GetNextCheck();
double before_check = Utility::GetTime();
CheckResult::Ptr cr = new CheckResult();
cr->SetScheduleStart(scheduled_start);
cr->SetExecutionStart(before_check);
GetCheckCommand()->Execute(this, cr, resolvedMacros, true);
}
void Checkable::ExecuteCheck()
{
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
CheckResult::Ptr cr = new CheckResult();
2013-03-25 18:36:15 +01:00
cr->SetScheduleStart(scheduled_start);
cr->SetExecutionStart(before_check);
Endpoint::Ptr endpoint = GetCommandEndpoint();
bool local = !endpoint || endpoint == Endpoint::GetLocalEndpoint();
if (local) {
GetCheckCommand()->Execute(this, cr, NULL, false);
} else {
Dictionary::Ptr macros = new Dictionary();
GetCheckCommand()->Execute(this, cr, macros, false);
if (endpoint->GetConnected()) {
/* perform check on remote endpoint */
Dictionary::Ptr message = new Dictionary();
message->Set("jsonrpc", "2.0");
message->Set("method", "event::ExecuteCommand");
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(this);
Dictionary::Ptr params = new Dictionary();
message->Set("params", params);
params->Set("command_type", "check_command");
params->Set("command", GetCheckCommand()->GetName());
params->Set("host", host->GetName());
if (service)
params->Set("service", service->GetShortName());
params->Set("macros", macros);
ApiListener::Ptr listener = ApiListener::GetInstance();
if (listener)
listener->SyncSendMessage(endpoint, message);
} else if (Application::GetInstance()->GetStartTime() < Utility::GetTime() - 30) {
/* fail to perform check on unconnected endpoint */
cr->SetState(ServiceUnknown);
cr->SetOutput("Remote Icinga instance '" + endpoint->GetName() +
"' " + "is not connected to '" + Endpoint::GetLocalEndpoint()->GetName() + "'");
ProcessCheckResult(cr);
}
{
ObjectLock olock(this);
m_CheckRunning = false;
}
}
}
void Checkable::UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type)
{
time_t ts = cr->GetScheduleEnd();
if (type == CheckableHost) {
if (cr->GetActive())
CIB::UpdateActiveHostChecksStatistics(ts, 1);
else
CIB::UpdatePassiveHostChecksStatistics(ts, 1);
} else if (type == CheckableService) {
if (cr->GetActive())
CIB::UpdateActiveServiceChecksStatistics(ts, 1);
else
CIB::UpdatePassiveServiceChecksStatistics(ts, 1);
} else {
Log(LogWarning, "Checkable", "Unknown checkable type for statistic update.");
}
}
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
}