1 /******************************************************************************
3 * Copyright (C) 2012-2016 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 <boost/test/unit_test.hpp>
22 #include "icinga/host.hpp"
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: " << obj->GetFlappingThresholdLow()
51 << "\nOur value: " << obj->GetFlappingCurrent() << "\nPtr: " << oldestIndex << " Buf: " << stateChangeBuf << '\n';
55 static void LogHostStatus(const Host::Ptr &host)
57 std::cout << "Current status: state: " << host->GetState() << " state_type: " << host->GetStateType()
58 << " check attempt: " << host->GetCheckAttempt() << "/" << host->GetMaxCheckAttempts() << std::endl;
62 BOOST_AUTO_TEST_SUITE(icinga_checkable_flapping)
64 BOOST_AUTO_TEST_CASE(host_not_flapping)
67 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
69 std::cout << "Running test with a non-flapping host...\n";
71 Host::Ptr host = new Host();
72 host->SetName("test");
73 host->SetEnableFlapping(true);
74 host->SetMaxCheckAttempts(5);
76 // Host otherwise is soft down
77 host->SetState(HostUp);
78 host->SetStateType(StateTypeHard);
82 BOOST_CHECK(host->GetFlappingCurrent() == 0);
87 // watch the state being stable
90 // For some reason, elusive to me, the first check is a state change
91 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
96 BOOST_CHECK(host->GetState() == 0);
97 BOOST_CHECK(host->GetCheckAttempt() == 1);
98 BOOST_CHECK(host->GetStateType() == StateTypeHard);
100 //Should not be flapping
101 BOOST_CHECK(!host->IsFlapping());
102 BOOST_CHECK(host->GetFlappingCurrent() < 30.0);
104 #endif /* I2_DEBUG */
107 BOOST_AUTO_TEST_CASE(host_flapping)
110 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
112 std::cout << "Running test with host changing state with every check...\n";
114 Host::Ptr host = new Host();
115 host->SetName("test");
116 host->SetEnableFlapping(true);
117 host->SetMaxCheckAttempts(5);
124 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
126 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
131 //30 Percent is our high Threshold
133 BOOST_CHECK(host->IsFlapping());
135 BOOST_CHECK(!host->IsFlapping());
138 #endif /* I2_DEBUG */
141 BOOST_AUTO_TEST_CASE(host_flapping_recover)
144 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
146 std::cout << "Running test with flapping recovery...\n";
148 Host::Ptr host = new Host();
149 host->SetName("test");
150 host->SetEnableFlapping(true);
151 host->SetMaxCheckAttempts(5);
153 // Host otherwise is soft down
154 host->SetState(HostUp);
155 host->SetStateType(StateTypeHard);
160 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
161 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
162 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
163 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
167 for (int i = 0; i <= 7; i++) {
169 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
171 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
177 // We should be flapping now
178 BOOST_CHECK(host->GetFlappingCurrent() > 30.0);
179 BOOST_CHECK(host->IsFlapping());
181 // Now recover from flapping
183 while (host->IsFlapping()) {
184 BOOST_CHECK(host->GetFlappingCurrent() > 25.0);
185 BOOST_CHECK(host->IsFlapping());
187 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
193 std::cout << "Recovered from flapping after " << count << " Warning results.\n";
195 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
196 BOOST_CHECK(!host->IsFlapping());
197 #endif /* I2_DEBUG */
200 BOOST_AUTO_TEST_CASE(host_flapping_docs_example)
203 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
205 std::cout << "Simulating the documentation example...\n";
207 Host::Ptr host = new Host();
208 host->SetName("test");
209 host->SetEnableFlapping(true);
210 host->SetMaxCheckAttempts(5);
212 // Host otherwise is soft down
213 host->SetState(HostUp);
214 host->SetStateType(StateTypeHard);
218 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
219 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
220 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
221 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
222 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
223 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
224 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
225 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
226 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
227 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
228 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
229 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
230 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
231 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
232 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
233 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
234 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
235 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
236 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
237 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
238 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
242 BOOST_CHECK(host->GetFlappingCurrent() == 39.1);
243 BOOST_CHECK(host->IsFlapping());
245 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
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));
255 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
256 BOOST_CHECK(!host->IsFlapping());
260 BOOST_AUTO_TEST_SUITE_END()