2019-02-25 14:48:22 +01:00
|
|
|
/* 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();
|
2017-11-03 17:50:59 +01:00
|
|
|
int oldestIndex = GetFlappingIndex();
|
2013-06-21 10:20:29 +02:00
|
|
|
|
2018-01-23 12:52:33 +01:00
|
|
|
stateChangeBuf.Modify(oldestIndex, stateChange);
|
2017-10-19 17:32:52 +02:00
|
|
|
oldestIndex = (oldestIndex + 1) % 20;
|
2013-06-21 10:20:29 +02:00
|
|
|
|
2017-10-19 17:32:52 +02:00
|
|
|
double stateChanges = 0;
|
2013-06-21 10:20:29 +02:00
|
|
|
|
2017-11-03 17:50:59 +01:00
|
|
|
/* Iterate over our state array and compute a weighted total */
|
2017-10-19 17:32:52 +02:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2018-01-23 12:52:33 +01:00
|
|
|
if (stateChangeBuf.Get((oldestIndex + i) % 20))
|
2017-10-19 17:32:52 +02:00
|
|
|
stateChanges += 0.8 + (0.02 * i);
|
2013-06-21 12:51:29 +02:00
|
|
|
}
|
2013-06-21 10:20:29 +02:00
|
|
|
|
2017-10-19 17:32:52 +02:00
|
|
|
double flappingValue = 100.0 * stateChanges / 20.0;
|
2013-06-21 12:51:29 +02:00
|
|
|
|
2017-10-19 17:32:52 +02:00
|
|
|
bool flapping;
|
2013-06-26 08:52:06 +02:00
|
|
|
|
2017-10-19 17:32:52 +02:00
|
|
|
if (GetFlapping())
|
|
|
|
flapping = flappingValue > GetFlappingThresholdLow();
|
|
|
|
else
|
|
|
|
flapping = flappingValue > GetFlappingThresholdHigh();
|
2013-06-26 08:52:06 +02:00
|
|
|
|
2018-01-23 12:52:33 +01:00
|
|
|
SetFlappingBuffer(stateChangeBuf.GetValue());
|
2017-11-03 17:50:59 +01:00
|
|
|
SetFlappingIndex(oldestIndex);
|
|
|
|
SetFlappingCurrent(flappingValue);
|
|
|
|
SetFlapping(flapping, true);
|
|
|
|
|
2017-10-19 17:32:52 +02:00
|
|
|
if (flapping != GetFlapping())
|
|
|
|
SetFlappingLastChange(Utility::GetTime());
|
2013-06-21 10:20:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01: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
|
2017-10-19 17:32:52 +02:00
|
|
|
return GetFlapping();
|
2013-06-21 10:20:29 +02:00
|
|
|
}
|