icinga2/lib/icinga/service-flapping.cpp

102 lines
3.4 KiB
C++
Raw Normal View History

2013-06-21 10:20:29 +02:00
/******************************************************************************
* Icinga 2 *
2014-01-09 00:32:11 +01:00
* Copyright (C) 2012-present 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. *
******************************************************************************/
#include "icinga/service.h"
2013-10-08 11:57:35 +02:00
#include "icinga/icingaapplication.h"
2013-06-21 10:20:29 +02:00
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/timer.h"
#include "base/utility.h"
2013-06-21 12:51:29 +02:00
#include "base/convert.h"
2013-06-21 10:20:29 +02:00
#include <boost/foreach.hpp>
using namespace icinga;
#define FLAPPING_INTERVAL (30 * 60)
double Service::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());
}
bool Service::GetEnableFlapping(void) const
{
if (!GetOverrideEnableFlapping().IsEmpty())
return GetOverrideEnableFlapping();
else
return GetEnableFlappingRaw();
}
void Service::SetEnableFlapping(bool enabled, const String& authority)
{
SetOverrideEnableFlapping(enabled);
OnFlappingChanged(GetSelf(), enabled ? FlappingEnabled : FlappingDisabled);
2013-11-10 16:53:57 +01:00
OnEnableFlappingChanged(GetSelf(), enabled, authority);
}
2013-06-21 10:20:29 +02:00
void Service::UpdateFlappingStatus(bool stateChange)
{
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, "icinga", "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
}
bool Service::IsFlapping(void) const
{
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
}