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};
|
|
|
|
};
|
|
|
|
|
2020-11-11 17:43:30 +01:00
|
|
|
void Checkable::UpdateFlappingStatus(ServiceState newState)
|
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
|
|
|
|
2020-11-11 17:43:30 +01:00
|
|
|
ServiceState lastState = GetFlappingLastState();
|
|
|
|
bool stateChange = false;
|
|
|
|
|
|
|
|
int stateFilter = GetFlappingIgnoreStatesFilter();
|
|
|
|
|
|
|
|
/* Only count as state change if no state filter is set or the new state isn't filtered out */
|
|
|
|
if (stateFilter == -1 || !(ServiceStateToFlappingFilter(newState) & stateFilter)) {
|
|
|
|
stateChange = newState != lastState;
|
|
|
|
SetFlappingLastState(newState);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-12-04 16:06:04 +01:00
|
|
|
if (flapping != GetFlapping()) {
|
|
|
|
SetFlapping(flapping, true);
|
|
|
|
|
|
|
|
double ee = GetLastCheckResult()->GetExecutionEnd();
|
|
|
|
|
2020-03-04 14:58:32 +01:00
|
|
|
if (GetEnableFlapping() && IcingaApplication::GetInstance()->GetEnableFlapping()) {
|
|
|
|
OnFlappingChange(this, ee);
|
|
|
|
}
|
2019-12-04 16:06:04 +01:00
|
|
|
|
|
|
|
SetFlappingLastChange(ee);
|
|
|
|
}
|
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
|
|
|
}
|
2020-11-11 17:43:30 +01:00
|
|
|
|
|
|
|
int Checkable::ServiceStateToFlappingFilter(ServiceState state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case ServiceOK:
|
|
|
|
return StateFilterOK;
|
|
|
|
case ServiceWarning:
|
|
|
|
return StateFilterWarning;
|
|
|
|
case ServiceCritical:
|
|
|
|
return StateFilterCritical;
|
|
|
|
case ServiceUnknown:
|
|
|
|
return StateFilterUnknown;
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid state type.");
|
|
|
|
}
|
|
|
|
}
|