]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-flapping.cpp
Merge pull request #5760 from Icinga/fix/http-client-bugs
[icinga2] / lib / icinga / checkable-flapping.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include <bitset>
21 #include "icinga/checkable.hpp"
22 #include "icinga/icingaapplication.hpp"
23 #include "base/utility.hpp"
24
25 using namespace icinga;
26
27 void Checkable::UpdateFlappingStatus(bool stateChange)
28 {
29 /* TODO: Add support for Windows satellites/masters. */
30 #ifndef _WIN32 /* _WIN32 */
31         std::bitset<20> stateChangeBuf = GetFlappingBuffer();
32         int oldestIndex = GetFlappingIndex();
33
34         stateChangeBuf[oldestIndex] = stateChange;
35         oldestIndex = (oldestIndex + 1) % 20;
36
37         double stateChanges = 0;
38
39         /* Iterate over our state array and compute a weighted total */
40         for (int i = 0; i < 20; i++) {
41                 if (stateChangeBuf[(oldestIndex + i) % 20])
42                         stateChanges += 0.8 + (0.02 * i);
43         }
44
45         double flappingValue = 100.0 * stateChanges / 20.0;
46
47         bool flapping;
48
49         if (GetFlapping())
50                 flapping = flappingValue > GetFlappingThresholdLow();
51         else
52                 flapping = flappingValue > GetFlappingThresholdHigh();
53
54         SetFlappingBuffer(stateChangeBuf.to_ulong());
55         SetFlappingIndex(oldestIndex);
56         SetFlappingCurrent(flappingValue);
57         SetFlapping(flapping, true);
58
59         if (flapping != GetFlapping())
60                 SetFlappingLastChange(Utility::GetTime());
61
62 #endif /* _WIN32 */
63 }
64
65 bool Checkable::IsFlapping(void) const
66 {
67         if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
68                 return false;
69         else
70                 return GetFlapping();
71 }