]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-flapping.cpp
Merge branch 'fix/rename-log-lib-name-6346' into next
[icinga2] / lib / icinga / checkable-flapping.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
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 "icinga/checkable.hpp"
21 #include "icinga/icingaapplication.hpp"
22 #include "base/utility.hpp"
23 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26
27 #define FLAPPING_INTERVAL (30 * 60)
28
29 double Checkable::GetFlappingCurrent(void) const
30 {
31         if (GetFlappingPositive() + GetFlappingNegative() <= 0)
32                 return 0;
33
34         return 100 * GetFlappingPositive() / (GetFlappingPositive() + GetFlappingNegative());
35 }
36
37 bool Checkable::GetEnableFlapping(void) const
38 {
39         if (!GetOverrideEnableFlapping().IsEmpty())
40                 return GetOverrideEnableFlapping();
41         else
42                 return GetEnableFlappingRaw();
43 }
44
45 void Checkable::SetEnableFlapping(bool enabled, const MessageOrigin& origin)
46 {
47         SetOverrideEnableFlapping(enabled);
48
49         OnFlappingChanged(GetSelf(), enabled ? FlappingEnabled : FlappingDisabled);
50         OnEnableFlappingChanged(GetSelf(), enabled, origin);
51 }
52
53 void Checkable::UpdateFlappingStatus(bool stateChange)
54 {
55         double ts, now;
56         long positive, negative;
57
58         now = Utility::GetTime();
59
60         ts = GetFlappingLastChange();
61         positive = GetFlappingPositive();
62         negative = GetFlappingNegative();
63
64         double diff = now - ts;
65
66         if (positive + negative > FLAPPING_INTERVAL) {
67                 double pct = (positive + negative - FLAPPING_INTERVAL) / FLAPPING_INTERVAL;
68                 positive -= pct * positive;
69                 negative -= pct * negative;
70         }
71
72         if (stateChange)
73                 positive += diff;
74         else
75                 negative += diff;
76
77         if (positive < 0)
78                 positive = 0;
79
80         if (negative < 0)
81                 negative = 0;
82
83 //      Log(LogDebug, "Checkable", "Flapping counter for '" + GetName() + "' is positive=" + Convert::ToString(positive) + ", negative=" + Convert::ToString(negative));
84
85         SetFlappingLastChange(now);
86         SetFlappingPositive(positive);
87         SetFlappingNegative(negative);
88 }
89
90 bool Checkable::IsFlapping(void) const
91 {
92         if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
93                 return false;
94         else
95                 return GetFlappingCurrent() > GetFlappingThreshold();
96 }