icinga2/lib/icinga/checkable-flapping.cpp
Yonas Habteab 91c7e60df8 Replace all existing copyright headers with SPDX headers
I've used the following command to replace the original copyright header
lines in a C-style comment block:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

For files that use shell-style comments (#) like CMakeLists.txt, I've
used this command:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

And for SQL files:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```
2026-02-04 14:00:05 +01:00

115 lines
2.5 KiB
C++

// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#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(ServiceState newState)
{
Bitset<unsigned long> stateChangeBuf = GetFlappingBuffer();
int oldestIndex = GetFlappingIndex();
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);
}
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.Get((oldestIndex + i) % 20))
stateChanges += 0.8 + (0.02 * i);
}
double flappingValue = 100.0 * stateChanges / 20.0;
bool flapping;
if (GetFlapping())
flapping = flappingValue > GetFlappingThresholdLow();
else
flapping = flappingValue > GetFlappingThresholdHigh();
SetFlappingBuffer(stateChangeBuf.GetValue());
SetFlappingIndex(oldestIndex);
SetFlappingCurrent(flappingValue);
if (flapping != GetFlapping()) {
SetFlapping(flapping, true);
double ee = GetLastCheckResult()->GetExecutionEnd();
if (GetEnableFlapping() && IcingaApplication::GetInstance()->GetEnableFlapping()) {
OnFlappingChange(this, ee);
}
SetFlappingLastChange(ee);
}
}
bool Checkable::IsFlapping() const
{
if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
return false;
else
return GetFlapping();
}
int Checkable::ServiceStateToFlappingFilter(ServiceState state)
{
switch (state) {
case ServiceOK:
return StateFilterOK;
case ServiceWarning:
return StateFilterWarning;
case ServiceCritical:
return StateFilterCritical;
case ServiceUnknown:
return StateFilterUnknown;
default:
ABORT("Invalid state type.");
}
}