1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "icinga/host.hpp"
6 #include <BoostTestTargetConfig.h>
8 using namespace icinga;
11 static CheckResult::Ptr MakeCheckResult(ServiceState state)
13 CheckResult::Ptr cr = new CheckResult();
17 double now = Utility::GetTime();
18 cr->SetScheduleStart(now);
19 cr->SetScheduleEnd(now);
20 cr->SetExecutionStart(now);
21 cr->SetExecutionEnd(now);
23 Utility::IncrementTime(60);
28 static void LogFlapping(const Checkable::Ptr& obj)
30 std::bitset<20> stateChangeBuf = obj->GetFlappingBuffer();
31 int oldestIndex = (obj->GetFlappingBuffer() & 0xFF00000) >> 20;
33 std::cout << "Flapping: " << obj->IsFlapping() << "\nHT: " << obj->GetFlappingThresholdHigh() << " LT: "
34 << obj->GetFlappingThresholdLow() << "\nOur value: " << obj->GetFlappingCurrent() << "\nPtr: " << oldestIndex
35 << " Buf: " << stateChangeBuf.to_ulong() << '\n';
39 static void LogHostStatus(const Host::Ptr &host)
41 std::cout << "Current status: state: " << host->GetState() << " state_type: " << host->GetStateType()
42 << " check attempt: " << host->GetCheckAttempt() << "/" << host->GetMaxCheckAttempts() << " Active: " << host->IsActive() << std::endl;
46 BOOST_AUTO_TEST_SUITE(icinga_checkable_flapping)
48 BOOST_AUTO_TEST_CASE(host_not_flapping)
51 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
53 std::cout << "Running test with a non-flapping host...\n";
55 Host::Ptr host = new Host();
56 host->SetName("test");
57 host->SetEnableFlapping(true);
58 host->SetMaxCheckAttempts(5);
59 host->SetActive(true);
61 // Host otherwise is soft down
62 host->SetState(HostUp);
63 host->SetStateType(StateTypeHard);
67 BOOST_CHECK(host->GetFlappingCurrent() == 0);
72 // watch the state being stable
75 // For some reason, elusive to me, the first check is a state change
76 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
81 BOOST_CHECK(host->GetState() == 0);
82 BOOST_CHECK(host->GetCheckAttempt() == 1);
83 BOOST_CHECK(host->GetStateType() == StateTypeHard);
85 //Should not be flapping
86 BOOST_CHECK(!host->IsFlapping());
87 BOOST_CHECK(host->GetFlappingCurrent() < 30.0);
92 BOOST_AUTO_TEST_CASE(host_flapping)
95 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
97 std::cout << "Running test with host changing state with every check...\n";
99 Host::Ptr host = new Host();
100 host->SetName("test");
101 host->SetEnableFlapping(true);
102 host->SetMaxCheckAttempts(5);
103 host->SetActive(true);
110 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
112 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
117 //30 Percent is our high Threshold
119 BOOST_CHECK(host->IsFlapping());
121 BOOST_CHECK(!host->IsFlapping());
124 #endif /* I2_DEBUG */
127 BOOST_AUTO_TEST_CASE(host_flapping_recover)
130 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
132 std::cout << "Running test with flapping recovery...\n";
134 Host::Ptr host = new Host();
135 host->SetName("test");
136 host->SetEnableFlapping(true);
137 host->SetMaxCheckAttempts(5);
138 host->SetActive(true);
140 // Host otherwise is soft down
141 host->SetState(HostUp);
142 host->SetStateType(StateTypeHard);
147 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
148 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
149 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
150 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
154 for (int i = 0; i <= 7; i++) {
156 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
158 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
164 // We should be flapping now
165 BOOST_CHECK(host->GetFlappingCurrent() > 30.0);
166 BOOST_CHECK(host->IsFlapping());
168 // Now recover from flapping
170 while (host->IsFlapping()) {
171 BOOST_CHECK(host->GetFlappingCurrent() > 25.0);
172 BOOST_CHECK(host->IsFlapping());
174 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
180 std::cout << "Recovered from flapping after " << count << " Warning results.\n";
182 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
183 BOOST_CHECK(!host->IsFlapping());
184 #endif /* I2_DEBUG */
187 BOOST_AUTO_TEST_CASE(host_flapping_docs_example)
190 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
192 std::cout << "Simulating the documentation example...\n";
194 Host::Ptr host = new Host();
195 host->SetName("test");
196 host->SetEnableFlapping(true);
197 host->SetMaxCheckAttempts(5);
198 host->SetActive(true);
200 // Host otherwise is soft down
201 host->SetState(HostUp);
202 host->SetStateType(StateTypeHard);
206 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
207 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
208 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
209 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
210 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
211 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
212 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
213 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
214 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
215 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
216 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
217 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
218 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
219 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
220 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
221 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
222 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
223 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
224 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
225 host->ProcessCheckResult(MakeCheckResult(ServiceWarning));
226 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
230 BOOST_CHECK(host->GetFlappingCurrent() == 39.1);
231 BOOST_CHECK(host->IsFlapping());
233 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
234 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
235 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
236 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
237 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
238 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
239 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
243 BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
244 BOOST_CHECK(!host->IsFlapping());
248 BOOST_AUTO_TEST_SUITE_END()