]> granicus.if.org Git - icinga2/commitdiff
Move Check Stats into CIB.
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 10 Feb 2014 13:56:39 +0000 (14:56 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 10 Feb 2014 13:59:56 +0000 (14:59 +0100)
Fixes #5617

lib/icinga/cib.cpp
lib/icinga/cib.h
lib/methods/icingachecktask.cpp

index 45ecf46b5a58ff30a978204488a319102bb87808..e98d8c2dcfa7bec3f867720e7b351bdf6a3bc20b 100644 (file)
  ******************************************************************************/
 
 #include "icinga/cib.h"
+#include "icinga/service.h"
+#include "base/objectlock.h"
+#include "base/utility.h"
+#include "base/dynamictype.h"
+#include <boost/foreach.hpp>
 
 using namespace icinga;
 
@@ -43,3 +48,121 @@ int CIB::GetPassiveChecksStatistics(long timespan)
 {
        return m_PassiveChecksStatistics.GetValues(timespan);
 }
+
+ServiceCheckStatistics CIB::CalculateServiceCheckStats(void)
+{
+       double min_latency = -1, max_latency = 0, sum_latency = 0;
+       int count_latency = 0;
+       double min_execution_time = -1, max_execution_time = 0, sum_execution_time = 0;
+       int count_execution_time = 0;
+
+       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
+               ObjectLock olock(service);
+
+               CheckResult::Ptr cr = service->GetLastCheckResult();
+
+               /* latency */
+               double latency = Service::CalculateLatency(cr);
+
+               if (min_latency == -1 || latency < min_latency)
+                       min_latency = latency;
+
+               if (latency > max_latency)
+                       max_latency = latency;
+
+               sum_latency += latency;
+               count_latency++;
+
+               /* execution_time */
+               double execution_time = Service::CalculateExecutionTime(cr);
+
+               if (min_execution_time == -1 || execution_time < min_execution_time)
+                       min_execution_time = execution_time;
+
+               if (execution_time > max_execution_time)
+                       max_execution_time = execution_time;
+
+               sum_execution_time += execution_time;
+               count_execution_time++;
+       }
+
+       ServiceCheckStatistics scs = {0};
+
+       scs.min_latency = min_latency;
+       scs.max_latency = max_latency;
+       scs.avg_latency = sum_latency / count_latency;
+       scs.min_execution_time = min_execution_time;
+       scs.max_execution_time = max_execution_time;
+       scs.avg_execution_time = sum_execution_time / count_execution_time;
+
+       return scs;
+}
+
+ServiceStatistics CIB::CalculateServiceStats(void)
+{
+       ServiceStatistics ss = {0};
+
+       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
+               ObjectLock olock(service);
+
+               CheckResult::Ptr cr = service->GetLastCheckResult();
+
+               if (service->GetState() == StateOK)
+                       ss.services_ok++;
+               if (service->GetState() == StateWarning)
+                       ss.services_warning++;
+               if (service->GetState() == StateCritical)
+                       ss.services_critical++;
+               if (service->GetState() == StateUnknown)
+                       ss.services_unknown++;
+
+               if (!cr)
+                       ss.services_pending++;
+               if (!service->IsReachable())
+                       ss.services_unreachable++;
+
+               if (service->IsFlapping())
+                       ss.services_flapping++;
+               if (service->IsInDowntime())
+                       ss.services_in_downtime++;
+               if (service->IsAcknowledged())
+                       ss.services_acknowledged++;
+       }
+
+       return ss;
+}
+
+HostStatistics CIB::CalculateHostStats(void)
+{
+       HostStatistics hs = {0};
+
+       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
+               ObjectLock olock(host);
+
+               if (host->GetState() == HostUp)
+                       hs.hosts_up++;
+               if (host->GetState() == HostDown)
+                       hs.hosts_down++;
+               if (host->GetState() == HostUnreachable)
+                       hs.hosts_unreachable++;
+
+               Service::Ptr hc = host->GetCheckService();
+
+               if (!hc) {
+                       hs.hosts_pending++;
+                       continue; /* skip host service check counting */
+               }
+
+               if (!hc->GetLastCheckResult())
+                       hs.hosts_pending++;
+
+               if (hc->IsFlapping())
+                       hs.hosts_flapping++;
+               if (hc->IsInDowntime())
+                       hs.hosts_in_downtime++;
+               if (hc->IsAcknowledged())
+                       hs.hosts_acknowledged++;
+       }
+
+       return hs;
+}
index 26ff812f8e74aa306ddda2161eaf872eb80fd361..e44d5bdcf1a7f3610dec842a59054bf98d076bb7 100644 (file)
 namespace icinga
 {
 
+typedef struct {
+    double min_latency;
+    double max_latency;
+    double avg_latency;
+    double min_execution_time;
+    double max_execution_time;
+    double avg_execution_time;
+} ServiceCheckStatistics;
+
+typedef struct {
+    double services_ok;
+    double services_warning;
+    double services_critical;
+    double services_unknown;
+    double services_pending;
+    double services_unreachable;
+    double services_flapping;
+    double services_in_downtime;
+    double services_acknowledged;
+} ServiceStatistics;
+
+typedef struct {
+    double hosts_up;
+    double hosts_down;
+    double hosts_unreachable;
+    double hosts_pending;
+    double hosts_flapping;
+    double hosts_in_downtime;
+    double hosts_acknowledged;
+} HostStatistics;
+
 /**
  * Common Information Base class. Holds some statistics (and will likely be
  * removed/refactored).
@@ -41,6 +72,10 @@ public:
        static void UpdatePassiveChecksStatistics(long tv, int num);
        static int GetPassiveChecksStatistics(long timespan);
 
+        static ServiceCheckStatistics CalculateServiceCheckStats(void);
+        static ServiceStatistics CalculateServiceStats(void);
+        static HostStatistics CalculateHostStats(void);
+
 private:
        CIB(void);
 
index 8aaf24417703e1e352b16f0f05cf3cb526e767f8..0b13a755fe45675be1990daa54eed4c9a564f556 100644 (file)
@@ -42,120 +42,35 @@ CheckResult::Ptr IcingaCheckTask::ScriptFunc(const Service::Ptr&)
        perfdata->Set("active_checks", CIB::GetActiveChecksStatistics(interval) / interval);
        perfdata->Set("passive_checks", CIB::GetPassiveChecksStatistics(interval) / interval);
 
-       double min_latency = -1, max_latency = 0, sum_latency = 0;
-       int count_latency = 0;
-       double min_execution_time = -1, max_execution_time = 0, sum_execution_time = 0;
-       int count_execution_time = 0;
-
-       double services_ok, services_warn, services_crit, services_unknown, services_pending, services_unreachable,
-           services_flapping, services_in_downtime, services_acknowledged = 0;
-       double hosts_up, hosts_down, hosts_unreachable, hosts_pending, hosts_flapping, hosts_in_downtime, hosts_acknowledged = 0;
-
-       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
-               ObjectLock olock(service);
-
-               CheckResult::Ptr cr = service->GetLastCheckResult();
-
-               /* latency */
-               double latency = Service::CalculateLatency(cr);
-
-               if (min_latency == -1 || latency < min_latency)
-                       min_latency = latency;
-
-               if (latency > max_latency)
-                       max_latency = latency;
-
-               sum_latency += latency;
-               count_latency++;
-
-               /* execution_time */
-               double execution_time = Service::CalculateExecutionTime(cr);
-
-               if (min_execution_time == -1 || execution_time < min_execution_time)
-                       min_execution_time = execution_time;
-
-               if (execution_time > max_execution_time)
-                       max_execution_time = execution_time;
-
-               sum_execution_time += execution_time;
-               count_execution_time++;
-
-               /* states */
-               if (service->GetState() == StateOK)
-                       services_ok++;
-               if (service->GetState() == StateWarning)
-                       services_warn++;
-               if (service->GetState() == StateCritical)
-                       services_crit++;
-               if (service->GetState() == StateUnknown)
-                       services_unknown++;
-
-               /* pending, unreachable */
-               if (!cr)
-                       services_pending++;
-               if (!service->IsReachable())
-                       services_unreachable++;
-
-               /* flapping, downtime, acknowledgements */
-               if (service->IsFlapping())
-                       services_flapping++;
-               if (service->IsInDowntime())
-                       services_in_downtime++;
-               if (service->IsAcknowledged())
-                       services_acknowledged++;
-       }
-
-       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
-               ObjectLock olock(host);
-
-               if (host->GetState() == HostUp)
-                       hosts_up++;
-               if (host->GetState() == HostDown)
-                       hosts_down++;
-               if (host->GetState() == HostUnreachable)
-                       hosts_unreachable++;
-
-               Service::Ptr hc = host->GetCheckService();
-
-               if (!hc) {
-                       hosts_pending++;
-                       continue; /* skip host service check counting */
-               }
-
-               if (!hc->GetLastCheckResult())
-                       hosts_pending++;
-
-               if (hc->IsFlapping())
-                       hosts_flapping++;
-               if (hc->IsInDowntime())
-                       hosts_in_downtime++;
-               if (hc->IsAcknowledged())
-                       hosts_acknowledged++;
-       }
-
-       perfdata->Set("min_latency", min_latency);
-       perfdata->Set("max_latency", max_latency);
-       perfdata->Set("avg_latency", sum_latency / count_latency);
-       perfdata->Set("min_execution_time", min_latency);
-       perfdata->Set("max_execution_time", max_latency);
-       perfdata->Set("avg_execution_time", sum_execution_time / count_execution_time);
-
-       perfdata->Set("num_services_ok", services_ok);
-       perfdata->Set("num_services_warn", services_warn);
-       perfdata->Set("num_services_crit", services_crit);
-       perfdata->Set("num_services_unknown", services_unknown);
-       perfdata->Set("num_services_pending", services_pending);
-       perfdata->Set("num_services_unreachable", services_unreachable);
-       perfdata->Set("num_services_flapping", services_flapping);
-       perfdata->Set("num_services_in_downtime", services_in_downtime);
-       perfdata->Set("num_services_acknowledged", services_acknowledged);
-
-       perfdata->Set("num_hosts_up", hosts_up);
-       perfdata->Set("num_hosts_down", hosts_down);
-       perfdata->Set("num_hosts_unreachable", hosts_unreachable);
-       perfdata->Set("num_hosts_flapping", hosts_flapping);
-       perfdata->Set("num_hosts_in_downtime", hosts_in_downtime);
-       perfdata->Set("num_hosts_acknowledged", hosts_acknowledged);
+       ServiceCheckStatistics scs = CIB::CalculateServiceCheckStats();
+
+       perfdata->Set("min_latency", scs.min_latency);
+       perfdata->Set("max_latency", scs.max_latency);
+       perfdata->Set("avg_latency", scs.avg_latency);
+       perfdata->Set("min_execution_time", scs.min_latency);
+       perfdata->Set("max_execution_time", scs.max_latency);
+       perfdata->Set("avg_execution_time", scs.avg_execution_time);
+
+       ServiceStatistics ss = CIB::CalculateServiceStats();
+
+       perfdata->Set("num_services_ok", ss.services_ok);
+       perfdata->Set("num_services_warning", ss.services_warning);
+       perfdata->Set("num_services_critical", ss.services_critical);
+       perfdata->Set("num_services_unknown", ss.services_unknown);
+       perfdata->Set("num_services_pending", ss.services_pending);
+       perfdata->Set("num_services_unreachable", ss.services_unreachable);
+       perfdata->Set("num_services_flapping", ss.services_flapping);
+       perfdata->Set("num_services_in_downtime", ss.services_in_downtime);
+       perfdata->Set("num_services_acknowledged", ss.services_acknowledged);
+
+       HostStatistics hs = CIB::CalculateHostStats();
+
+       perfdata->Set("num_hosts_up", hs.hosts_up);
+       perfdata->Set("num_hosts_down", hs.hosts_down);
+       perfdata->Set("num_hosts_unreachable", hs.hosts_unreachable);
+       perfdata->Set("num_hosts_flapping", hs.hosts_flapping);
+       perfdata->Set("num_hosts_in_downtime", hs.hosts_in_downtime);
+       perfdata->Set("num_hosts_acknowledged", hs.hosts_acknowledged);
 
        CheckResult::Ptr cr = make_shared<CheckResult>();
        cr->SetOutput("Icinga 2 is running.");