]> granicus.if.org Git - icinga2/commitdiff
Don't run checks for services which have pending checks.
authorGunnar Beutner <gunnar@beutner.name>
Sun, 17 Jun 2012 20:46:40 +0000 (22:46 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Sun, 17 Jun 2012 20:46:40 +0000 (22:46 +0200)
components/checker/checkercomponent.cpp
components/checker/checkercomponent.h
icinga/checktask.cpp
icinga/checktask.h
icinga/nagioschecktask.cpp
icinga/service.cpp
icinga/service.h

index f22ececedc372a306c0ee27efc09bfd551e04263..ce36421dae6374f33e4f16e32aed443b19693844 100644 (file)
@@ -70,23 +70,23 @@ void CheckerComponent::CheckTimerHandler(void)
        for (;;) {
                Service service = m_Services.top();
 
-               if (service.GetNextCheck() > now)
+               if (service.GetNextCheck() > now || service.HasPendingCheck())
                        break;
 
+               m_Services.pop();
+               service.SetPendingCheck(true);
+
                Application::Log(LogInformation, "checker", "Executing service check for '" + service.GetName() + "'");
 
                CheckTask::Ptr task = CheckTask::CreateTask(service);
                task->Execute();
                m_PendingTasks.push_back(task);
 
-               m_Services.pop();
                service.SetNextCheck(now + service.GetCheckInterval());
                m_Services.push(service);
        }
 
-       /* adjust next call time for the check timer */
-       Service service = m_Services.top();
-       m_CheckTimer->SetInterval(service.GetNextCheck() - now);
+       AdjustCheckTimer();
 }
 
 void CheckerComponent::ResultTimerHandler(void)
@@ -101,11 +101,31 @@ void CheckerComponent::ResultTimerHandler(void)
                        continue;
                }
 
+               task->GetService().SetPendingCheck(false);
+
                CheckResult result = task->GetResult();
                Application::Log(LogInformation, "checker", "Got result! Plugin output: " + result.Output);
        }
 
        m_PendingTasks = unfinishedTasks;
+
+       AdjustCheckTimer();
+}
+
+void CheckerComponent::AdjustCheckTimer(void)
+{
+       if (m_Services.size() == 0)
+               return;
+
+       /* adjust next call time for the check timer */
+       Service service = m_Services.top();
+
+       if (service.HasPendingCheck()) {
+               m_CheckTimer->Stop();
+       } else {
+               m_CheckTimer->SetInterval(service.GetNextCheck() - time(NULL));
+               m_CheckTimer->Start();
+       }
 }
 
 void CheckerComponent::AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
index 3da7e721e760a56f61708d17cbc406d666a2ee38..da924d47c1b1c32aa857e34f110dba51760d3e0f 100644 (file)
 namespace icinga
 {
 
-struct ServiceNextCheckGreaterComparer
+struct ServiceCheckPriorityLessComparer
 {
 public:
        bool operator()(const Service& a, const Service& b)
        {
+               if (a.HasPendingCheck() && !b.HasPendingCheck())
+                       return true;
+
                return a.GetNextCheck() > b.GetNextCheck();
        }
 };
@@ -41,7 +44,7 @@ public:
        typedef shared_ptr<CheckerComponent> Ptr;
        typedef weak_ptr<CheckerComponent> WeakPtr;
 
-       typedef priority_queue<Service, vector<Service>, ServiceNextCheckGreaterComparer> ServiceQueue;
+       typedef priority_queue<Service, vector<Service>, ServiceCheckPriorityLessComparer> ServiceQueue;
 
        virtual string GetName(void) const;
        virtual void Start(void);
@@ -58,6 +61,8 @@ private:
        void CheckTimerHandler(void);
        void ResultTimerHandler(void);
 
+       void AdjustCheckTimer(void);
+
        void AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
        void RevokeServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
        void ClearServicesRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
index f346053fec6650b943a047693f37b88d6cd00961..a82ffd7fd63f3a159f189fa5ef6956c16440a451 100644 (file)
@@ -4,6 +4,15 @@ using namespace icinga;
 
 map<string, CheckTask::Factory> CheckTask::m_Types;
 
+CheckTask::CheckTask(const Service& service)
+       : m_Service(service)
+{ }
+
+Service CheckTask::GetService(void) const
+{
+       return m_Service;
+}
+
 void CheckTask::RegisterType(string type, Factory factory)
 {
        m_Types[type] = factory;
index 091e0c27cbcbca39f912d37abc1a3ef72c4d45dd..045378c14f666f3a9ac636091840736a1240e47e 100644 (file)
@@ -32,6 +32,8 @@ public:
 
        typedef function<CheckTask::Ptr(const Service&)> Factory;
 
+       Service GetService(void) const;
+
        virtual void Execute(void) = 0;
        virtual bool IsFinished(void) const = 0;
        virtual CheckResult GetResult(void) = 0;
@@ -39,7 +41,12 @@ public:
        static void RegisterType(string type, Factory factory);
        static CheckTask::Ptr CreateTask(const Service& service);
 
+protected:
+       CheckTask(const Service& service);
+
 private:
+       Service m_Service;
+
        static map<string, Factory> m_Types;
 };
 
index 60b69efca169f851189c5faa197ff4434781310a..4dd6f7d2ddd2a46908796139f44128f29018969e 100644 (file)
@@ -3,6 +3,7 @@
 using namespace icinga;
 
 NagiosCheckTask::NagiosCheckTask(const Service& service)
+       : CheckTask(service)
 {
        string checkCommand = service.GetCheckCommand();
        m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros()) + " 2>&1";
index e60198673c8a44b629b0872976214d1c38bef35f..31405cfaefc786c2ec75aecdbe55af96c91bea54 100644 (file)
@@ -86,3 +86,15 @@ string Service::GetChecker(void) const
        GetConfigObject()->GetTag("checker", &value);
        return value;
 }
+
+void Service::SetPendingCheck(bool pending)
+{
+       GetConfigObject()->SetTag("pendingCheck", pending);
+}
+
+bool Service::HasPendingCheck(void) const
+{
+       bool value = false;
+       GetConfigObject()->GetTag("pendingCheck", &value);
+       return value;
+}
\ No newline at end of file
index a2680dea44cd43487154146548d8bff5bf2931f9..06dff6641f7671c2cd8ba907ad079c2ac395c98a 100644 (file)
@@ -24,6 +24,8 @@ public:
        time_t GetNextCheck(void) const;
        void SetChecker(string checker);
        string GetChecker(void) const;
+       void SetPendingCheck(bool pending);
+       bool HasPendingCheck(void) const;
 };
 
 }