icinga2/lib/icinga/checkable-flapping.cpp

80 lines
1.5 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2013-06-21 10:20:29 +02:00
2014-05-25 16:23:35 +02:00
#include "icinga/checkable.hpp"
#include "icinga/icingaapplication.hpp"
#include "base/utility.hpp"
2013-06-21 10:20:29 +02:00
using namespace icinga;
2018-01-23 12:52:33 +01:00
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};
};
2014-04-03 15:36:13 +02:00
void Checkable::UpdateFlappingStatus(bool stateChange)
2013-06-21 10:20:29 +02:00
{
2018-01-23 12:52:33 +01:00
Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer();
int oldestIndex = GetFlappingIndex();
2013-06-21 10:20:29 +02:00
2018-01-23 12:52:33 +01:00
stateChangeBuf.Modify(oldestIndex, stateChange);
oldestIndex = (oldestIndex + 1) % 20;
2013-06-21 10:20:29 +02:00
double stateChanges = 0;
2013-06-21 10:20:29 +02:00
/* Iterate over our state array and compute a weighted total */
for (int i = 0; i < 20; i++) {
2018-01-23 12:52:33 +01:00
if (stateChangeBuf.Get((oldestIndex + i) % 20))
stateChanges += 0.8 + (0.02 * i);
2013-06-21 12:51:29 +02:00
}
2013-06-21 10:20:29 +02:00
double flappingValue = 100.0 * stateChanges / 20.0;
2013-06-21 12:51:29 +02:00
bool flapping;
if (GetFlapping())
flapping = flappingValue > GetFlappingThresholdLow();
else
flapping = flappingValue > GetFlappingThresholdHigh();
2018-01-23 12:52:33 +01:00
SetFlappingBuffer(stateChangeBuf.GetValue());
SetFlappingIndex(oldestIndex);
SetFlappingCurrent(flappingValue);
SetFlapping(flapping, true);
if (flapping != GetFlapping())
SetFlappingLastChange(Utility::GetTime());
2013-06-21 10:20:29 +02:00
}
bool Checkable::IsFlapping() const
2013-06-21 10:20:29 +02:00
{
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 GetFlapping();
2013-06-21 10:20:29 +02:00
}