]> granicus.if.org Git - icinga2/commitdiff
Move CalculateExecutionTime and CalculateLatency into the CheckResult class
authorMichael Friedrich <michael.friedrich@netways.de>
Tue, 10 May 2016 09:44:14 +0000 (11:44 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Tue, 10 May 2016 10:16:49 +0000 (12:16 +0200)
fixes #11751

16 files changed:
lib/compat/statusdatawriter.cpp
lib/db_ido/dbevents.cpp
lib/db_ido/hostdbobject.cpp
lib/db_ido/servicedbobject.cpp
lib/icinga/checkable-check.cpp
lib/icinga/checkable.hpp
lib/icinga/checkresult.cpp
lib/icinga/checkresult.hpp
lib/icinga/cib.cpp
lib/icinga/host.cpp
lib/icinga/service.cpp
lib/livestatus/hoststable.cpp
lib/livestatus/servicestable.cpp
lib/perfdata/gelfwriter.cpp
lib/perfdata/graphitewriter.cpp
lib/perfdata/opentsdbwriter.cpp

index d6073f57c80e45923d4b1466320492c471dd24ee..3c88e8a0ac4dd6b552f12903f18d3af65d39b6f8 100644 (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;
index fc870f78aa27e7ac134d5b59071bb829ef9003ad..b9b379632006573f0b15de780feea2fdd9a26a1c 100644 (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));
index 05ee42a65cf61bb09a0b2f6d4d073611ffbd0b52..47fd7d244a748def9cb82e379ee6f674e7ccb336 100644 (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());
index e56f5fceb29ec4e99a878ef108c8a83c8427093e..2806e1c7018c853b7d4727f3f35020104a00df7d 100644 (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());
index 6e69a8ffa121c7f65cc8362e5794f986c2de07fa..f23cee7ed69734d0a0df345e404ba036939f5155 100644 (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;
-}
index a576f5768e15b097c58c634f92964c941b760ada..abd966dfd1882d6f17ca456e4c44fd62bfc71dec 100644 (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;
index 50028df7833e93c571711517d26ddd4a148ece9c..5a17547b50e707140c7e4fe219bf58b8644886b8 100644 (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;
+}
index 3e4f729bd746503092a6edeb0ec6121b6d859189..b80f6c3d79caf7387ebb0fc7a25331169966e5b6 100644 (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);
 };
 
index 67c14a120e88ebea06dc19be7eb654ba184e9f28..ccdbc177efee854bdaa8d8f78317b38e344ac77c 100644 (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;
index 5fd2dfc471549e51d0d57f91475787d062c1f826..af33d528b8237babe03d5dc2fa55af5db7ad5a52 100644 (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();
index a96d6058dd576529b37d84b68f0cf5739c81a6a8..78b389ce2f084a3e0471c7b3b5b7bffcecad929e 100644 (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();
index 168aaf2ad103347ba2a18cfd54d4f03113165cc9..62da70dd960e014e41fb39fb9ec00fa6c83d7260 100644 (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)
index 7b5c04327455a1c70e80784ce42ab6dea9edaf08..95f05212fc2149af2d5dfd1973258d47ac688cdd 100644 (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)
index d77ac272020400e32ff3e353f6b25e79a799a796..f32d34487f83ef916bdedcfad60d81108dde0ad4 100644 (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) {
index fc5987db8c0133f98943e0d3605935f012b8c828..3f59287e586a4e6d6d764fef989a638c62d9ff25 100644 (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);
        }
 }
index aa8bdcc4284c001c9eca07a9724866ed01e4c7c6..1f9b3896adb063a9041ace8d5b7942e1df6d590f 100644 (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)