]> granicus.if.org Git - icinga2/commitdiff
Add uptime information to the "icinga" check type.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 28 Apr 2014 07:16:27 +0000 (09:16 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 28 Apr 2014 07:23:52 +0000 (09:23 +0200)
Fixes #6064

lib/base/utility.cpp
lib/base/utility.h
lib/methods/icingachecktask.cpp

index ba967b179e46418992bb42826d74f48a6c9b7b6b..56559fea22eb2ff9259652753cbb1464688dd255 100644 (file)
@@ -697,6 +697,55 @@ void Utility::QueueAsyncCallback(const boost::function<void (void)>& callback)
        Application::GetTP().Post(callback);
 }
 
+String Utility::NaturalJoin(const std::vector<String>& 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<String> 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];
index 10f7633d32909c127c6549aaae3075d1fc4f6e6f..f8c05a5fe930af6b28eb33235feefb7c960b47b2 100644 (file)
@@ -25,6 +25,7 @@
 #include <typeinfo>
 #include <boost/function.hpp>
 #include <boost/thread/tss.hpp>
+#include <vector>
 
 namespace icinga
 {
@@ -83,6 +84,9 @@ public:
 
        static void QueueAsyncCallback(const boost::function<void (void)>& callback);
 
+       static String NaturalJoin(const std::vector<String>& tokens);
+
+       static String FormatDuration(int duration);
        static String FormatDateTime(const char *format, double ts);
 
        static
index 72c7066fe59681070654f60b4d3c438534931856..d5045ce57c5e97104d0afa13576dfb69ad844b09 100644 (file)
@@ -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);