diff --git a/lib/compat/statusdatawriter.cpp b/lib/compat/statusdatawriter.cpp index d6073f57c..3c88e8a0a 100644 --- a/lib/compat/statusdatawriter.cpp +++ b/lib/compat/statusdatawriter.cpp @@ -335,8 +335,8 @@ void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkabl "\t" "event_handler_enabled=" << CompatUtility::GetCheckableEventHandlerEnabled(checkable) << "\n"; if (cr) { - fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n" - "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n"; + fp << "\t" << "check_execution_time=" << Convert::ToString(cr->CalculateExecutionTime()) << "\n" + "\t" "check_latency=" << Convert::ToString(cr->CalculateLatency()) << "\n"; } Host::Ptr host; diff --git a/lib/db_ido/dbevents.cpp b/lib/db_ido/dbevents.cpp index fc870f78a..b9b379632 100644 --- a/lib/db_ido/dbevents.cpp +++ b/lib/db_ido/dbevents.cpp @@ -1391,7 +1391,7 @@ void DbEvents::AddCheckableCheckHistory(const Checkable::Ptr& checkable, const C double end = cr->GetExecutionEnd(); std::pair time_bag_end = CompatUtility::ConvertTimestamp(end); - double execution_time = Service::CalculateExecutionTime(cr); + double execution_time = cr->CalculateExecutionTime(); fields1->Set("start_time", DbValue::FromTimestamp(time_bag_start.first)); fields1->Set("start_time_usec", time_bag_start.second); @@ -1401,7 +1401,7 @@ void DbEvents::AddCheckableCheckHistory(const Checkable::Ptr& checkable, const C fields1->Set("command_args", Empty); fields1->Set("command_line", CompatUtility::GetCommandLine(checkable->GetCheckCommand())); fields1->Set("execution_time", Convert::ToString(execution_time)); - fields1->Set("latency", Convert::ToString(Service::CalculateLatency(cr))); + fields1->Set("latency", Convert::ToString(cr->CalculateLatency())); fields1->Set("return_code", cr->GetExitStatus()); fields1->Set("output", CompatUtility::GetCheckResultOutput(cr)); fields1->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index 05ee42a65..47fd7d244 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -157,8 +157,8 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host)); if (cr) { - fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr))); - fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr))); + fields->Set("latency", Convert::ToString(cr->CalculateLatency())); + fields->Set("execution_time", Convert::ToString(cr->CalculateExecutionTime())); } fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth()); diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index e56f5fceb..2806e1c70 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -153,8 +153,8 @@ Dictionary::Ptr ServiceDbObject::GetStatusFields(void) const fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(service)); if (cr) { - fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr))); - fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr))); + fields->Set("latency", Convert::ToString(cr->CalculateLatency())); + fields->Set("execution_time", Convert::ToString(cr->CalculateExecutionTime())); } fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth()); diff --git a/lib/icinga/checkable-check.cpp b/lib/icinga/checkable-check.cpp index 6e69a8ffa..f23cee7ed 100644 --- a/lib/icinga/checkable-check.cpp +++ b/lib/icinga/checkable-check.cpp @@ -515,24 +515,3 @@ void Checkable::UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type) Log(LogWarning, "Checkable", "Unknown checkable type for statistic update."); } } - -double Checkable::CalculateExecutionTime(const CheckResult::Ptr& cr) -{ - if (!cr) - return 0; - - return cr->GetExecutionEnd() - cr->GetExecutionStart(); -} - -double Checkable::CalculateLatency(const CheckResult::Ptr& cr) -{ - if (!cr) - return 0; - - double latency = (cr->GetScheduleEnd() - cr->GetScheduleStart()) - CalculateExecutionTime(cr); - - if (latency < 0) - latency = 0; - - return latency; -} diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index a576f5768..abd966dfd 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -106,9 +106,6 @@ public: Endpoint::Ptr GetCommandEndpoint(void) const; - static double CalculateExecutionTime(const CheckResult::Ptr& cr); - static double CalculateLatency(const CheckResult::Ptr& cr); - static boost::signals2::signal OnNewCheckResult; static boost::signals2::signal OnStateChange; static boost::signals2::signal, const MessageOrigin::Ptr&)> OnReachabilityChanged; diff --git a/lib/icinga/checkresult.cpp b/lib/icinga/checkresult.cpp index 50028df78..5a17547b5 100644 --- a/lib/icinga/checkresult.cpp +++ b/lib/icinga/checkresult.cpp @@ -36,3 +36,18 @@ void CheckResult::StaticInitialize(void) ScriptGlobal::Set("HostUp", HostUp); ScriptGlobal::Set("HostDown", HostDown); } + +double CheckResult::CalculateExecutionTime(void) const +{ + return GetExecutionEnd() - GetExecutionStart(); +} + +double CheckResult::CalculateLatency(void) const +{ + double latency = (GetScheduleEnd() - GetScheduleStart()) - CalculateExecutionTime(); + + if (latency < 0) + latency = 0; + + return latency; +} diff --git a/lib/icinga/checkresult.hpp b/lib/icinga/checkresult.hpp index 3e4f729bd..b80f6c3d7 100644 --- a/lib/icinga/checkresult.hpp +++ b/lib/icinga/checkresult.hpp @@ -36,6 +36,9 @@ class I2_ICINGA_API CheckResult : public ObjectImpl public: DECLARE_OBJECT(CheckResult); + double CalculateExecutionTime(void) const; + double CalculateLatency(void) const; + static void StaticInitialize(void); }; diff --git a/lib/icinga/cib.cpp b/lib/icinga/cib.cpp index 67c14a120..ccdbc177e 100644 --- a/lib/icinga/cib.cpp +++ b/lib/icinga/cib.cpp @@ -86,8 +86,11 @@ CheckableCheckStatistics CIB::CalculateHostCheckStats(void) CheckResult::Ptr cr = host->GetLastCheckResult(); + if (!cr) + continue; + /* latency */ - double latency = Host::CalculateLatency(cr); + double latency = cr->CalculateLatency(); if (min_latency == -1 || latency < min_latency) min_latency = latency; @@ -99,7 +102,7 @@ CheckableCheckStatistics CIB::CalculateHostCheckStats(void) count_latency++; /* execution_time */ - double execution_time = Host::CalculateExecutionTime(cr); + double execution_time = cr->CalculateExecutionTime(); if (min_execution_time == -1 || execution_time < min_execution_time) min_execution_time = execution_time; @@ -135,8 +138,11 @@ CheckableCheckStatistics CIB::CalculateServiceCheckStats(void) CheckResult::Ptr cr = service->GetLastCheckResult(); + if (!cr) + continue; + /* latency */ - double latency = Service::CalculateLatency(cr); + double latency = cr->CalculateLatency(); if (min_latency == -1 || latency < min_latency) min_latency = latency; @@ -148,7 +154,7 @@ CheckableCheckStatistics CIB::CalculateServiceCheckStats(void) count_latency++; /* execution_time */ - double execution_time = Service::CalculateExecutionTime(cr); + double execution_time = cr->CalculateExecutionTime(); if (min_execution_time == -1 || execution_time < min_execution_time) min_execution_time = execution_time; diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 5fd2dfc47..af33d528b 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -289,10 +289,10 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res if (cr) { if (macro == "latency") { - *result = Convert::ToString(Service::CalculateLatency(cr)); + *result = Convert::ToString(cr->CalculateLatency()); return true; } else if (macro == "execution_time") { - *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + *result = Convert::ToString(cr->CalculateExecutionTime()); return true; } else if (macro == "output") { *result = cr->GetOutput(); diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index a96d6058d..78b389ce2 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -219,10 +219,10 @@ bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Valu if (cr) { if (macro == "latency") { - *result = Convert::ToString(Service::CalculateLatency(cr)); + *result = Convert::ToString(cr->CalculateLatency()); return true; } else if (macro == "execution_time") { - *result = Convert::ToString(Service::CalculateExecutionTime(cr)); + *result = Convert::ToString(cr->CalculateExecutionTime()); return true; } else if (macro == "output") { *result = cr->GetOutput(); diff --git a/lib/livestatus/hoststable.cpp b/lib/livestatus/hoststable.cpp index 168aaf2ad..62da70dd9 100644 --- a/lib/livestatus/hoststable.cpp +++ b/lib/livestatus/hoststable.cpp @@ -836,7 +836,12 @@ Value HostsTable::LatencyAccessor(const Value& row) if (!host) return Empty; - return (Service::CalculateLatency(host->GetLastCheckResult())); + CheckResult::Ptr cr = host->GetLastCheckResult(); + + if (!cr) + return Empty; + + return cr->CalculateLatency(); } Value HostsTable::ExecutionTimeAccessor(const Value& row) @@ -846,7 +851,12 @@ Value HostsTable::ExecutionTimeAccessor(const Value& row) if (!host) return Empty; - return (Service::CalculateExecutionTime(host->GetLastCheckResult())); + CheckResult::Ptr cr = host->GetLastCheckResult(); + + if (!cr) + return Empty; + + return cr->CalculateExecutionTime(); } Value HostsTable::PercentStateChangeAccessor(const Value& row) diff --git a/lib/livestatus/servicestable.cpp b/lib/livestatus/servicestable.cpp index 7b5c04327..95f05212f 100644 --- a/lib/livestatus/servicestable.cpp +++ b/lib/livestatus/servicestable.cpp @@ -874,7 +874,12 @@ Value ServicesTable::LatencyAccessor(const Value& row) if (!service) return Empty; - return (Service::CalculateLatency(service->GetLastCheckResult())); + CheckResult::Ptr cr = service->GetLastCheckResult(); + + if (!cr) + return Empty; + + return cr->CalculateLatency(); } Value ServicesTable::ExecutionTimeAccessor(const Value& row) @@ -884,7 +889,12 @@ Value ServicesTable::ExecutionTimeAccessor(const Value& row) if (!service) return Empty; - return (Service::CalculateExecutionTime(service->GetLastCheckResult())); + CheckResult::Ptr cr = service->GetLastCheckResult(); + + if (!cr) + return Empty; + + return cr->CalculateExecutionTime(); } Value ServicesTable::PercentStateChangeAccessor(const Value& row) diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index d77ac2720..f32d34487 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -110,8 +110,8 @@ void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const Check fields->Set("_current_check_attempt", checkable->GetCheckAttempt()); fields->Set("_max_check_attempts", checkable->GetMaxCheckAttempts()); - fields->Set("_latency", Service::CalculateLatency(cr)); - fields->Set("_execution_time", Service::CalculateExecutionTime(cr)); + fields->Set("_latency", cr->CalculateLatency()); + fields->Set("_execution_time", cr->CalculateExecutionTime()); fields->Set("_reachable", checkable->IsReachable()); if (cr) { diff --git a/lib/perfdata/graphitewriter.cpp b/lib/perfdata/graphitewriter.cpp index fc5987db8..3f59287e5 100644 --- a/lib/perfdata/graphitewriter.cpp +++ b/lib/perfdata/graphitewriter.cpp @@ -141,8 +141,8 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C SendMetric(prefix_metadata, "state_type", checkable->GetStateType(), ts); SendMetric(prefix_metadata, "reachable", checkable->IsReachable(), ts); SendMetric(prefix_metadata, "downtime_depth", checkable->GetDowntimeDepth(), ts); - SendMetric(prefix_metadata, "latency", Service::CalculateLatency(cr), ts); - SendMetric(prefix_metadata, "execution_time", Service::CalculateExecutionTime(cr), ts); + SendMetric(prefix_metadata, "latency", cr->CalculateLatency(), ts); + SendMetric(prefix_metadata, "execution_time", cr->CalculateExecutionTime(), ts); } SendPerfdata(prefix_perfdata, cr, ts); @@ -160,8 +160,8 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C SendMetric(prefix, "state_type", checkable->GetStateType(), ts); SendMetric(prefix, "reachable", checkable->IsReachable(), ts); SendMetric(prefix, "downtime_depth", checkable->GetDowntimeDepth(), ts); - SendMetric(prefix, "latency", Service::CalculateLatency(cr), ts); - SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr), ts); + SendMetric(prefix, "latency", cr->CalculateLatency(), ts); + SendMetric(prefix, "execution_time", cr->CalculateExecutionTime(), ts); SendPerfdata(prefix, cr, ts); } } diff --git a/lib/perfdata/opentsdbwriter.cpp b/lib/perfdata/opentsdbwriter.cpp index aa8bdcc42..1f9b3896a 100644 --- a/lib/perfdata/opentsdbwriter.cpp +++ b/lib/perfdata/opentsdbwriter.cpp @@ -145,8 +145,8 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts); SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts); - SendMetric(metric + ".latency", tags, Service::CalculateLatency(cr), ts); - SendMetric(metric + ".execution_time", tags, Service::CalculateExecutionTime(cr), ts); + SendMetric(metric + ".latency", tags, cr->CalculateLatency(), ts); + SendMetric(metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts); } void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map& tags, const CheckResult::Ptr& cr, double ts)