1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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"
21 #include <BoostTestTargetConfig.h>
24 using namespace icinga;
26 BOOST_AUTO_TEST_SUITE(icinga_checkresult)
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);
43 static void NotificationHandler(const Checkable::Ptr& checkable, NotificationType type)
45 std::cout << "Notification triggered: " << Notification::NotificationTypeToString(type) << std::endl;
47 checkable->SetExtension("requested_notifications", true);
48 checkable->SetExtension("notification_type", type);
51 static void CheckNotification(const Checkable::Ptr& checkable, bool expected, NotificationType type = NotificationRecovery)
53 BOOST_CHECK((expected && checkable->GetExtension("requested_notifications").ToBool()) || (!expected && !checkable->GetExtension("requested_notifications").ToBool()));
55 if (expected && checkable->GetExtension("requested_notifications").ToBool())
56 BOOST_CHECK(checkable->GetExtension("notification_type") == type);
58 checkable->SetExtension("requested_notifications", false);
61 BOOST_AUTO_TEST_CASE(host_1attempt)
63 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
65 Host::Ptr host = new Host();
66 host->SetMaxCheckAttempts(1);
68 host->SetAuthority(true);
69 host->SetStateRaw(ServiceOK);
70 host->SetStateType(StateTypeHard);
72 std::cout << "Before first check result (ok, hard)" << std::endl;
73 BOOST_CHECK(host->GetState() == HostUp);
74 BOOST_CHECK(host->GetStateType() == StateTypeHard);
75 BOOST_CHECK(host->GetCheckAttempt() == 1);
76 CheckNotification(host, false);
78 std::cout << "First check result (unknown)" << std::endl;
79 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
80 BOOST_CHECK(host->GetState() == HostDown);
81 BOOST_CHECK(host->GetStateType() == StateTypeHard);
82 BOOST_CHECK(host->GetCheckAttempt() == 1);
83 CheckNotification(host, true, NotificationProblem);
85 std::cout << "Second check result (ok)" << std::endl;
86 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
87 BOOST_CHECK(host->GetState() == HostUp);
88 BOOST_CHECK(host->GetStateType() == StateTypeHard);
89 BOOST_CHECK(host->GetCheckAttempt() == 1);
90 CheckNotification(host, true, NotificationRecovery);
92 std::cout << "Third check result (critical)" << std::endl;
93 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
94 BOOST_CHECK(host->GetState() == HostDown);
95 BOOST_CHECK(host->GetStateType() == StateTypeHard);
96 BOOST_CHECK(host->GetCheckAttempt() == 1);
97 CheckNotification(host, true, NotificationProblem);
99 std::cout << "Fourth check result (ok)" << std::endl;
100 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
101 BOOST_CHECK(host->GetState() == HostUp);
102 BOOST_CHECK(host->GetStateType() == StateTypeHard);
103 BOOST_CHECK(host->GetCheckAttempt() == 1);
104 CheckNotification(host, true, NotificationRecovery);
109 BOOST_AUTO_TEST_CASE(host_2attempts)
111 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
113 Host::Ptr host = new Host();
114 host->SetMaxCheckAttempts(2);
116 host->SetAuthority(true);
117 host->SetStateRaw(ServiceOK);
118 host->SetStateType(StateTypeHard);
120 std::cout << "Before first check result (ok, hard)" << std::endl;
121 BOOST_CHECK(host->GetState() == HostUp);
122 BOOST_CHECK(host->GetStateType() == StateTypeHard);
123 BOOST_CHECK(host->GetCheckAttempt() == 1);
124 CheckNotification(host, false);
126 std::cout << "First check result (unknown)" << std::endl;
127 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
128 BOOST_CHECK(host->GetState() == HostDown);
129 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
130 BOOST_CHECK(host->GetCheckAttempt() == 1);
131 CheckNotification(host, false);
133 std::cout << "Second check result (critical)" << std::endl;
134 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
135 BOOST_CHECK(host->GetState() == HostDown);
136 BOOST_CHECK(host->GetStateType() == StateTypeHard);
137 BOOST_CHECK(host->GetCheckAttempt() == 1);
138 CheckNotification(host, true, NotificationProblem);
140 std::cout << "Third check result (ok)" << std::endl;
141 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
142 BOOST_CHECK(host->GetState() == HostUp);
143 BOOST_CHECK(host->GetStateType() == StateTypeHard);
144 BOOST_CHECK(host->GetCheckAttempt() == 1);
145 CheckNotification(host, true, NotificationRecovery);
147 std::cout << "Fourth check result (critical)" << std::endl;
148 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
149 BOOST_CHECK(host->GetState() == HostDown);
150 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
151 BOOST_CHECK(host->GetCheckAttempt() == 1);
152 CheckNotification(host, false);
154 std::cout << "Fifth check result (ok)" << std::endl;
155 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
156 BOOST_CHECK(host->GetState() == HostUp);
157 BOOST_CHECK(host->GetStateType() == StateTypeHard);
158 BOOST_CHECK(host->GetCheckAttempt() == 1);
159 CheckNotification(host, false);
164 BOOST_AUTO_TEST_CASE(host_3attempts)
166 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
168 Host::Ptr host = new Host();
169 host->SetMaxCheckAttempts(3);
171 host->SetAuthority(true);
172 host->SetStateRaw(ServiceOK);
173 host->SetStateType(StateTypeHard);
175 std::cout << "Before first check result (ok, hard)" << std::endl;
176 BOOST_CHECK(host->GetState() == HostUp);
177 BOOST_CHECK(host->GetStateType() == StateTypeHard);
178 BOOST_CHECK(host->GetCheckAttempt() == 1);
179 CheckNotification(host, false);
181 std::cout << "First check result (unknown)" << std::endl;
182 host->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
183 BOOST_CHECK(host->GetState() == HostDown);
184 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
185 BOOST_CHECK(host->GetCheckAttempt() == 1);
186 CheckNotification(host, false);
188 std::cout << "Second check result (critical)" << std::endl;
189 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
190 BOOST_CHECK(host->GetState() == HostDown);
191 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
192 BOOST_CHECK(host->GetCheckAttempt() == 2);
193 CheckNotification(host, false);
195 std::cout << "Third check result (critical)" << std::endl;
196 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
197 BOOST_CHECK(host->GetState() == HostDown);
198 BOOST_CHECK(host->GetStateType() == StateTypeHard);
199 BOOST_CHECK(host->GetCheckAttempt() == 1);
200 CheckNotification(host, true, NotificationProblem);
202 std::cout << "Fourth check result (ok)" << std::endl;
203 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
204 BOOST_CHECK(host->GetState() == HostUp);
205 BOOST_CHECK(host->GetStateType() == StateTypeHard);
206 BOOST_CHECK(host->GetCheckAttempt() == 1);
207 CheckNotification(host, true, NotificationRecovery);
209 std::cout << "Fifth check result (critical)" << std::endl;
210 host->ProcessCheckResult(MakeCheckResult(ServiceCritical));
211 BOOST_CHECK(host->GetState() == HostDown);
212 BOOST_CHECK(host->GetStateType() == StateTypeSoft);
213 BOOST_CHECK(host->GetCheckAttempt() == 1);
214 CheckNotification(host, false);
216 std::cout << "Sixth check result (ok)" << std::endl;
217 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
218 BOOST_CHECK(host->GetState() == HostUp);
219 BOOST_CHECK(host->GetStateType() == StateTypeHard);
220 BOOST_CHECK(host->GetCheckAttempt() == 1);
221 CheckNotification(host, false);
226 BOOST_AUTO_TEST_CASE(service_1attempt)
228 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
230 Service::Ptr service = new Service();
231 service->SetMaxCheckAttempts(1);
233 service->SetAuthority(true);
234 service->SetStateRaw(ServiceOK);
235 service->SetStateType(StateTypeHard);
237 std::cout << "Before first check result (ok, hard)" << std::endl;
238 BOOST_CHECK(service->GetState() == ServiceOK);
239 BOOST_CHECK(service->GetStateType() == StateTypeHard);
240 BOOST_CHECK(service->GetCheckAttempt() == 1);
241 CheckNotification(service, false);
243 std::cout << "First check result (unknown)" << std::endl;
244 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
245 BOOST_CHECK(service->GetState() == ServiceUnknown);
246 BOOST_CHECK(service->GetStateType() == StateTypeHard);
247 BOOST_CHECK(service->GetCheckAttempt() == 1);
248 CheckNotification(service, true, NotificationProblem);
250 std::cout << "Second check result (ok)" << std::endl;
251 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
252 BOOST_CHECK(service->GetState() == ServiceOK);
253 BOOST_CHECK(service->GetStateType() == StateTypeHard);
254 BOOST_CHECK(service->GetCheckAttempt() == 1);
255 CheckNotification(service, true, NotificationRecovery);
257 std::cout << "Third check result (critical)" << std::endl;
258 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
259 BOOST_CHECK(service->GetState() == ServiceCritical);
260 BOOST_CHECK(service->GetStateType() == StateTypeHard);
261 BOOST_CHECK(service->GetCheckAttempt() == 1);
262 CheckNotification(service, true, NotificationProblem);
264 std::cout << "Fourth check result (ok)" << std::endl;
265 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
266 BOOST_CHECK(service->GetState() == ServiceOK);
267 BOOST_CHECK(service->GetStateType() == StateTypeHard);
268 BOOST_CHECK(service->GetCheckAttempt() == 1);
269 CheckNotification(service, true, NotificationRecovery);
274 BOOST_AUTO_TEST_CASE(service_2attempts)
276 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
278 Service::Ptr service = new Service();
279 service->SetMaxCheckAttempts(2);
281 service->SetAuthority(true);
282 service->SetStateRaw(ServiceOK);
283 service->SetStateType(StateTypeHard);
285 std::cout << "Before first check result (ok, hard)" << std::endl;
286 BOOST_CHECK(service->GetState() == ServiceOK);
287 BOOST_CHECK(service->GetStateType() == StateTypeHard);
288 BOOST_CHECK(service->GetCheckAttempt() == 1);
289 CheckNotification(service, false);
291 std::cout << "First check result (unknown)" << std::endl;
292 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
293 BOOST_CHECK(service->GetState() == ServiceUnknown);
294 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
295 BOOST_CHECK(service->GetCheckAttempt() == 1);
296 CheckNotification(service, false);
298 std::cout << "Second check result (critical)" << std::endl;
299 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
300 BOOST_CHECK(service->GetState() == ServiceCritical);
301 BOOST_CHECK(service->GetStateType() == StateTypeHard);
302 BOOST_CHECK(service->GetCheckAttempt() == 1);
303 CheckNotification(service, true, NotificationProblem);
305 std::cout << "Third check result (ok)" << std::endl;
306 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
307 BOOST_CHECK(service->GetState() == ServiceOK);
308 BOOST_CHECK(service->GetStateType() == StateTypeHard);
309 BOOST_CHECK(service->GetCheckAttempt() == 1);
310 CheckNotification(service, true, NotificationRecovery);
312 std::cout << "Fourth check result (critical)" << std::endl;
313 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
314 BOOST_CHECK(service->GetState() == ServiceCritical);
315 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
316 BOOST_CHECK(service->GetCheckAttempt() == 1);
317 CheckNotification(service, false);
319 std::cout << "Fifth check result (ok)" << std::endl;
320 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
321 BOOST_CHECK(service->GetState() == ServiceOK);
322 BOOST_CHECK(service->GetStateType() == StateTypeHard);
323 BOOST_CHECK(service->GetCheckAttempt() == 1);
324 CheckNotification(service, false);
329 BOOST_AUTO_TEST_CASE(service_3attempts)
331 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
333 Service::Ptr service = new Service();
334 service->SetMaxCheckAttempts(3);
336 service->SetAuthority(true);
337 service->SetStateRaw(ServiceOK);
338 service->SetStateType(StateTypeHard);
340 std::cout << "Before first check result (ok, hard)" << std::endl;
341 BOOST_CHECK(service->GetState() == ServiceOK);
342 BOOST_CHECK(service->GetStateType() == StateTypeHard);
343 BOOST_CHECK(service->GetCheckAttempt() == 1);
344 CheckNotification(service, false);
346 std::cout << "First check result (unknown)" << std::endl;
347 service->ProcessCheckResult(MakeCheckResult(ServiceUnknown));
348 BOOST_CHECK(service->GetState() == ServiceUnknown);
349 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
350 BOOST_CHECK(service->GetCheckAttempt() == 1);
351 CheckNotification(service, false);
353 std::cout << "Second check result (critical)" << std::endl;
354 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
355 BOOST_CHECK(service->GetState() == ServiceCritical);
356 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
357 BOOST_CHECK(service->GetCheckAttempt() == 2);
358 CheckNotification(service, false);
360 std::cout << "Third check result (critical)" << std::endl;
361 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
362 BOOST_CHECK(service->GetState() == ServiceCritical);
363 BOOST_CHECK(service->GetStateType() == StateTypeHard);
364 BOOST_CHECK(service->GetCheckAttempt() == 1);
365 CheckNotification(service, true, NotificationProblem);
367 std::cout << "Fourth check result (ok)" << std::endl;
368 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
369 BOOST_CHECK(service->GetState() == ServiceOK);
370 BOOST_CHECK(service->GetStateType() == StateTypeHard);
371 BOOST_CHECK(service->GetCheckAttempt() == 1);
372 CheckNotification(service, true, NotificationRecovery);
374 std::cout << "Fifth check result (critical)" << std::endl;
375 service->ProcessCheckResult(MakeCheckResult(ServiceCritical));
376 BOOST_CHECK(service->GetState() == ServiceCritical);
377 BOOST_CHECK(service->GetStateType() == StateTypeSoft);
378 BOOST_CHECK(service->GetCheckAttempt() == 1);
379 CheckNotification(service, false);
381 std::cout << "Sixth check result (ok)" << std::endl;
382 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
383 BOOST_CHECK(service->GetState() == ServiceOK);
384 BOOST_CHECK(service->GetStateType() == StateTypeHard);
385 BOOST_CHECK(service->GetCheckAttempt() == 1);
386 CheckNotification(service, false);
391 BOOST_AUTO_TEST_CASE(host_flapping_notification)
394 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
396 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
398 int timeStepInterval = 60;
400 Host::Ptr host = new Host();
402 host->SetAuthority(true);
403 host->SetStateRaw(ServiceOK);
404 host->SetStateType(StateTypeHard);
405 host->SetEnableFlapping(true);
407 /* Initialize start time */
410 std::cout << "Before first check result (ok, hard)" << std::endl;
411 BOOST_CHECK(host->GetState() == HostUp);
412 BOOST_CHECK(host->GetStateType() == StateTypeHard);
413 BOOST_CHECK(host->GetCheckAttempt() == 1);
415 Utility::IncrementTime(timeStepInterval);
417 std::cout << "Inserting flapping check results" << std::endl;
419 for (int i = 0; i < 10; i++) {
420 ServiceState state = (i % 2 == 0 ? ServiceOK : ServiceCritical);
421 host->ProcessCheckResult(MakeCheckResult(state));
422 Utility::IncrementTime(timeStepInterval);
425 BOOST_CHECK(host->IsFlapping() == true);
427 CheckNotification(host, true, NotificationFlappingStart);
429 std::cout << "Now calm down..." << std::endl;
431 for (int i = 0; i < 20; i++) {
432 host->ProcessCheckResult(MakeCheckResult(ServiceOK));
433 Utility::IncrementTime(timeStepInterval);
436 CheckNotification(host, true, NotificationFlappingEnd);
441 #endif /* I2_DEBUG */
444 BOOST_AUTO_TEST_CASE(service_flapping_notification)
447 BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
449 boost::signals2::connection c = Checkable::OnNotificationsRequested.connect(boost::bind(&NotificationHandler, _1, _2));
451 int timeStepInterval = 60;
453 Host::Ptr service = new Host();
455 service->SetAuthority(true);
456 service->SetStateRaw(ServiceOK);
457 service->SetStateType(StateTypeHard);
458 service->SetEnableFlapping(true);
460 /* Initialize start time */
463 std::cout << "Before first check result (ok, hard)" << std::endl;
464 BOOST_CHECK(service->GetState() == HostUp);
465 BOOST_CHECK(service->GetStateType() == StateTypeHard);
466 BOOST_CHECK(service->GetCheckAttempt() == 1);
468 Utility::IncrementTime(timeStepInterval);
470 std::cout << "Inserting flapping check results" << std::endl;
472 for (int i = 0; i < 10; i++) {
473 ServiceState state = (i % 2 == 0 ? ServiceOK : ServiceCritical);
474 service->ProcessCheckResult(MakeCheckResult(state));
475 Utility::IncrementTime(timeStepInterval);
478 BOOST_CHECK(service->IsFlapping() == true);
480 CheckNotification(service, true, NotificationFlappingStart);
482 std::cout << "Now calm down..." << std::endl;
484 for (int i = 0; i < 20; i++) {
485 service->ProcessCheckResult(MakeCheckResult(ServiceOK));
486 Utility::IncrementTime(timeStepInterval);
489 CheckNotification(service, true, NotificationFlappingEnd);
493 #endif /* I2_DEBUG */
495 BOOST_AUTO_TEST_SUITE_END()