From ea24147d0d6f875ef8b0d7243827c18610b6cbb3 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 28 Apr 2014 09:16:27 +0200 Subject: [PATCH] Add uptime information to the "icinga" check type. Fixes #6064 --- lib/base/utility.cpp | 49 +++++++++++++++++++++++++++++++++ lib/base/utility.h | 4 +++ lib/methods/icingachecktask.cpp | 6 +++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index ba967b179..56559fea2 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -697,6 +697,55 @@ void Utility::QueueAsyncCallback(const boost::function& callback) Application::GetTP().Post(callback); } +String Utility::NaturalJoin(const std::vector& tokens) +{ + String result; + + for (int i = 0; i < tokens.size(); i++) { + result += tokens[i]; + + if (tokens.size() > i + 1) { + if (i < tokens.size() - 2) + result += ", "; + else if (i == tokens.size() - 2) + result += " and "; + } + } + + return result; +} + +String Utility::FormatDuration(int duration) +{ + std::vector tokens; + String result; + + if (duration >= 86400) { + int days = duration / 86400; + tokens.push_back(Convert::ToString(days) + (days != 1 ? " days" : " day")); + duration %= 86400; + } + + if (duration >= 3600) { + int hours = duration / 3600; + tokens.push_back(Convert::ToString(hours) + (hours != 1 ? " hours" : " hour")); + duration %= 3600; + } + + if (duration >= 60) { + int minutes = duration / 60; + tokens.push_back(Convert::ToString(minutes) + (minutes != 1 ? " minutes" : " minute")); + duration %= 60; + } + + if (duration >= 1) { + int seconds = duration; + tokens.push_back(Convert::ToString(seconds) + (seconds != 1 ? " seconds" : " second")); + } + + return NaturalJoin(tokens); +} + String Utility::FormatDateTime(const char *format, double ts) { char timestamp[128]; diff --git a/lib/base/utility.h b/lib/base/utility.h index 10f7633d3..f8c05a5fe 100644 --- a/lib/base/utility.h +++ b/lib/base/utility.h @@ -25,6 +25,7 @@ #include #include #include +#include namespace icinga { @@ -83,6 +84,9 @@ public: static void QueueAsyncCallback(const boost::function& callback); + static String NaturalJoin(const std::vector& tokens); + + static String FormatDuration(int duration); static String FormatDateTime(const char *format, double ts); static diff --git a/lib/methods/icingachecktask.cpp b/lib/methods/icingachecktask.cpp index 72c7066fe..d5045ce57 100644 --- a/lib/methods/icingachecktask.cpp +++ b/lib/methods/icingachecktask.cpp @@ -70,6 +70,9 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResul perfdata->Set("num_services_in_downtime", ss.services_in_downtime); perfdata->Set("num_services_acknowledged", ss.services_acknowledged); + double uptime = Utility::GetTime() - Application::GetStartTime(); + perfdata->Set("uptime", uptime); + HostStatistics hs = CIB::CalculateHostStats(); perfdata->Set("num_hosts_up", hs.hosts_up); @@ -79,7 +82,8 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResul perfdata->Set("num_hosts_in_downtime", hs.hosts_in_downtime); perfdata->Set("num_hosts_acknowledged", hs.hosts_acknowledged); - cr->SetOutput("Icinga 2 is running. Version: " + Application::GetVersion()); + cr->SetOutput("Icinga 2 has been running for " + Utility::FormatDuration(uptime) + + ". Version: " + Application::GetVersion()); cr->SetPerformanceData(perfdata); cr->SetState(ServiceOK);