From ae8a0b6a6437e4f997db05f3cc134b927f1925ea Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 31 Jan 2020 16:49:49 +0100 Subject: [PATCH 1/2] Introduce Application::GetUptime() --- lib/base/application.cpp | 5 +++++ lib/base/application.hpp | 2 ++ lib/icinga/cib.cpp | 2 +- lib/methods/icingachecktask.cpp | 2 +- lib/methods/randomchecktask.cpp | 2 +- 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 4707f0d23..9468bb9f1 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -1162,6 +1162,11 @@ void Application::SetMainTime(double ts) m_MainTime = ts; } +double Application::GetUptime() +{ + return Utility::GetTime() - m_StartTime; +} + bool Application::GetScriptDebuggerEnabled() { return m_ScriptDebuggerEnabled; diff --git a/lib/base/application.hpp b/lib/base/application.hpp index a6b78ff09..e2455e3e6 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -98,6 +98,8 @@ public: static double GetMainTime(); static void SetMainTime(double ts); + static double GetUptime(); + static bool GetScriptDebuggerEnabled(); static void SetScriptDebuggerEnabled(bool enabled); diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index 0cb6ef0d5..898b0e75f 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -329,7 +329,7 @@ void CIB::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata) { status->Set("num_services_handled", ss.services_handled); status->Set("num_services_problem", ss.services_problem); - double uptime = Utility::GetTime() - Application::GetStartTime(); + double uptime = Application::GetUptime(); status->Set("uptime", uptime); HostStatistics hs = CalculateHostStats(); diff --git a/lib/methods/icingachecktask.cpp b/lib/methods/icingachecktask.cpp index 3f46fba81..2c2c5c592 100644 --- a/lib/methods/icingachecktask.cpp +++ b/lib/methods/icingachecktask.cpp @@ -102,7 +102,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes perfdata->Add(new PerfdataValue("num_services_handled", ss.services_handled)); perfdata->Add(new PerfdataValue("num_services_problem", ss.services_problem)); - double uptime = Utility::GetTime() - Application::GetStartTime(); + double uptime = Application::GetUptime(); perfdata->Add(new PerfdataValue("uptime", uptime)); HostStatistics hs = CIB::CalculateHostStats(); diff --git a/lib/methods/randomchecktask.cpp b/lib/methods/randomchecktask.cpp index ddda9879b..35e10d2a6 100644 --- a/lib/methods/randomchecktask.cpp +++ b/lib/methods/randomchecktask.cpp @@ -25,7 +25,7 @@ void RandomCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes return; double now = Utility::GetTime(); - double uptime = now - Application::GetStartTime(); + double uptime = Application::GetUptime(); String output = "Hello from " + IcingaApplication::GetInstance()->GetNodeName() + ". Icinga 2 has been running for " + Utility::FormatDuration(uptime) From d53eb345200832cea6c7fc6ddf3c4d7646e388fc Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 11 Feb 2020 12:49:40 +0100 Subject: [PATCH 2/2] Unify Application::GetStartTime() and drop GetMainTime() This essentially moves the start time into the scope when main starts to "do something", after the reload and configuration handling is done. --- icinga-app/icinga.cpp | 2 -- lib/base/application.cpp | 13 +------------ lib/base/application.hpp | 3 --- lib/icinga/checkable-check.cpp | 6 ++++++ lib/icinga/checkable.cpp | 3 ++- lib/icinga/icingaapplication.cpp | 2 +- lib/remote/apilistener-authority.cpp | 4 ++-- 7 files changed, 12 insertions(+), 21 deletions(-) diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 6b4af7614..0756cee70 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -166,8 +166,6 @@ static int Main() argv += 3; } - Application::SetStartTime(Utility::GetTime()); - /* Set thread title. */ Utility::SetThreadName("Main Thread", false); diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 9468bb9f1..e8fd04704 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -51,7 +51,6 @@ static bool l_InExceptionHandler = false; int Application::m_ArgC; char **Application::m_ArgV; double Application::m_StartTime; -double Application::m_MainTime; bool Application::m_ScriptDebuggerEnabled = false; double Application::m_LastReloadFailed; @@ -965,7 +964,7 @@ int Application::Run() } #endif /* _WIN32 */ - SetMainTime(Utility::GetTime()); + SetStartTime(Utility::GetTime()); return Main(); } @@ -1152,16 +1151,6 @@ void Application::SetStartTime(double ts) m_StartTime = ts; } -double Application::GetMainTime() -{ - return m_MainTime; -} - -void Application::SetMainTime(double ts) -{ - m_MainTime = ts; -} - double Application::GetUptime() { return Utility::GetTime() - m_StartTime; diff --git a/lib/base/application.hpp b/lib/base/application.hpp index e2455e3e6..53d0b0c8b 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -95,9 +95,6 @@ public: static double GetStartTime(); static void SetStartTime(double ts); - static double GetMainTime(); - static void SetMainTime(double ts); - static double GetUptime(); static bool GetScriptDebuggerEnabled(); diff --git a/lib/icinga/checkable-check.cpp b/lib/icinga/checkable-check.cpp index 49aeaf223..7577f095d 100644 --- a/lib/icinga/checkable-check.cpp +++ b/lib/icinga/checkable-check.cpp @@ -581,6 +581,12 @@ void Checkable::ExecuteCheck() * using the proper check interval once we've received a check result. */ SetNextCheck(Utility::GetTime() + GetCheckCommand()->GetTimeout() + 30); + + /* + * Let the user know that there was a problem with the check if + * 1) The endpoint is not syncing (replay log, etc.) + * 2) Outside of the cold startup window (5min) + */ } else if (!endpoint->GetSyncing() && Application::GetInstance()->GetStartTime() < Utility::GetTime() - 300) { /* fail to perform check on unconnected endpoint */ cr->SetState(ServiceUnknown); diff --git a/lib/icinga/checkable.cpp b/lib/icinga/checkable.cpp index 3ff54b267..2ee1b4929 100644 --- a/lib/icinga/checkable.cpp +++ b/lib/icinga/checkable.cpp @@ -194,6 +194,7 @@ Timestamp Checkable::GetNextUpdate() const auto cr (GetLastCheckResult()); double interval, latency; + // TODO: Document this behavior. if (cr) { interval = GetProblem() && GetStateType() == StateTypeSoft ? GetRetryInterval() : GetCheckInterval(); latency = cr->GetExecutionEnd() - cr->GetScheduleStart(); @@ -202,7 +203,7 @@ Timestamp Checkable::GetNextUpdate() const latency = 0.0; } - return (GetEnableActiveChecks() ? GetNextCheck() : (cr ? cr->GetExecutionEnd() : Application::GetMainTime()) + interval) + interval + 2 * latency; + return (GetEnableActiveChecks() ? GetNextCheck() : (cr ? cr->GetExecutionEnd() : Application::GetStartTime()) + interval) + interval + 2 * latency; } void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime) diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index 5785b3358..074c26dbc 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -206,7 +206,7 @@ bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr *result = Utility::FormatDateTime("%H:%M:%S %z", now); return true; } else if (macro == "uptime") { - *result = Utility::FormatDuration(Utility::GetTime() - Application::GetStartTime()); + *result = Utility::FormatDuration(Application::GetUptime()); return true; } diff --git a/lib/remote/apilistener-authority.cpp b/lib/remote/apilistener-authority.cpp index d5fe5120c..f33a1905b 100644 --- a/lib/remote/apilistener-authority.cpp +++ b/lib/remote/apilistener-authority.cpp @@ -40,10 +40,10 @@ void ApiListener::UpdateObjectAuthority() endpoints.push_back(endpoint); } - double mainTime = Application::GetMainTime(); + double startTime = Application::GetStartTime(); /* 30 seconds cold startup, don't update any authority to give the secondary endpoint time to reconnect. */ - if (num_total > 1 && endpoints.size() <= 1 && (mainTime == 0 || Utility::GetTime() - mainTime < 30)) + if (num_total > 1 && endpoints.size() <= 1 && (startTime == 0 || Utility::GetTime() - startTime < 30)) return; std::sort(endpoints.begin(), endpoints.end(),