icinga2/lib/icinga/checkable-flapping.cpp

97 lines
3.2 KiB
C++
Raw Normal View History

2013-06-21 10:20:29 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
2013-06-21 10:20:29 +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 "icinga/checkable.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/utility.hpp"
2013-06-21 10:20:29 +02:00
#include <boost/foreach.hpp>
using namespace icinga;
#define FLAPPING_INTERVAL (30 * 60)
2014-04-03 15:36:13 +02:00
double Checkable::GetFlappingCurrent(void) const
{
2013-10-26 09:41:45 +02:00
if (GetFlappingPositive() + GetFlappingNegative() <= 0)
return 0;
2013-10-26 09:41:45 +02:00
return 100 * GetFlappingPositive() / (GetFlappingPositive() + GetFlappingNegative());
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetEnableFlapping(void) const
{
if (!GetOverrideEnableFlapping().IsEmpty())
return GetOverrideEnableFlapping();
else
return GetEnableFlappingRaw();
}
void Checkable::SetEnableFlapping(bool enabled, const MessageOrigin& origin)
{
SetOverrideEnableFlapping(enabled);
OnFlappingChanged(GetSelf(), enabled ? FlappingEnabled : FlappingDisabled);
OnEnableFlappingChanged(GetSelf(), enabled, origin);
}
2014-04-03 15:36:13 +02:00
void Checkable::UpdateFlappingStatus(bool stateChange)
2013-06-21 10:20:29 +02:00
{
double ts, now;
2013-06-21 12:51:29 +02:00
long positive, negative;
2013-06-21 10:20:29 +02:00
now = Utility::GetTime();
2013-10-26 09:41:45 +02:00
ts = GetFlappingLastChange();
positive = GetFlappingPositive();
negative = GetFlappingNegative();
2013-06-21 10:20:29 +02:00
double diff = now - ts;
2013-06-21 12:51:29 +02:00
if (positive + negative > FLAPPING_INTERVAL) {
double pct = (positive + negative - FLAPPING_INTERVAL) / FLAPPING_INTERVAL;
positive -= pct * positive;
negative -= pct * negative;
}
2013-06-21 10:20:29 +02:00
if (stateChange)
2013-06-21 12:51:29 +02:00
positive += diff;
else
negative += diff;
if (positive < 0)
positive = 0;
if (negative < 0)
negative = 0;
// Log(LogDebug, "Checkable", "Flapping counter for '" + GetName() + "' is positive=" + Convert::ToString(positive) + ", negative=" + Convert::ToString(negative));
2013-06-21 10:20:29 +02:00
2013-10-26 09:41:45 +02:00
SetFlappingLastChange(now);
SetFlappingPositive(positive);
SetFlappingNegative(negative);
2013-06-21 10:20:29 +02:00
}
2014-04-03 15:36:13 +02:00
bool Checkable::IsFlapping(void) const
2013-06-21 10:20:29 +02:00
{
2013-10-08 11:57:35 +02:00
if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
2013-08-29 14:13:18 +02:00
return false;
else
return GetFlappingCurrent() > GetFlappingThreshold();
2013-06-21 10:20:29 +02:00
}