summaryrefslogtreecommitdiffstats
path: root/lib/icinga/checkable-flapping.cpp
blob: e905e05f5ccba13988d8503e0d53415fad4a8def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#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:
			VERIFY(!"Invalid state type.");
	}
}