]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/checkable-flapping.cpp
Merge pull request #6727 from Icinga/feature/cluster-config-sync-stage
[icinga2] / lib / icinga / checkable-flapping.cpp
index 64aa6a4906543109f9184abf65ce5d68c168d202..d0c8f8ec074faa1c8d917761a0fa13fe98fcd59e 100644 (file)
@@ -1,21 +1,4 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2016 Icinga Development Team (https://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.             *
- ******************************************************************************/
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
 
 #include "icinga/checkable.hpp"
 #include "icinga/icingaapplication.hpp"
 
 using namespace icinga;
 
-#define FLAPPING_INTERVAL (30 * 60)
-
-double Checkable::GetFlappingCurrent(void) const
+template<typename T>
+struct Bitset
 {
-       if (GetFlappingPositive() + GetFlappingNegative() <= 0)
-               return 0;
+public:
+       Bitset(T value)
+               : m_Data(value)
+       { }
+
+       void Modify(int index, bool bit)
+       {
+               if (bit)
+                       m_Data |= 1 << index;
+               else
+                       m_Data &= ~(1 << index);
+       }
 
-       return 100 * GetFlappingPositive() / (GetFlappingPositive() + GetFlappingNegative());
-}
+       bool Get(int index) const
+       {
+               return m_Data & (1 << index);
+       }
+
+       T GetValue() const
+       {
+               return m_Data;
+       }
+
+private:
+       T m_Data{0};
+};
 
 void Checkable::UpdateFlappingStatus(bool stateChange)
 {
-       double ts, now;
-       long positive, negative;
-
-       now = Utility::GetTime();
+       Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer();
+       int oldestIndex = GetFlappingIndex();
 
-       ts = GetFlappingLastChange();
-       positive = GetFlappingPositive();
-       negative = GetFlappingNegative();
+       stateChangeBuf.Modify(oldestIndex, stateChange);
+       oldestIndex = (oldestIndex + 1) % 20;
 
-       double diff = now - ts;
+       double stateChanges = 0;
 
-       if (positive + negative > FLAPPING_INTERVAL) {
-               double pct = (positive + negative - FLAPPING_INTERVAL) / FLAPPING_INTERVAL;
-               positive -= pct * positive;
-               negative -= pct * negative;
+       /* Iterate over our state array and compute a weighted total */
+       for (int i = 0; i < 20; i++) {
+               if (stateChangeBuf.Get((oldestIndex + i) % 20))
+                       stateChanges += 0.8 + (0.02 * i);
        }
 
-       if (stateChange)
-               positive += diff;
-       else
-               negative += diff;
+       double flappingValue = 100.0 * stateChanges / 20.0;
 
-       if (positive < 0)
-               positive = 0;
+       bool flapping;
 
-       if (negative < 0)
-               negative = 0;
+       if (GetFlapping())
+               flapping = flappingValue > GetFlappingThresholdLow();
+       else
+               flapping = flappingValue > GetFlappingThresholdHigh();
 
-//     Log(LogDebug, "Checkable")
-//         << "Flapping counter for '" << GetName() << "' is positive=" << positive << ", negative=" << negative;
+       SetFlappingBuffer(stateChangeBuf.GetValue());
+       SetFlappingIndex(oldestIndex);
+       SetFlappingCurrent(flappingValue);
+       SetFlapping(flapping, true);
 
-       SetFlappingLastChange(now);
-       SetFlappingPositive(positive);
-       SetFlappingNegative(negative);
+       if (flapping != GetFlapping())
+               SetFlappingLastChange(Utility::GetTime());
 }
 
-bool Checkable::IsFlapping(void) const
+bool Checkable::IsFlapping() const
 {
        if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
                return false;
        else
-               return GetFlappingCurrent() > GetFlappingThreshold();
+               return GetFlapping();
 }