]> 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 c8afe9052c3391536be8498c380d688c501a5f1e..d0c8f8ec074faa1c8d917761a0fa13fe98fcd59e 100644 (file)
@@ -1,44 +1,54 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
- *                                                                            *
- * 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 <bitset>
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
+
 #include "icinga/checkable.hpp"
 #include "icinga/icingaapplication.hpp"
 #include "base/utility.hpp"
 
 using namespace icinga;
 
+template<typename T>
+struct Bitset
+{
+public:
+       Bitset(T value)
+               : m_Data(value)
+       { }
+
+       void Modify(int index, bool bit)
+       {
+               if (bit)
+                       m_Data |= 1 << index;
+               else
+                       m_Data &= ~(1 << index);
+       }
+
+       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)
 {
-/* TODO: Add support for Windows satellites/masters. */
-#ifndef _WIN32 /* _WIN32 */
-       std::bitset<20> stateChangeBuf = GetFlappingBuffer();
+       Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer();
        int oldestIndex = GetFlappingIndex();
 
-       stateChangeBuf[oldestIndex] = stateChange;
+       stateChangeBuf.Modify(oldestIndex, stateChange);
        oldestIndex = (oldestIndex + 1) % 20;
 
        double stateChanges = 0;
 
        /* Iterate over our state array and compute a weighted total */
        for (int i = 0; i < 20; i++) {
-               if (stateChangeBuf[(oldestIndex + i) % 20])
+               if (stateChangeBuf.Get((oldestIndex + i) % 20))
                        stateChanges += 0.8 + (0.02 * i);
        }
 
@@ -51,18 +61,16 @@ void Checkable::UpdateFlappingStatus(bool stateChange)
        else
                flapping = flappingValue > GetFlappingThresholdHigh();
 
-       SetFlappingBuffer(stateChangeBuf.to_ulong());
+       SetFlappingBuffer(stateChangeBuf.GetValue());
        SetFlappingIndex(oldestIndex);
        SetFlappingCurrent(flappingValue);
        SetFlapping(flapping, true);
 
        if (flapping != GetFlapping())
                SetFlappingLastChange(Utility::GetTime());
-
-#endif /* _WIN32 */
 }
 
-bool Checkable::IsFlapping(void) const
+bool Checkable::IsFlapping() const
 {
        if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
                return false;