1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "icinga/host.hpp"
#include <bitset>
#include <iostream>
#include <BoostTestTargetConfig.h>
using namespace icinga;
#ifdef I2_DEBUG
static CheckResult::Ptr MakeCheckResult(ServiceState state)
{
CheckResult::Ptr cr = new CheckResult();
cr->SetState(state);
double now = Utility::GetTime();
cr->SetScheduleStart(now);
cr->SetScheduleEnd(now);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
Utility::IncrementTime(60);
return cr;
}
static void LogFlapping(const Checkable::Ptr& obj)
{
std::bitset<20> stateChangeBuf = obj->GetFlappingBuffer();
int oldestIndex = (obj->GetFlappingBuffer() & 0xFF00000) >> 20;
std::cout << "Flapping: " << obj->IsFlapping() << "\nHT: " << obj->GetFlappingThresholdHigh() << " LT: "
<< obj->GetFlappingThresholdLow() << "\nOur value: " << obj->GetFlappingCurrent() << "\nPtr: " << oldestIndex
<< " Buf: " << stateChangeBuf.to_ulong() << '\n';
}
static void LogHostStatus(const Host::Ptr &host)
{
std::cout << "Current status: state: " << host->GetState() << " state_type: " << host->GetStateType()
<< " check attempt: " << host->GetCheckAttempt() << "/" << host->GetMaxCheckAttempts() << " Active: " << host->IsActive() << std::endl;
}
#endif /* I2_DEBUG */
BOOST_AUTO_TEST_SUITE(icinga_checkable_flapping)
BOOST_AUTO_TEST_CASE(host_not_flapping)
{
#ifndef I2_DEBUG
BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
#else /* I2_DEBUG */
std::cout << "Running test with a non-flapping host...\n";
Host::Ptr host = new Host();
host->SetName("test");
host->SetEnableFlapping(true);
host->SetMaxCheckAttempts(5);
host->SetActive(true);
// Host otherwise is soft down
host->SetState(HostUp);
host->SetStateType(StateTypeHard);
Utility::SetTime(0);
BOOST_CHECK(host->GetFlappingCurrent() == 0);
LogFlapping(host);
LogHostStatus(host);
// watch the state being stable
int i = 0;
while (i++ < 10) {
// For some reason, elusive to me, the first check is a state change
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
BOOST_CHECK(host->GetState() == 0);
BOOST_CHECK(host->GetCheckAttempt() == 1);
BOOST_CHECK(host->GetStateType() == StateTypeHard);
//Should not be flapping
BOOST_CHECK(!host->IsFlapping());
BOOST_CHECK(host->GetFlappingCurrent() < 30.0);
}
#endif /* I2_DEBUG */
}
BOOST_AUTO_TEST_CASE(host_flapping)
{
#ifndef I2_DEBUG
BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
#else /* I2_DEBUG */
std::cout << "Running test with host changing state with every check...\n";
Host::Ptr host = new Host();
host->SetName("test");
host->SetEnableFlapping(true);
host->SetMaxCheckAttempts(5);
host->SetActive(true);
Utility::SetTime(0);
int i = 0;
while (i++ < 25) {
if (i % 2)
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
else
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
//30 Percent is our high Threshold
if (i >= 6) {
BOOST_CHECK(host->IsFlapping());
} else {
BOOST_CHECK(!host->IsFlapping());
}
}
#endif /* I2_DEBUG */
}
BOOST_AUTO_TEST_CASE(host_flapping_recover)
{
#ifndef I2_DEBUG
BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
#else /* I2_DEBUG */
std::cout << "Running test with flapping recovery...\n";
Host::Ptr host = new Host();
host->SetName("test");
host->SetEnableFlapping(true);
host->SetMaxCheckAttempts(5);
host->SetActive(true);
// Host otherwise is soft down
host->SetState(HostUp);
host->SetStateType(StateTypeHard);
Utility::SetTime(0);
// A few warning
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
for (int i = 0; i <= 7; i++) {
if (i % 2)
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
else
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
}
LogFlapping(host);
LogHostStatus(host);
// We should be flapping now
BOOST_CHECK(host->GetFlappingCurrent() > 30.0);
BOOST_CHECK(host->IsFlapping());
// Now recover from flapping
int count = 0;
while (host->IsFlapping()) {
BOOST_CHECK(host->GetFlappingCurrent() > 25.0);
BOOST_CHECK(host->IsFlapping());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
count++;
}
std::cout << "Recovered from flapping after " << count << " Warning results.\n";
BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
BOOST_CHECK(!host->IsFlapping());
#endif /* I2_DEBUG */
}
BOOST_AUTO_TEST_CASE(host_flapping_docs_example)
{
#ifndef I2_DEBUG
BOOST_WARN_MESSAGE(false, "This test can only be run in a debug build!");
#else /* I2_DEBUG */
std::cout << "Simulating the documentation example...\n";
Host::Ptr host = new Host();
host->SetName("test");
host->SetEnableFlapping(true);
host->SetMaxCheckAttempts(5);
host->SetActive(true);
// Host otherwise is soft down
host->SetState(HostUp);
host->SetStateType(StateTypeHard);
Utility::SetTime(0);
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceOK), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceWarning), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
BOOST_CHECK(host->GetFlappingCurrent() == 39.1);
BOOST_CHECK(host->IsFlapping());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
host->ProcessCheckResult(MakeCheckResult(ServiceCritical), new StoppableWaitGroup());
LogFlapping(host);
LogHostStatus(host);
BOOST_CHECK(host->GetFlappingCurrent() < 25.0);
BOOST_CHECK(!host->IsFlapping());
#endif
}
BOOST_AUTO_TEST_SUITE_END()
|