2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/cib.hpp"
|
2014-05-26 20:56:34 +02:00
|
|
|
#include "icinga/host.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2014-02-10 14:56:39 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-07-02 12:34:54 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
RingBuffer CIB::m_ActiveHostChecksStatistics(15 * 60);
|
|
|
|
RingBuffer CIB::m_ActiveServiceChecksStatistics(15 * 60);
|
|
|
|
RingBuffer CIB::m_PassiveHostChecksStatistics(15 * 60);
|
|
|
|
RingBuffer CIB::m_PassiveServiceChecksStatistics(15 * 60);
|
2012-07-02 12:34:54 +02:00
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
void CIB::UpdateActiveHostChecksStatistics(long tv, int num)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2014-05-26 20:56:34 +02:00
|
|
|
m_ActiveHostChecksStatistics.InsertValue(tv, num);
|
2012-07-02 12:34:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
void CIB::UpdateActiveServiceChecksStatistics(long tv, int num)
|
2012-07-02 12:34:54 +02:00
|
|
|
{
|
2014-05-26 20:56:34 +02:00
|
|
|
m_ActiveServiceChecksStatistics.InsertValue(tv, num);
|
2013-01-28 09:01:47 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
int CIB::GetActiveHostChecksStatistics(long timespan)
|
2013-01-28 09:01:47 +01:00
|
|
|
{
|
2014-05-26 20:56:34 +02:00
|
|
|
return m_ActiveHostChecksStatistics.GetValues(timespan);
|
2013-01-28 09:01:47 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
int CIB::GetActiveServiceChecksStatistics(long timespan)
|
2013-01-28 09:01:47 +01:00
|
|
|
{
|
2014-05-26 20:56:34 +02:00
|
|
|
return m_ActiveServiceChecksStatistics.GetValues(timespan);
|
2012-07-02 12:34:54 +02:00
|
|
|
}
|
2014-02-10 14:56:39 +01:00
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
void CIB::UpdatePassiveHostChecksStatistics(long tv, int num)
|
|
|
|
{
|
|
|
|
m_PassiveServiceChecksStatistics.InsertValue(tv, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIB::UpdatePassiveServiceChecksStatistics(long tv, int num)
|
|
|
|
{
|
|
|
|
m_PassiveServiceChecksStatistics.InsertValue(tv, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CIB::GetPassiveHostChecksStatistics(long timespan)
|
|
|
|
{
|
|
|
|
return m_PassiveHostChecksStatistics.GetValues(timespan);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CIB::GetPassiveServiceChecksStatistics(long timespan)
|
|
|
|
{
|
|
|
|
return m_PassiveServiceChecksStatistics.GetValues(timespan);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckableCheckStatistics CIB::CalculateHostCheckStats(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;
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
|
2014-05-26 20:56:34 +02:00
|
|
|
ObjectLock olock(host);
|
|
|
|
|
|
|
|
CheckResult::Ptr cr = host->GetLastCheckResult();
|
|
|
|
|
|
|
|
/* latency */
|
|
|
|
double latency = Host::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 = Host::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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckableCheckStatistics ccs;
|
|
|
|
|
|
|
|
ccs.min_latency = min_latency;
|
|
|
|
ccs.max_latency = max_latency;
|
|
|
|
ccs.avg_latency = sum_latency / count_latency;
|
|
|
|
ccs.min_execution_time = min_execution_time;
|
|
|
|
ccs.max_execution_time = max_execution_time;
|
|
|
|
ccs.avg_execution_time = sum_execution_time / count_execution_time;
|
|
|
|
|
|
|
|
return ccs;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckableCheckStatistics CIB::CalculateServiceCheckStats(void)
|
2014-02-10 14:56:39 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
|
2014-02-10 14:56:39 +01:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
CheckableCheckStatistics ccs;
|
2014-02-10 14:56:39 +01:00
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
ccs.min_latency = min_latency;
|
|
|
|
ccs.max_latency = max_latency;
|
|
|
|
ccs.avg_latency = sum_latency / count_latency;
|
|
|
|
ccs.min_execution_time = min_execution_time;
|
|
|
|
ccs.max_execution_time = max_execution_time;
|
|
|
|
ccs.avg_execution_time = sum_execution_time / count_execution_time;
|
2014-02-10 14:56:39 +01:00
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
return ccs;
|
2014-02-10 14:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceStatistics CIB::CalculateServiceStats(void)
|
|
|
|
{
|
|
|
|
ServiceStatistics ss = {0};
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
|
2014-02-10 14:56:39 +01:00
|
|
|
ObjectLock olock(service);
|
|
|
|
|
|
|
|
CheckResult::Ptr cr = service->GetLastCheckResult();
|
|
|
|
|
2014-04-08 09:11:54 +02:00
|
|
|
if (service->GetState() == ServiceOK)
|
2014-02-10 14:56:39 +01:00
|
|
|
ss.services_ok++;
|
2014-04-08 09:11:54 +02:00
|
|
|
if (service->GetState() == ServiceWarning)
|
2014-02-10 14:56:39 +01:00
|
|
|
ss.services_warning++;
|
2014-04-08 09:11:54 +02:00
|
|
|
if (service->GetState() == ServiceCritical)
|
2014-02-10 14:56:39 +01:00
|
|
|
ss.services_critical++;
|
2014-04-08 09:11:54 +02:00
|
|
|
if (service->GetState() == ServiceUnknown)
|
2014-02-10 14:56:39 +01:00
|
|
|
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};
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
|
2014-02-10 14:56:39 +01:00
|
|
|
ObjectLock olock(host);
|
|
|
|
|
2014-04-08 08:54:49 +02:00
|
|
|
if (host->IsReachable()) {
|
|
|
|
if (host->GetState() == HostUp)
|
|
|
|
hs.hosts_up++;
|
|
|
|
if (host->GetState() == HostDown)
|
|
|
|
hs.hosts_down++;
|
|
|
|
} else
|
2014-02-10 14:56:39 +01:00
|
|
|
hs.hosts_unreachable++;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (!host->GetLastCheckResult())
|
2014-02-10 14:56:39 +01:00
|
|
|
hs.hosts_pending++;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (host->IsFlapping())
|
2014-02-10 14:56:39 +01:00
|
|
|
hs.hosts_flapping++;
|
2014-04-03 15:36:13 +02:00
|
|
|
if (host->IsInDowntime())
|
2014-02-10 14:56:39 +01:00
|
|
|
hs.hosts_in_downtime++;
|
2014-04-03 15:36:13 +02:00
|
|
|
if (host->IsAcknowledged())
|
2014-02-10 14:56:39 +01:00
|
|
|
hs.hosts_acknowledged++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hs;
|
|
|
|
}
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2014-02-18 10:53:44 +01:00
|
|
|
/*
|
|
|
|
* 'perfdata' must be a flat dictionary with double values
|
|
|
|
* 'status' dictionary can contain multiple levels of dictionaries
|
|
|
|
*/
|
2014-09-17 15:38:39 +02:00
|
|
|
std::pair<Dictionary::Ptr, Array::Ptr> CIB::GetFeatureStats(void)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr status = new Dictionary();
|
|
|
|
Array::Ptr perfdata = new Array();
|
2014-02-17 16:34:18 +01:00
|
|
|
|
|
|
|
String name;
|
2014-04-03 15:36:13 +02:00
|
|
|
BOOST_FOREACH(tie(name, boost::tuples::ignore), StatsFunctionRegistry::GetInstance()->GetItems()) {
|
2014-02-17 16:34:18 +01:00
|
|
|
StatsFunction::Ptr func = StatsFunctionRegistry::GetInstance()->GetItem(name);
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Function '" + name + "' does not exist."));
|
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
func->Invoke(status, perfdata);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(status, perfdata);
|
|
|
|
}
|
|
|
|
|