]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-flapping.cpp
Merge branch 'support/2.3'
[icinga2] / lib / icinga / checkable-flapping.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 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 void Checkable::UpdateFlappingStatus(bool stateChange)
38 {
39         double ts, now;
40         long positive, negative;
41
42         now = Utility::GetTime();
43
44         ts = GetFlappingLastChange();
45         positive = GetFlappingPositive();
46         negative = GetFlappingNegative();
47
48         double diff = now - ts;
49
50         if (positive + negative > FLAPPING_INTERVAL) {
51                 double pct = (positive + negative - FLAPPING_INTERVAL) / FLAPPING_INTERVAL;
52                 positive -= pct * positive;
53                 negative -= pct * negative;
54         }
55
56         if (stateChange)
57                 positive += diff;
58         else
59                 negative += diff;
60
61         if (positive < 0)
62                 positive = 0;
63
64         if (negative < 0)
65                 negative = 0;
66
67 //      Log(LogDebug, "Checkable")
68 //          << "Flapping counter for '" << GetName() << "' is positive=" << positive << ", negative=" << negative;
69
70         SetFlappingLastChange(now);
71         SetFlappingPositive(positive);
72         SetFlappingNegative(negative);
73 }
74
75 bool Checkable::IsFlapping(void) const
76 {
77         if (!GetEnableFlapping() || !IcingaApplication::GetInstance()->GetEnableFlapping())
78                 return false;
79         else
80                 return GetFlappingCurrent() > GetFlappingThreshold();
81 }