diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index 45ecf46b5..e98d8c2dc 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -18,6 +18,11 @@ ******************************************************************************/ #include "icinga/cib.h" +#include "icinga/service.h" +#include "base/objectlock.h" +#include "base/utility.h" +#include "base/dynamictype.h" +#include 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()) { + 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()) { + 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()) { + 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; +} diff --git a/lib/icinga/cib.h b/lib/icinga/cib.h index 26ff812f8..e44d5bdcf 100644 --- a/lib/icinga/cib.h +++ b/lib/icinga/cib.h @@ -26,6 +26,37 @@ 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); diff --git a/lib/methods/icingachecktask.cpp b/lib/methods/icingachecktask.cpp index 8aaf24417..0b13a755f 100644 --- a/lib/methods/icingachecktask.cpp +++ b/lib/methods/icingachecktask.cpp @@ -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; + ServiceCheckStatistics scs = CIB::CalculateServiceCheckStats(); - 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; + 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); - BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { - ObjectLock olock(service); + ServiceStatistics ss = CIB::CalculateServiceStats(); - CheckResult::Ptr cr = service->GetLastCheckResult(); + 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); - /* latency */ - double latency = Service::CalculateLatency(cr); + HostStatistics hs = CIB::CalculateHostStats(); - 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()) { - 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); + 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(); cr->SetOutput("Icinga 2 is running.");