Move CalculateExecutionTime and CalculateLatency into the CheckResult class

fixes #11751
This commit is contained in:
Michael Friedrich 2016-05-10 11:44:14 +02:00
parent f6f3bd1e4c
commit ba82d2eb20
16 changed files with 72 additions and 52 deletions

View File

@ -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;

View File

@ -1391,7 +1391,7 @@ void DbEvents::AddCheckableCheckHistory(const Checkable::Ptr& checkable, const C
double end = cr->GetExecutionEnd();
std::pair<unsigned long, unsigned long> 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));

View File

@ -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());

View File

@ -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());

View File

@ -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;
}

View File

@ -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<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> OnNewCheckResult;
static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> OnStateChange;
static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> OnReachabilityChanged;

View File

@ -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;
}

View File

@ -36,6 +36,9 @@ class I2_ICINGA_API CheckResult : public ObjectImpl<CheckResult>
public:
DECLARE_OBJECT(CheckResult);
double CalculateExecutionTime(void) const;
double CalculateLatency(void) const;
static void StaticInitialize(void);
};

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -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)

View File

@ -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)

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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<String, String>& tags, const CheckResult::Ptr& cr, double ts)