Merge pull request #6026 from Icinga/feature/windows-flapping

Fix flapping support for Windows
This commit is contained in:
Jean Flach 2018-02-09 15:12:36 +01:00 committed by GitHub
commit d578b742ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,28 +17,55 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/ ******************************************************************************/
#include <bitset>
#include "icinga/checkable.hpp" #include "icinga/checkable.hpp"
#include "icinga/icingaapplication.hpp" #include "icinga/icingaapplication.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
using namespace icinga; 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) void Checkable::UpdateFlappingStatus(bool stateChange)
{ {
/* TODO: Add support for Windows satellites/masters. */ Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer();
#ifndef _WIN32 /* _WIN32 */
std::bitset<20> stateChangeBuf = GetFlappingBuffer();
int oldestIndex = GetFlappingIndex(); int oldestIndex = GetFlappingIndex();
stateChangeBuf[oldestIndex] = stateChange; stateChangeBuf.Modify(oldestIndex, stateChange);
oldestIndex = (oldestIndex + 1) % 20; oldestIndex = (oldestIndex + 1) % 20;
double stateChanges = 0; double stateChanges = 0;
/* Iterate over our state array and compute a weighted total */ /* Iterate over our state array and compute a weighted total */
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
if (stateChangeBuf[(oldestIndex + i) % 20]) if (stateChangeBuf.Get((oldestIndex + i) % 20))
stateChanges += 0.8 + (0.02 * i); stateChanges += 0.8 + (0.02 * i);
} }
@ -51,15 +78,13 @@ void Checkable::UpdateFlappingStatus(bool stateChange)
else else
flapping = flappingValue > GetFlappingThresholdHigh(); flapping = flappingValue > GetFlappingThresholdHigh();
SetFlappingBuffer(stateChangeBuf.to_ulong()); SetFlappingBuffer(stateChangeBuf.GetValue());
SetFlappingIndex(oldestIndex); SetFlappingIndex(oldestIndex);
SetFlappingCurrent(flappingValue); SetFlappingCurrent(flappingValue);
SetFlapping(flapping, true); SetFlapping(flapping, true);
if (flapping != GetFlapping()) if (flapping != GetFlapping())
SetFlappingLastChange(Utility::GetTime()); SetFlappingLastChange(Utility::GetTime());
#endif /* _WIN32 */
} }
bool Checkable::IsFlapping() const bool Checkable::IsFlapping() const