From: Michael Friedrich Date: Mon, 2 Nov 2015 13:10:44 +0000 (+0100) Subject: Add host.last_state_{up,down} and last_check attribute, hide *_raw attributes X-Git-Tag: v2.4.0~104 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=43976d3989552e1425f70843b587cde13aeb74f4;p=icinga2 Add host.last_state_{up,down} and last_check attribute, hide *_raw attributes fixes #10508 fixes #10509 --- diff --git a/doc/6-object-types.md b/doc/6-object-types.md index 46a7335bd..03732b019 100644 --- a/doc/6-object-types.md +++ b/doc/6-object-types.md @@ -591,6 +591,7 @@ Runtime Attributes: Name | Type | Description --------------------------|---------------|----------------- next\_check | Number | When the next check occurs (as a UNIX timestamp). + last\_check | Number | When the last check occured (as a UNIX timestamp). check\_attempt | Number | The current check attempt number. state\_type | Number | The current state type (0 = SOFT, 1 = HARD). last\_state\_type | Number | The previous state type (0 = SOFT, 1 = HARD). @@ -606,6 +607,8 @@ Runtime Attributes: state | Number | The current state (0 = UP, 1 = DOWN). last\_state | Number | The previous state (0 = UP, 1 = DOWN). last\_hard\_state | Number | The last hard state (0 = UP, 1 = DOWN). + last_state_up | Number | When the last UP state occurred (as a UNIX timestamp). + last_state_down | Number | When the last DOWN state occurred (as a UNIX timestamp). @@ -1206,6 +1209,7 @@ Runtime Attributes: Name | Type | Description --------------------------|---------------|----------------- next\_check | Number | When the next check occurs (as a UNIX timestamp). + last\_check | Number | When the last check occured (as a UNIX timestamp). check\_attempt | Number | The current check attempt number. state\_type | Number | The current state type (0 = SOFT, 1 = HARD). last\_state\_type | Number | The previous state type (0 = SOFT, 1 = HARD). @@ -1221,6 +1225,10 @@ Runtime Attributes: state | Number | The current state (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN). last\_state | Number | The previous state (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN). last\_hard\_state | Number | The last hard state (0 = OK, 1 = WARNING, 2 = CRITICAL, 3 = UNKNOWN). + last_state_ok | Number | When the last OK state occurred (as a UNIX timestamp). + last_state_warning | Number | When the last WARNING state occurred (as a UNIX timestamp). + last_state_critical | Number | When the last CRITICAL state occurred (as a UNIX timestamp). + last_state_unknown | Number | When the last UNKNOWN state occurred (as a UNIX timestamp). ## ServiceGroup diff --git a/lib/compat/statusdatawriter.cpp b/lib/compat/statusdatawriter.cpp index d37c61355..21ff13b9e 100644 --- a/lib/compat/statusdatawriter.cpp +++ b/lib/compat/statusdatawriter.cpp @@ -344,11 +344,17 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl tie(host, service) = GetHostService(checkable); if (service) { - fp << "\t" << "current_state=" << service->GetState() << "\n" - << "\t" << "last_hard_state=" << service->GetLastHardState() << "\n"; + fp << "\t" "current_state=" << service->GetState() << "\n" + "\t" "last_hard_state=" << service->GetLastHardState() << "\n" + "\t" "last_time_ok=" << static_cast(service->GetLastStateOK()) << "\n" + "\t" "last_time_warn=" << static_cast(service->GetLastStateWarning()) << "\n" + "\t" "last_time_critical=" << static_cast(service->GetLastStateCritical()) << "\n" + "\t" "last_time_unknown=" << static_cast(service->GetLastStateUnknown()) << "\n"; } else { - fp << "\t" << "current_state=" << CompatUtility::GetHostCurrentState(host) << "\n" - << "\t" << "last_hard_state=" << host->GetLastHardState() << "\n"; + fp << "\t" "current_state=" << CompatUtility::GetHostCurrentState(host) << "\n" + "\t" "last_hard_state=" << host->GetLastHardState() << "\n" + "\t" "last_time_up=" << static_cast(host->GetLastStateUp()) << "\n" + "\t" "last_time_down=" << static_cast(host->GetLastStateDown()) << "\n"; } fp << "\t" "state_type=" << checkable->GetStateType() << "\n" @@ -366,10 +372,6 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl "\t" "max_attempts=" << checkable->GetMaxCheckAttempts() << "\n" "\t" "last_state_change=" << static_cast(checkable->GetLastStateChange()) << "\n" "\t" "last_hard_state_change=" << static_cast(checkable->GetLastHardStateChange()) << "\n" - "\t" "last_time_ok=" << static_cast(checkable->GetLastStateOK()) << "\n" - "\t" "last_time_warn=" << static_cast(checkable->GetLastStateWarning()) << "\n" - "\t" "last_time_critical=" << static_cast(checkable->GetLastStateCritical()) << "\n" - "\t" "last_time_unknown=" << static_cast(checkable->GetLastStateUnknown()) << "\n" "\t" "last_update=" << static_cast(time(NULL)) << "\n" "\t" "notifications_enabled=" << CompatUtility::GetCheckableNotificationsEnabled(checkable) << "\n" "\t" "active_checks_enabled=" << CompatUtility::GetCheckableActiveChecksEnabled(checkable) << "\n" diff --git a/lib/icinga/checkable-check.cpp b/lib/icinga/checkable-check.cpp index da1bbd7ec..17c213191 100644 --- a/lib/icinga/checkable-check.cpp +++ b/lib/icinga/checkable-check.cpp @@ -185,7 +185,7 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig recovery = true; // NOT OK -> SOFT/HARD OK ResetNotificationNumbers(); - SetLastStateOK(Utility::GetTime()); + SaveLastState(ServiceOK, Utility::GetTime()); /* update reachability for child objects in OK state */ if (!children.empty()) @@ -203,19 +203,8 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig attempt = old_attempt; } - switch (cr->GetState()) { - case ServiceOK: - /* Nothing to do here. */ - break; - case ServiceWarning: - SetLastStateWarning(Utility::GetTime()); - break; - case ServiceCritical: - SetLastStateCritical(Utility::GetTime()); - break; - case ServiceUnknown: - SetLastStateUnknown(Utility::GetTime()); - break; + if (cr->GetState() != ServiceOK) { + SaveLastState(cr->GetState(), Utility::GetTime()); } /* update reachability for child objects in NOT-OK state */ diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 58e90a9cb..cced848d7 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -92,8 +92,9 @@ public: void UpdateNextCheck(void); bool HasBeenChecked(void) const; + virtual double GetLastCheck(void) const override; - double GetLastCheck(void) const; + virtual void SaveLastState(ServiceState state, double timestamp) = 0; static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type); diff --git a/lib/icinga/checkable.ti b/lib/icinga/checkable.ti index 433de6509..51ef77425 100644 --- a/lib/icinga/checkable.ti +++ b/lib/icinga/checkable.ti @@ -101,16 +101,16 @@ abstract class Checkable : CustomVarObject [state] int check_attempt { default {{{ return 1; }}} }; - [state, enum] ServiceState state_raw { + [state, enum, no_user_view, no_user_modify] ServiceState state_raw { default {{{ return ServiceUnknown; }}} }; [state, enum] StateType state_type { default {{{ return StateTypeSoft; }}} }; - [state, enum] ServiceState last_state_raw { + [state, enum, no_user_view, no_user_modify] ServiceState last_state_raw { default {{{ return ServiceUnknown; }}} }; - [state, enum] ServiceState last_hard_state_raw { + [state, enum, no_user_view, no_user_modify] ServiceState last_hard_state_raw { default {{{ return ServiceUnknown; }}} }; [state, enum] StateType last_state_type { @@ -126,10 +126,6 @@ abstract class Checkable : CustomVarObject [state] double last_hard_state_change { default {{{ return Application::GetStartTime(); }}} }; - [state] double last_state_ok (LastStateOK); - [state] double last_state_warning; - [state] double last_state_critical; - [state] double last_state_unknown; [state] double last_state_unreachable; [state] bool last_in_downtime; [state] bool force_next_check; @@ -144,6 +140,9 @@ abstract class Checkable : CustomVarObject [no_storage, protected] bool flapping { get {{{ return false; }}} }; + [no_storage] double last_check { + get; + }; [config, navigation] name(Endpoint) command_endpoint (CommandEndpointRaw) { navigate {{{ diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 7cecf285a..1f3f9a4ee 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -173,17 +173,12 @@ HostState Host::GetLastHardState(void) const return CalculateState(GetLastHardStateRaw()); } -double Host::GetLastStateUp(void) const +void Host::SaveLastState(ServiceState state, double timestamp) { - if (GetLastStateOK() > GetLastStateWarning()) - return GetLastStateOK(); - else - return GetLastStateWarning(); -} - -double Host::GetLastStateDown(void) const -{ - return GetLastStateCritical(); + if (state == ServiceOK || state == ServiceWarning) + SetLastStateUp(timestamp); + else if (state == ServiceCritical) + SetLastStateDown(timestamp); } HostState Host::StateFromString(const String& state) diff --git a/lib/icinga/host.hpp b/lib/icinga/host.hpp index 8afa9dae5..3c25c8a47 100644 --- a/lib/icinga/host.hpp +++ b/lib/icinga/host.hpp @@ -54,8 +54,8 @@ public: virtual HostState GetState(void) const override; virtual HostState GetLastState(void) const override; virtual HostState GetLastHardState(void) const override; - double GetLastStateUp(void) const; - double GetLastStateDown(void) const; + + virtual void SaveLastState(ServiceState state, double timestamp); static HostState StateFromString(const String& state); static String StateToString(HostState state); diff --git a/lib/icinga/host.ti b/lib/icinga/host.ti index 37af43437..41998f30a 100644 --- a/lib/icinga/host.ti +++ b/lib/icinga/host.ti @@ -52,7 +52,8 @@ class Host : Checkable [enum, no_storage] HostState last_hard_state { get; }; - + [state] double last_state_up; + [state] double last_state_down; }; } diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 35a83e971..b4d778186 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -117,6 +117,18 @@ Host::Ptr Service::GetHost(void) const return m_Host; } +void Service::SaveLastState(ServiceState state, double timestamp) +{ + if (state == ServiceOK) + SetLastStateOK(timestamp); + else if (state == ServiceWarning) + SetLastStateWarning(timestamp); + else if (state == ServiceCritical) + SetLastStateCritical(timestamp); + else if (state == ServiceUnknown) + SetLastStateUnknown(timestamp); +} + ServiceState Service::StateFromString(const String& state) { if (state == "OK") diff --git a/lib/icinga/service.hpp b/lib/icinga/service.hpp index c863699ec..dfe7b9c37 100644 --- a/lib/icinga/service.hpp +++ b/lib/icinga/service.hpp @@ -45,6 +45,8 @@ public: virtual bool ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Value *result) const override; + virtual void SaveLastState(ServiceState state, double timestamp); + static ServiceState StateFromString(const String& state); static String StateToString(ServiceState state); diff --git a/lib/icinga/service.ti b/lib/icinga/service.ti index 224e5fc58..82d15dcaa 100644 --- a/lib/icinga/service.ti +++ b/lib/icinga/service.ti @@ -74,6 +74,10 @@ class Service : Checkable < ServiceNameComposer return GetLastHardStateRaw(); }}} }; + [state] double last_state_ok (LastStateOK); + [state] double last_state_warning; + [state] double last_state_critical; + [state] double last_state_unknown; }; }