diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index e052962bf..45ee60847 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -566,6 +566,45 @@ HostState Host::GetLastHardState(void) const } } +double Host::GetLastStateUp(void) const +{ + ASSERT(!OwnsLock()); + + Service::Ptr hc = GetHostCheckService(); + + if (!hc) + return 0; + + if (hc->GetLastStateOK() > hc->GetLastStateWarning()) + return hc->GetLastStateOK(); + else + return hc->GetLastStateWarning(); +} + +double Host::GetLastStateDown(void) const +{ + ASSERT(!OwnsLock()); + + Service::Ptr hc = GetHostCheckService(); + + if (!hc) + return 0; + + return hc->GetLastStateCritical(); +} + +double Host::GetLastStateUnreachable(void) const +{ + ASSERT(!OwnsLock()); + + Service::Ptr hc = GetHostCheckService(); + + if (!hc) + return 0; + + return hc->GetLastStateUnreachable(); +} + double Host::GetLastStateChange(void) const { Service::Ptr hc = GetHostCheckService(); diff --git a/lib/icinga/host.h b/lib/icinga/host.h index 85b6469a8..176cfa6c9 100644 --- a/lib/icinga/host.h +++ b/lib/icinga/host.h @@ -115,6 +115,9 @@ public: StateType GetLastStateType(void) const; double GetLastStateChange(void) const; double GetLastHardStateChange(void) const; + double GetLastStateUp(void) const; + double GetLastStateDown(void) const; + double GetLastStateUnreachable(void) const; static String StateToString(HostState state); diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp index c5ab8f22b..8677c8209 100644 --- a/lib/icinga/service-check.cpp +++ b/lib/icinga/service-check.cpp @@ -224,6 +224,76 @@ StateType Service::GetLastStateType(void) const return static_cast(ivalue); } +void Service::SetLastStateOK(double ts) +{ + m_LastStateOK = ts; + Touch("last_state_ok"); +} + +double Service::GetLastStateOK(void) const +{ + if (m_LastStateOK.IsEmpty()) + return 0; + + return m_LastStateOK; +} + +void Service::SetLastStateWarning(double ts) +{ + m_LastStateWarning = ts; + Touch("last_state_warning"); +} + +double Service::GetLastStateWarning(void) const +{ + if (m_LastStateWarning.IsEmpty()) + return 0; + + return m_LastStateWarning; +} + +void Service::SetLastStateCritical(double ts) +{ + m_LastStateCritical = ts; + Touch("last_state_critical"); +} + +double Service::GetLastStateCritical(void) const +{ + if (m_LastStateCritical.IsEmpty()) + return 0; + + return m_LastStateCritical; +} + +void Service::SetLastStateUnknown(double ts) +{ + m_LastStateUnknown = ts; + Touch("last_state_unknown"); +} + +double Service::GetLastStateUnknown(void) const +{ + if (m_LastStateUnknown.IsEmpty()) + return 0; + + return m_LastStateUnknown; +} + +void Service::SetLastStateUnreachable(double ts) +{ + m_LastStateUnreachable = ts; + Touch("last_state_unreachable"); +} + +double Service::GetLastStateUnreachable(void) const +{ + if (m_LastStateUnreachable.IsEmpty()) + return 0; + + return m_LastStateUnreachable; +} + void Service::SetLastReachable(bool reachable) { m_LastReachable = reachable; @@ -400,6 +470,9 @@ void Service::ProcessCheckResult(const Dictionary::Ptr& cr) bool reachable = IsReachable(); + if (!reachable) + SetLastStateUnreachable(Utility::GetTime()); + Host::Ptr host = GetHost(); bool host_reachable = true; @@ -430,6 +503,7 @@ void Service::ProcessCheckResult(const Dictionary::Ptr& cr) attempt = 1; recovery = true; ResetNotificationNumbers(); + SetLastStateOK(Utility::GetTime()); } else { if (old_attempt >= GetMaxCheckAttempts()) { SetStateType(StateTypeHard); @@ -442,6 +516,13 @@ void Service::ProcessCheckResult(const Dictionary::Ptr& cr) } recovery = false; + + if (cr->Get("state") == StateWarning) + SetLastStateWarning(Utility::GetTime()); + if (cr->Get("state") == StateCritical) + SetLastStateCritical(Utility::GetTime()); + if (cr->Get("state") == StateUnknown) + SetLastStateUnknown(Utility::GetTime()); } SetCurrentCheckAttempt(attempt); diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 495e52d89..57321b3aa 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -70,6 +70,11 @@ Service::Service(const Dictionary::Ptr& serializedObject) RegisterAttribute("last_result", Attribute_Replicated, &m_LastResult); RegisterAttribute("last_state_change", Attribute_Replicated, &m_LastStateChange); RegisterAttribute("last_hard_state_change", Attribute_Replicated, &m_LastHardStateChange); + RegisterAttribute("last_state_ok", Attribute_Replicated, &m_LastStateOK); + RegisterAttribute("last_state_warning", Attribute_Replicated, &m_LastStateWarning); + RegisterAttribute("last_state_critical", Attribute_Replicated, &m_LastStateCritical); + RegisterAttribute("last_state_unknown", Attribute_Replicated, &m_LastStateUnknown); + RegisterAttribute("last_state_unreachable", Attribute_Replicated, &m_LastStateUnreachable); RegisterAttribute("last_in_downtime", Attribute_Replicated, &m_LastInDowntime); RegisterAttribute("enable_active_checks", Attribute_Replicated, &m_EnableActiveChecks); RegisterAttribute("enable_passive_checks", Attribute_Replicated, &m_EnablePassiveChecks); diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 3d7911a7d..34832eb3f 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -184,6 +184,17 @@ public: void SetLastHardStateChange(double ts); double GetLastHardStateChange(void) const; + void SetLastStateOK(double ts); + double GetLastStateOK(void) const; + void SetLastStateWarning(double ts); + double GetLastStateWarning(void) const; + void SetLastStateCritical(double ts); + double GetLastStateCritical(void) const; + void SetLastStateUnknown(double ts); + double GetLastStateUnknown(void) const; + void SetLastStateUnreachable(double ts); + double GetLastStateUnreachable(void) const; + void SetLastReachable(bool reachable); bool GetLastReachable(void) const; @@ -340,6 +351,11 @@ private: Attribute m_LastResult; Attribute m_LastStateChange; Attribute m_LastHardStateChange; + Attribute m_LastStateOK; + Attribute m_LastStateWarning; + Attribute m_LastStateCritical; + Attribute m_LastStateUnknown; + Attribute m_LastStateUnreachable; Attribute m_LastInDowntime; Attribute m_EnableActiveChecks; Attribute m_EnablePassiveChecks;