1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "icinga/host.hpp"
23 #include <BoostTestTargetConfig.h>
25 using namespace icinga;
28 static CheckResult::Ptr MakeCheckResult(ServiceState state)
30 CheckResult::Ptr cr = new CheckResult();
34 double now = Utility::GetTime();
35 cr->SetScheduleStart(now);
36 cr->SetScheduleEnd(now);
37 cr->SetExecutionStart(now);
38 cr->SetExecutionEnd(now);
40 Utility::IncrementTime(60);
45 static void LogFlapping(const Checkable::Ptr& obj)
47 std::bitset<20> stateChangeBuf = obj->GetFlappingBuffer();
48 int oldestIndex = (obj->GetFlappingBuffer() & 0xFF00000) >> 20;
50 std::cout << "Flapping: " << obj->IsFlapping() << "\nHT: " << obj->GetFlappingThresholdHigh() << " LT: "
51 << obj->GetFlappingThresholdLow() << "\nOur value: " << obj->GetFlappingCurrent() << "\nPtr: " << oldestIndex
52 << " Buf: " << stateChangeBuf.to_ulong() << '\n';
56 static void LogHostStatus(const Host::Ptr &host)
58 std::cout << "Current status: state: " << host->GetState() << " state_type: " << host->GetStateType()
59 << " check attempt: " << host->GetCheckAttempt() << "/" << host->GetMaxCheckAttempts() << std::endl;
63 BOOST_AUTO_TEST_SUITE(icinga_checkable_flapping)
65 BOOST_AUTO_TEST_CASE(host_not_flapping)
68 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
70 std::cout << "Running test with a non-flapping host...\n";
72 Host::Ptr host = new Host();
73 host->SetName("test");
74 host->SetEnableFlapping(true);
75 host->SetMaxCheckAttempts(5);
77 // Host otherwise is soft down
78 host->SetState(HostUp);
79 host->SetStateType(StateTypeHard);
83 BOOST_CHECK(host->GetFlappingCurrent() == 0);
88 // watch the state being stable
91 // For some reason, elusive to me, the first check is a state change
92 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
97 BOOST_CHECK(host->GetState() == 0);
98 BOOST_CHECK(host->GetCheckAttempt() == 1);
99 BOOST_CHECK(host->GetStateType() == StateTypeHard);
101 //Should not be flapping
102 BOOST_CHECK(!host->IsFlapping());
103 BOOST_CHECK(host->GetFlappingCurrent() < 30.0);
105 #endif /* I2_DEBUG */
108 BOOST_AUTO_TEST_CASE(host_flapping)
111 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
113 std::cout << "Running test with host changing state with every check...\n";
115 Host::Ptr host = new Host();
116 host->SetName("test");
117 host->SetEnableFlapping(true);
118 host->SetMaxCheckAttempts(5);
125 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
127 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
132 //30 Percent is our high Threshold
134 BOOST_CHECK(host->IsFlapping());
136 BOOST_CHECK(!host->IsFlapping());
139 #endif /* I2_DEBUG */
142 BOOST_AUTO_TEST_CASE(host_flapping_recover)
145 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
147 std::cout << "Running test with flapping recovery...\n";
149 Host::Ptr host = new Host();
150 host->SetName("test");
151 host->SetEnableFlapping(true);
152 host->SetMaxCheckAttempts(5);
154 // Host otherwise is soft down
155 host->SetState(HostUp);
156 host->SetStateType(StateTypeHard);
161 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
162 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
163 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
164 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
168 for (int i = 0; i <= 7; i++) {
170 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
172 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
178 // We should be flapping now
179 BOOST_CHECK(host->GetFlappingCurrent() > 30.0);
180 BOOST_CHECK(host->IsFlapping());
182 // Now recover from flapping
184 while (host->IsFlapping()) {
185 BOOST_CHECK(host->GetFlappingCurrent() > 25.0);
186 BOOST_CHECK(host->IsFlapping());
188 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
194 std::cout << "Recovered from flapping after " << count << " Warning results.\n";
196 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
197 BOOST_CHECK(!host->IsFlapping());
198 #endif /* I2_DEBUG */
201 BOOST_AUTO_TEST_CASE(host_flapping_docs_example)
204 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
206 std::cout << "Simulating the documentation example...\n";
208 Host::Ptr host = new Host();
209 host->SetName("test");
210 host->SetEnableFlapping(true);
211 host->SetMaxCheckAttempts(5);
213 // Host otherwise is soft down
214 host->SetState(HostUp);
215 host->SetStateType(StateTypeHard);
219 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
220 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
221 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
222 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
223 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
224 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
225 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
226 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
227 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
228 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
229 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
230 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
231 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
232 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
233 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
234 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
235 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
236 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
237 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
238 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
239 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
243 BOOST_CHECK(host->GetFlappingCurrent() == 39.1);
244 BOOST_CHECK(host->IsFlapping());
246 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
247 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
248 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
249 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
250 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
251 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
252 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
256 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
257 BOOST_CHECK(!host->IsFlapping());
261 BOOST_AUTO_TEST_SUITE_END()