]> granicus.if.org Git - icinga2/commitdiff
Fixed latency calculation.
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 27 Jun 2012 21:38:50 +0000 (23:38 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 27 Jun 2012 21:38:50 +0000 (23:38 +0200)
components/checker/checkercomponent.cpp
components/compat/compatcomponent.cpp
icinga/checkresult.cpp
icinga/checkresult.h
icinga/checktask.cpp
icinga/checktask.h
icinga/icingaapplication.cpp
icinga/icingaapplication.h
icinga/nagioschecktask.cpp
icinga/nagioschecktask.h
icinga/service.cpp

index 0e414d68eaa721ad53da103b7126d3141b2b82ae..488a5c627ce5381e56893cc0aeccd8f50c32468c 100644 (file)
@@ -117,7 +117,8 @@ void CheckerComponent::ResultTimerHandler(void)
                CheckResult result = task->GetResult();
                Application::Log(LogDebug, "checker", "Got result for service '" + service.GetName() + "'");
 
-               long latency = result.GetEndTime() - result.GetStartTime();
+               long execution_time = result.GetExecutionEnd() - result.GetExecutionStart();
+               long latency = (result.GetScheduleEnd() - result.GetScheduleStart()) - execution_time;
                avg_latency += latency;
 
                if (min_latency == -1 || latency < min_latency)
index a32ee47d10cb0f4094ce50f2e41144ff4080bafb..797b35c5b2fd1f444b074e0bf0cf635850fec51f 100644 (file)
@@ -61,6 +61,7 @@ void CompatComponent::DumpHostStatus(ofstream& fp, Host host)
           << "\t" << "check_execution_time=0" << endl
           << "\t" << "check_latency=0" << endl
           << "\t" << "current_state=0" << endl
+          << "\t" << "state_type=1" << endl
           << "\t" << "last_check=" << time(NULL) << endl
           << "\t" << "next_check=" << time(NULL) << endl
           << "\t" << "current_attempt=1" << endl
@@ -91,25 +92,32 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service)
        cr = service.GetLastCheckResult();
 
        string plugin_output;
-       long start_time = -1, end_time = -1;
+       long schedule_start = -1, schedule_end = -1;
+       long execution_start = -1, execution_end = -1;
        if (cr) {
                cr->GetProperty("output", &plugin_output);
-               cr->GetProperty("start_time", &start_time);
-               cr->GetProperty("end_time", &end_time);
+               cr->GetProperty("schedule_start", &schedule_start);
+               cr->GetProperty("schedule_end", &schedule_end);
+               cr->GetProperty("execution_start", &execution_start);
+               cr->GetProperty("execution_end", &execution_end);
        }
 
+       long execution_time = (execution_start - execution_start);
+       long latency = (schedule_end - schedule_start) - execution_time;
+
        fp << "servicestatus {" << endl
            << "\t" << "host_name=" << service.GetHost().GetName() << endl
           << "\t" << "service_description=" << service.GetDisplayName() << endl
           << "\t" << "check_interval=" << service.GetCheckInterval() / 60.0 << endl
           << "\t" << "retry_interval=" << service.GetRetryInterval() / 60.0 << endl
-          << "\t" << "has_been_checked=" << (end_time == -1 ? 0 : 1) << endl
+          << "\t" << "has_been_checked=" << (cr ? 1 : 0) << endl
           << "\t" << "should_be_scheduled=1" << endl
-          << "\t" << "check_execution_time=" << end_time - start_time << endl
-          << "\t" << "check_latency=0" << endl
+          << "\t" << "check_execution_time=" << execution_time << endl
+          << "\t" << "check_latency=" << latency << endl
           << "\t" << "current_state=" << service.GetState() << endl
+          << "\t" << "state_type=" << service.GetStateType() << endl
           << "\t" << "plugin_output=" << plugin_output << endl
-          << "\t" << "last_check=" << start_time << endl
+          << "\t" << "last_check=" << schedule_start << endl
           << "\t" << "next_check=" << service.GetNextCheck() << endl
           << "\t" << "current_attempt=" << service.GetCurrentCheckAttempt() << endl
           << "\t" << "max_attempts=" << service.GetMaxCheckAttempts() << endl
@@ -155,6 +163,19 @@ void CompatComponent::StatusTimerHandler(void)
                 << "\t" << "}" << endl
                 << endl;
 
+       statusfp << "programstatus {" << endl
+                << "\t" << "daemon_mode=1" << endl
+                << "\t" << "program_start=" << IcingaApplication::GetInstance()->GetStartTime() << endl
+                << "\t" << "active_service_checks_enabled=1" << endl
+                << "\t" << "passive_service_checks_enabled=1" << endl
+                << "\t" << "active_host_checks_enabled=0" << endl
+                << "\t" << "passive_host_checks_enabled=0" << endl
+                << "\t" << "check_service_freshness=1" << endl
+                << "\t" << "check_host_freshness=0" << endl
+                << "\t" << "enable_flap_detection=1" << endl
+                << "\t" << "enable_failure_prediction=0" << endl
+                << endl;
+
        ofstream objectfp;
        objectfp.open("objects.cache.tmp", ofstream::out | ofstream::trunc);
 
index 4cef02fa97948d82734b6e42ef2f958cea31e53b..30e866446d5c41158f9bbc450c1d134f4ac87bf6 100644 (file)
@@ -15,30 +15,54 @@ Dictionary::Ptr CheckResult::GetDictionary(void) const
        return m_Data;
 }
 
-void CheckResult::SetStartTime(time_t ts)
+void CheckResult::SetScheduleStart(time_t ts)
 {
-       m_Data->SetProperty("start_time", static_cast<long>(ts));
+       m_Data->SetProperty("schedule_start", static_cast<long>(ts));
 }
 
-time_t CheckResult::GetStartTime(void) const
+time_t CheckResult::GetScheduleStart(void) const
 {
        long value = 0;
-       m_Data->GetProperty("start_time", &value);
+       m_Data->GetProperty("schedule_start", &value);
        return static_cast<time_t>(value);
 }
 
-void CheckResult::SetEndTime(time_t ts)
+void CheckResult::SetScheduleEnd(time_t ts)
 {
-       m_Data->SetProperty("end_time", static_cast<long>(ts));
+       m_Data->SetProperty("schedule_end", static_cast<long>(ts));
 }
 
-time_t CheckResult::GetEndTime(void) const
+time_t CheckResult::GetScheduleEnd(void) const
 {
        long value = 0;
-       m_Data->GetProperty("end_time", &value);
+       m_Data->GetProperty("schedule_end", &value);
        return static_cast<time_t>(value);
 }
 
+void CheckResult::SetExecutionStart(time_t ts)
+{
+       m_Data->SetProperty("execution_start", static_cast<long>(ts));
+}
+
+time_t CheckResult::GetExecutionStart(void) const
+{
+       long value = 0;
+       m_Data->GetProperty("execution_start", &value);
+       return static_cast<time_t>(value);
+}
+
+void CheckResult::SetExecutionEnd(time_t ts)
+{
+       m_Data->SetProperty("execution_end", static_cast<long>(ts));
+}
+
+time_t CheckResult::GetExecutionEnd(void) const
+{
+       long value = 0;
+       m_Data->GetProperty("execution_end", &value);
+       return value;
+}
+
 void CheckResult::SetState(ServiceState state)
 {
        m_Data->SetProperty("state", static_cast<long>(state));
index fbd4ebf3d3d52a7cf45e8cf0aae6fdd51ef61c21..7093a497a7f1cf8b83d2ea2f6f647b9f669579e0 100644 (file)
@@ -12,11 +12,17 @@ public:
 
        Dictionary::Ptr GetDictionary(void) const;
 
-       void SetStartTime(time_t ts);
-       time_t GetStartTime(void) const;
+       void SetScheduleStart(time_t ts);
+       time_t GetScheduleStart(void) const;
 
-       void SetEndTime(time_t ts);
-       time_t GetEndTime(void) const;
+       void SetScheduleEnd(time_t ts);
+       time_t GetScheduleEnd(void) const;
+
+       void SetExecutionStart(time_t ts);
+       time_t GetExecutionStart(void) const;
+
+       void SetExecutionEnd(time_t ts);
+       time_t GetExecutionEnd(void) const;
 
        void SetState(ServiceState state);
        ServiceState GetState(void) const;
index dc7b53f51dae58f472583514ae444a67b297567f..e94f7ed008eae795b218a2ba89a0cc4c40300c98 100644 (file)
@@ -10,11 +10,16 @@ CheckTask::CheckTask(const Service& service)
        : m_Service(service)
 { }
 
-Service CheckTask::GetService(void) const
+Service& CheckTask::GetService(void)
 {
        return m_Service;
 }
 
+CheckResult& CheckTask::GetResult(void)
+{
+       return m_Result;
+}
+
 void CheckTask::RegisterType(string type, Factory factory, QueueFlusher qflusher)
 {
        CheckTaskType ctt;
index 57bab28c691870337d980644023eb4163d6e26ed..92f4f04601145867b86fbde3c61751dee20bdbc3 100644 (file)
@@ -15,10 +15,10 @@ public:
        typedef function<CheckTask::Ptr(const Service&)> Factory;
        typedef function<void()> QueueFlusher;
 
-       Service GetService(void) const;
+       Service& GetService(void);
+       CheckResult& GetResult(void);
 
        virtual void Enqueue(void) = 0;
-       virtual CheckResult GetResult(void) = 0;
 
        static void RegisterType(string type, Factory factory, QueueFlusher qflusher);
        static CheckTask::Ptr CreateTask(const Service& service);
@@ -33,6 +33,7 @@ protected:
 
 private:
        Service m_Service;
+       CheckResult m_Result;
 
        static map<string, CheckTaskType> m_Types;
 
index e200eacd39b241487b95a5440f6f805098f4ff85..d4a92ae7817c90e5f26ecace84b3f33df2d19bae 100644 (file)
@@ -42,6 +42,8 @@ int IcingaApplication::Main(const vector<string>& args)
        Application::Log(LogInformation, "icinga", "Icinga component loader (version: " ICINGA_VERSION ")");
 #endif  /* _WIN32 */
 
+       time(&m_StartTime);
+
        if (args.size() < 2) {
                stringstream msgbuf;
                msgbuf << "Syntax: " << args[0] << " <config-file>";
@@ -150,3 +152,8 @@ string IcingaApplication::GetService(void) const
 {
        return m_Service;
 }
+
+time_t IcingaApplication::GetStartTime(void) const
+{
+       return m_StartTime;
+}
index ac0717408cb61edffe5112622d7cafaa09e7ae97..e9189b9676ce905ef75564fe3a1bb290f9eb4107 100644 (file)
@@ -43,12 +43,16 @@ public:
        string GetNode(void) const;
        string GetService(void) const;
 
+       time_t GetStartTime(void) const;
+
 private:
        string m_CertificateFile;
        string m_CAFile;
        string m_Node;
        string m_Service;
 
+       time_t m_StartTime;
+
        void NewComponentHandler(const ConfigObject::Ptr& object);
        void DeletedComponentHandler(const ConfigObject::Ptr& object);
 };
index ec39c6af22288358906d7e051aa9159199c243f4..39b2d31eb2a42b9ff864c9c15c024d0a43aac7de 100644 (file)
@@ -22,7 +22,7 @@ void NagiosCheckTask::Enqueue(void)
 {
        time_t now;
        time(&now);
-       m_Result.SetStartTime(now);
+       GetResult().SetScheduleStart(now);
 
        m_PendingTasks.push_back(GetSelf());
 }
@@ -37,11 +37,6 @@ void NagiosCheckTask::FlushQueue(void)
        }
 }
 
-CheckResult NagiosCheckTask::GetResult(void)
-{
-       return m_Result;
-}
-
 void NagiosCheckTask::CheckThreadProc(void)
 {
        mutex::scoped_lock lock(m_Mutex);
@@ -76,15 +71,22 @@ void NagiosCheckTask::CheckThreadProc(void)
 #endif /* _MSC_VER */
 
                        for (it = tasks.begin(); it != tasks.end(); ) {
+                               int fd = it->first;
+                               NagiosCheckTask::Ptr task = it->second;
+
 #ifndef _MSC_VER
-                               if (!FD_ISSET(it->first, &readfds)) {
+                               if (!FD_ISSET(fd, &readfds)) {
                                        it++;
                                        continue;
                                }
 #endif /* _MSC_VER */
 
-                               if (!it->second->RunTask()) {
-                                       CheckTask::FinishTask(it->second);
+                               if (!task->RunTask()) {
+                                       time_t now;
+                                       time(&now);
+                                       task->GetResult().SetScheduleEnd(now);
+
+                                       CheckTask::FinishTask(task);
                                        prev = it;
                                        it++;
                                        tasks.erase(prev);
@@ -100,6 +102,10 @@ void NagiosCheckTask::CheckThreadProc(void)
                        NagiosCheckTask::Ptr task = m_Tasks.front();
                        m_Tasks.pop_front();
                        if (!task->InitTask()) {
+                               time_t now;
+                               time(&now);
+                               task->GetResult().SetScheduleEnd(now);
+
                                CheckTask::FinishTask(task);
                        } else {
                                int fd = task->GetFD();
@@ -128,7 +134,15 @@ bool NagiosCheckTask::InitTask(void)
                m_FP = popen(m_Command.c_str(), "r");
 #endif /* _MSC_VER */
 
-       return (m_FP != NULL);
+       if (m_FP == NULL) {
+               time_t now;
+               time(&now);
+               GetResult().SetExecutionEnd(now);
+
+               return false;
+       }
+
+       return true;
 }
 
 bool NagiosCheckTask::RunTask(void)
@@ -144,7 +158,7 @@ bool NagiosCheckTask::RunTask(void)
 
        string output = m_OutputStream.str();
        boost::algorithm::trim(output);
-       m_Result.SetOutput(output);
+       GetResult().SetOutput(output);
 
        int status, exitcode;
 #ifdef _MSC_VER
@@ -182,19 +196,19 @@ bool NagiosCheckTask::RunTask(void)
                                break;
                }
 
-               m_Result.SetState(state);
+               GetResult().SetState(state);
 #ifndef _MSC_VER
        } else if (WIFSIGNALED(status)) {
                stringstream outputbuf;
                outputbuf << "Process was terminated by signal " << WTERMSIG(status);
-               m_Result.SetOutput(outputbuf.str());
-               m_Result.SetState(StateUnknown);
+               GetResult().SetOutput(outputbuf.str());
+               GetResult().SetState(StateUnknown);
        }
 #endif /* _MSC_VER */
 
        time_t now;
        time(&now);
-       m_Result.SetEndTime(now);
+       GetResult().SetExecutionEnd(now);
 
        return false;
 }
index 55643eb16685582f6e2f7392b008b0fa6e38d81a..422e901817190e5a0698da41d9d67b229a4f6eff 100644 (file)
@@ -15,7 +15,6 @@ public:
        NagiosCheckTask(const Service& service);
 
        virtual void Enqueue(void);
-       virtual CheckResult GetResult(void);
 
        static CheckTask::Ptr CreateTask(const Service& service);
        static void FlushQueue(void);
@@ -24,7 +23,6 @@ public:
 
 private:
        string m_Command;
-       CheckResult m_Result;
 
        FILE *m_FP;
        stringstream m_OutputStream;
index 6f64ddd5fbbd42a381d8adc43eb97bba582266e4..c630b0536706d88e00676f1c1ded7ce259067a5a 100644 (file)
@@ -179,8 +179,9 @@ void Service::SetLastStateChange(time_t ts)
 
 time_t Service::GetLastStateChange(void) const
 {
-       long value = 0;
-       GetConfigObject()->GetTag("last_state_change", &value);
+       long value;
+       if (!GetConfigObject()->GetTag("last_state_change", &value))
+               value = IcingaApplication::GetInstance()->GetStartTime();
        return value;
 }
 
@@ -191,8 +192,9 @@ void Service::SetLastHardStateChange(time_t ts)
 
 time_t Service::GetLastHardStateChange(void) const
 {
-       long value = 0;
-       GetConfigObject()->GetTag("last_hard_state_change", &value);
+       long value;
+       if (!GetConfigObject()->GetTag("last_hard_state_change", &value))
+               value = IcingaApplication::GetInstance()->GetStartTime();
        return value;
 }