From 872d4895f06b545534bd78243f090b467fc29cc0 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Fri, 3 Nov 2017 17:50:59 +0100 Subject: [PATCH 1/3] Fix flapping endianness and events fixes #5720 --- lib/icinga/apievents.cpp | 3 +++ lib/icinga/checkable-flapping.cpp | 12 +++++++----- lib/icinga/checkable.hpp | 5 ++++- lib/icinga/checkable.ti | 2 ++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/icinga/apievents.cpp b/lib/icinga/apievents.cpp index 0fb38e190..7d0f5cd29 100644 --- a/lib/icinga/apievents.cpp +++ b/lib/icinga/apievents.cpp @@ -170,6 +170,9 @@ void ApiEvents::FlappingChangedHandler(const Checkable::Ptr& checkable, const Me result->Set("state", service ? static_cast(service->GetState()) : static_cast(host->GetState())); result->Set("state_type", checkable->GetStateType()); result->Set("is_flapping", checkable->IsFlapping()); + result->Set("flapping_current", checkable->GetFlappingCurrent()); + result->Set("threshold_low", checkable->GetFlappingThresholdLow()); + result->Set("threshold_high", checkable->GetFlappingThresholdHigh()); for (const EventQueue::Ptr& queue : queues) { queue->ProcessEvent(result); diff --git a/lib/icinga/checkable-flapping.cpp b/lib/icinga/checkable-flapping.cpp index af5ced050..84bba8e5b 100644 --- a/lib/icinga/checkable-flapping.cpp +++ b/lib/icinga/checkable-flapping.cpp @@ -27,13 +27,14 @@ using namespace icinga; void Checkable::UpdateFlappingStatus(bool stateChange) { std::bitset<20> stateChangeBuf = GetFlappingBuffer(); - int oldestIndex = (GetFlappingBuffer() & 0xFF00000) >> 20; + int oldestIndex = GetFlappingIndex(); stateChangeBuf[oldestIndex] = stateChange; oldestIndex = (oldestIndex + 1) % 20; double stateChanges = 0; + /* Iterate over our state array and compute a weighted total */ for (int i = 0; i < 20; i++) { if (stateChangeBuf[(oldestIndex + i) % 20]) stateChanges += 0.8 + (0.02 * i); @@ -48,12 +49,13 @@ void Checkable::UpdateFlappingStatus(bool stateChange) else flapping = flappingValue > GetFlappingThresholdHigh(); + SetFlappingBuffer(stateChangeBuf.to_ulong()); + SetFlappingIndex(oldestIndex); + SetFlappingCurrent(flappingValue); + SetFlapping(flapping, true); + if (flapping != GetFlapping()) SetFlappingLastChange(Utility::GetTime()); - - SetFlappingBuffer((stateChangeBuf.to_ulong() | (oldestIndex << 20))); - SetFlappingCurrent(flappingValue); - SetFlapping(flapping); } bool Checkable::IsFlapping(void) const diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 0617eca12..413e452d3 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -181,7 +181,6 @@ public: /* Flapping Detection */ bool IsFlapping(void) const; - void UpdateFlappingStatus(bool stateChange); /* Dependencies */ void AddDependency(const intrusive_ptr& dep); @@ -237,6 +236,10 @@ private: std::set > m_ReverseDependencies; void GetAllChildrenInternal(std::set& children, int level = 0) const; + + /* Flapping */ + void UpdateFlappingStatus(bool stateChange); + bool SuppressEvent(void) const; }; } diff --git a/lib/icinga/checkable.ti b/lib/icinga/checkable.ti index ced78b8e9..6ecf3ffaf 100644 --- a/lib/icinga/checkable.ti +++ b/lib/icinga/checkable.ti @@ -158,7 +158,9 @@ abstract class Checkable : CustomVarObject default {{{ return 0; }}} }; [state] Timestamp flapping_last_change; + [state, no_user_view, no_user_modify] int flapping_buffer; + [state, no_user_view, no_user_modify] int flapping_index; [state, protected] bool flapping; [config, navigation] name(Endpoint) command_endpoint (CommandEndpointRaw) { From 026f6d6f2ef41b6c656f165f7b40257c5ddab0e4 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 8 Nov 2017 10:17:05 +0100 Subject: [PATCH 2/3] Add documentation for new attributes in `Flapping` API event stream type --- doc/12-icinga2-api.md | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/doc/12-icinga2-api.md b/doc/12-icinga2-api.md index b22b2fccb..1f0d5b6bf 100644 --- a/doc/12-icinga2-api.md +++ b/doc/12-icinga2-api.md @@ -1300,15 +1300,18 @@ Example for all downtime events: #### Event Stream Type: Flapping - Name | Type | Description - --------------|---------------|-------------------------- - type | String | Event type `Flapping`. - timestamp | Timestamp | Unix timestamp when the event happened. - host | String | [Host](09-object-types.md#objecttype-host) name. - service | String | [Service](09-object-types.md#objecttype-service) name. Optional if this is a host flapping event. - state | Number | [Host](09-object-types.md#objecttype-host) or [service](09-object-types.md#objecttype-service) state. - state\_type | Number | [Host](09-object-types.md#objecttype-host) or [service](09-object-types.md#objecttype-service) state type. - is\_flapping | Boolean | Whether this object is flapping. + Name | Type | Description + ------------------|---------------|-------------------------- + type | String | Event type `Flapping`. + timestamp | Timestamp | Unix timestamp when the event happened. + host | String | [Host](09-object-types.md#objecttype-host) name. + service | String | [Service](09-object-types.md#objecttype-service) name. Optional if this is a host flapping event. + state | Number | [Host](09-object-types.md#objecttype-host) or [service](09-object-types.md#objecttype-service) state. + state\_type | Number | [Host](09-object-types.md#objecttype-host) or [service](09-object-types.md#objecttype-service) state type. + is\_flapping | Boolean | Whether this object is flapping. + current\_flapping | Number | Current flapping value in percent (added in 2.8). + threshold\_low | Number | Low threshold in percent (added in 2.8). + threshold\_high | Number | High threshold in percent (added in 2.8). #### Event Stream Type: AcknowledgementSet From 41d54029c82a73b5df05b36c2c595bfb147f0ade Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 8 Nov 2017 12:12:27 +0100 Subject: [PATCH 3/3] Fix log messages for flapping --- lib/icinga/checkable-check.cpp | 21 +++++++++++++-------- lib/icinga/checkable.hpp | 1 - 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/icinga/checkable-check.cpp b/lib/icinga/checkable-check.cpp index a89fa1463..893903ba4 100644 --- a/lib/icinga/checkable-check.cpp +++ b/lib/icinga/checkable-check.cpp @@ -332,12 +332,15 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig olock.Unlock(); -// Log(LogDebug, "Checkable") -// << "Flapping: Checkable " << GetName() -// << " was: " << (was_flapping) -// << " is: " << is_flapping) -// << " threshold: " << GetFlappingThreshold() -// << "% current: " + GetFlappingCurrent()) << "%."; +#ifdef I2_DEBUG /* I2_DEBUG */ + Log(LogDebug, "Checkable") + << "Flapping: Checkable " << GetName() + << " was: " << was_flapping + << " is: " << is_flapping + << " threshold low: " << GetFlappingThresholdLow() + << " threshold high: " << GetFlappingThresholdHigh() + << "% current: " << GetFlappingCurrent() << "%."; +#endif /* I2_DEBUG */ OnNewCheckResult(this, cr, origin); @@ -371,7 +374,8 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig OnNotificationsRequested(this, NotificationFlappingStart, cr, "", "", MessageOrigin::Ptr()); Log(LogNotice, "Checkable") - << "Flapping: Checkable '" << GetName() << "' started flapping (Current flapping value " << GetFlappingCurrent() << "% > threshold " << GetFlappingThresholdHigh() << "%)."; + << "Flapping Start: Checkable '" << GetName() << "' started flapping (Current flapping value " + << GetFlappingCurrent() << "% > high threshold " << GetFlappingThresholdHigh() << "%)."; NotifyFlapping(origin); } else if (!in_downtime && was_flapping && !is_flapping) { @@ -380,7 +384,8 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig OnNotificationsRequested(this, NotificationFlappingEnd, cr, "", "", MessageOrigin::Ptr()); Log(LogNotice, "Checkable") - << "Flapping: Checkable '" << GetName() << "' stopped flapping (Current flapping value " << GetFlappingCurrent() << "% < threshold " << GetFlappingThresholdLow() << "%)."; + << "Flapping Stop: Checkable '" << GetName() << "' stopped flapping (Current flapping value " + << GetFlappingCurrent() << "% < low threshold " << GetFlappingThresholdLow() << "%)."; NotifyFlapping(origin); } diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 413e452d3..4060f1d00 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -239,7 +239,6 @@ private: /* Flapping */ void UpdateFlappingStatus(bool stateChange); - bool SuppressEvent(void) const; }; }