1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/checkable.hpp"
21 #include "icinga/icingaapplication.hpp"
22 #include "base/utility.hpp"
23 #include <boost/foreach.hpp>
25 using namespace icinga;
27 #define FLAPPING_INTERVAL (30 * 60)
29 double Checkable::GetFlappingCurrent(void) const
31 if (GetFlappingPositive() + GetFlappingNegative() <= 0)
34 return 100 * GetFlappingPositive() / (GetFlappingPositive() + GetFlappingNegative());
37 bool Checkable::GetEnableFlapping(void) const
39 if (!GetOverrideEnableFlapping().IsEmpty())
40 return GetOverrideEnableFlapping();
42 return GetEnableFlappingRaw();
45 void Checkable::SetEnableFlapping(bool enabled, const MessageOrigin& origin)
47 SetOverrideEnableFlapping(enabled);
49 OnFlappingChanged(GetSelf(), enabled ? FlappingEnabled : FlappingDisabled);
50 OnEnableFlappingChanged(GetSelf(), enabled, origin);
53 void Checkable::UpdateFlappingStatus(bool stateChange)
56 long positive, negative;
58 now = Utility::GetTime();
60 ts = GetFlappingLastChange();
61 positive = GetFlappingPositive();
62 negative = GetFlappingNegative();
64 double diff = now - ts;
66 if (positive + negative > FLAPPING_INTERVAL) {
67 double pct = (positive + negative - FLAPPING_INTERVAL) / FLAPPING_INTERVAL;
68 positive -= pct * positive;
69 negative -= pct * negative;
83 // Log(LogDebug, "Checkable", "Flapping counter for '" + GetName() + "' is positive=" + Convert::ToString(positive) + ", negative=" + Convert::ToString(negative));
85 SetFlappingLastChange(now);
86 SetFlappingPositive(positive);
87 SetFlappingNegative(negative);
90 bool Checkable::IsFlapping(void) const
92 if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
95 return GetFlappingCurrent() > GetFlappingThreshold();