]> granicus.if.org Git - icinga2/commitdiff
Implement enable_checks property for services.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 22 Jan 2013 15:01:08 +0000 (16:01 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 22 Jan 2013 15:01:08 +0000 (16:01 +0100)
Fixes #3550

components/checker/checkercomponent.cpp
components/compat/compatcomponent.cpp
docs/icinga2-config.txt
lib/icinga/externalcommand.cpp
lib/icinga/externalcommand.h
lib/icinga/host.cpp
lib/icinga/service.cpp
lib/icinga/service.h

index 98fc7f1f1b02dbe632f0f0b66c9356009d016278..b35b51664160a7cd61824fef8d3fb54683d4b585 100644 (file)
@@ -70,6 +70,20 @@ void CheckerComponent::CheckTimerHandler(void)
 
                idx.erase(it);
 
+               /* reschedule the service if checks are currently disabled
+                * for it and this is not a forced check */
+               if (!service->GetEnableChecks()) {
+                       if (!service->GetForceNextCheck()) {
+                               service->UpdateNextCheck();
+
+                               idx.insert(service);
+
+                               return;
+                       }
+
+                       service->SetForceNextCheck(false);
+               }
+
                Dictionary::Ptr cr = service->GetLastCheckResult();
 
                if (cr) {
index 85a7093515a374b6c1c5366a81bd58792b20cac9..c777bea3f8318e4fd015aafe39e9124575561ace 100644 (file)
@@ -285,7 +285,7 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, const Service::Ptr& servic
           << "\t" << "last_state_change=" << service->GetLastStateChange() << "\n"
           << "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n"
           << "\t" << "last_update=" << time(NULL) << "\n"
-          << "\t" << "active_checks_enabled=1" << "\n"
+          << "\t" << "active_checks_enabled=" << (service->GetEnableChecks() ? 1 : 0) <<"\n"
           << "\t" << "passive_checks_enabled=1" << "\n"
           << "\t" << "}" << "\n"
           << "\n";
@@ -300,7 +300,7 @@ void CompatComponent::DumpServiceObject(ofstream& fp, const Service::Ptr& servic
           << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
           << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
           << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
-          << "\t" << "active_checks_enabled" << "\t" << 1 << "\n"
+          << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableChecks() ? 1 : 0) << "\n"
           << "\t" << "passive_checks_enabled" << "\t" << 1 << "\n"
           << "\t" << "}" << "\n"
           << "\n";
index 8873a0620bb436c8a8ff002af38fb57cdb075b39..e3e5885bbc2cb6e2330da58068e02c9bbbf1f534 100644 (file)
@@ -614,6 +614,12 @@ Attribute: checkers
 Optional. A list of remote endpoints that may check this service. Wildcards can
 be used here.
 
+Attribute: enable_checks
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+Optional. Whether active checks should be performed for this service. Defaults
+to 1 (true).
+
 Type: ServiceGroup
 ~~~~~~~~~~~~~~~~~~
 
@@ -747,6 +753,12 @@ Attribute: checkers
 Optional. Copied into inline service definitions. The host itself does not have
 any checks.
 
+Attribute: enable_checks
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+Optional. Copied into inline service definitions. The host itself does not have 
+any checks.
+
 Type: HostGroup
 ~~~~~~~~~~~~~~~
 
index 7fafe7f2b37bb4a9ad683e0554fe58c26058c9cc..fc583aebb9c1996bcc8323462e2bce4b1b5c71fb 100644 (file)
@@ -31,6 +31,8 @@ void ExternalCommand::Execute(double time, const String& command, const vector<S
                RegisterCommand("PROCESS_SERVICE_CHECK_RESULT", &ExternalCommand::ProcessServiceCheckResult);
                RegisterCommand("SCHEDULE_SVC_CHECK", &ExternalCommand::ScheduleSvcCheck);
                RegisterCommand("SCHEDULE_FORCED_SVC_CHECK", &ExternalCommand::ScheduleForcedSvcCheck);
+               RegisterCommand("ENABLE_SVC_CHECK", &ExternalCommand::EnableSvcCheck);
+               RegisterCommand("DISABLE_SVC_CHECK", &ExternalCommand::DisableSvcCheck);
 
                m_Initialized = true;
        }
@@ -110,9 +112,36 @@ void ExternalCommand::ScheduleForcedSvcCheck(double time, const vector<String>&
 
        Service::Ptr service = Service::GetByName(arguments[1]);
 
-       // TODO: force checks (once we have time periods)
-
        Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'");
+       service->SetForceNextCheck(true);
        service->SetNextCheck(arguments[2].ToDouble());
 }
 
+void ExternalCommand::EnableSvcCheck(double time, const vector<String>& arguments)
+{
+       if (arguments.size() < 2)
+               throw_exception(invalid_argument("Expected 2 arguments."));
+
+       if (!Service::Exists(arguments[1]))
+               throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
+
+       Service::Ptr service = Service::GetByName(arguments[1]);
+
+       Logger::Write(LogInformation, "icinga", "Enabling checks for service '" + arguments[1] + "'");
+       service->SetEnableChecks(true);
+}
+
+void ExternalCommand::DisableSvcCheck(double time, const vector<String>& arguments)
+{
+       if (arguments.size() < 2)
+               throw_exception(invalid_argument("Expected 2 arguments."));
+
+       if (!Service::Exists(arguments[1]))
+               throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
+
+       Service::Ptr service = Service::GetByName(arguments[1]);
+
+       Logger::Write(LogInformation, "icinga", "Disabling checks for service '" + arguments[1] + "'");
+       service->SetEnableChecks(false);
+}
+
index f46601e5a44c3ff7aca1c27ab1c9817faa3e1394..dea2f72c4bad01b0b5c5d121507dd655ff83e267 100644 (file)
@@ -32,6 +32,8 @@ public:
        static void ProcessServiceCheckResult(double time, const vector<String>& arguments);
        static void ScheduleSvcCheck(double time, const vector<String>& arguments);
        static void ScheduleForcedSvcCheck(double time, const vector<String>& arguments);
+       static void EnableSvcCheck(double time, const vector<String>& arguments);
+       static void DisableSvcCheck(double time, const vector<String>& arguments);
 
 private:
        typedef function<void (double time, const vector<String>& arguments)> Callback;
index 7fafd78958270076ed418c0b5a9ab31ebe55ad5b..a289252d74d8d10094a39327316c408109b422b5 100644 (file)
@@ -175,6 +175,10 @@ static void CopyServiceAttributes(const Host::Ptr& host, TDict serviceDesc,
        if (!hostchecks.IsEmpty())
                builder->AddExpression("dependencies", OperatorPlus,
                    Service::ResolveDependencies(host, hostchecks));
+
+       Value enableChecks = serviceDesc->Get("enable_checks");
+       if (!enableChecks.IsEmpty())
+               builder->AddExpression("enable_checks", OperatorSet, enableChecks);
 }
 
 void Host::ObjectCommittedHandler(const ConfigItem::Ptr& item)
index 704da8854f7ce2ea679c8a2eebdbfeaf5fd14717..ef9b94dbdf724fe2356a73b5c6cf8800c687c491 100644 (file)
@@ -41,7 +41,9 @@ static AttributeDescription serviceAttributes[] = {
        { "state_type", Attribute_Replicated },
        { "last_result", Attribute_Replicated },
        { "last_state_change", Attribute_Replicated },
-       { "last_hard_state_change", Attribute_Replicated }
+       { "last_hard_state_change", Attribute_Replicated },
+       { "enable_checks", Attribute_Replicated },
+       { "force_next_check", Attribute_Replicated }
 };
 
 REGISTER_TYPE(Service, serviceAttributes);
@@ -353,6 +355,36 @@ double Service::GetLastHardStateChange(void) const
        return value;
 }
 
+bool Service::GetEnableChecks(void) const
+{
+       Value value = Get("enable_checks");
+
+       if (value.IsEmpty())
+               return true;
+
+       return static_cast<bool>(value);
+}
+
+void Service::SetEnableChecks(bool enabled)
+{
+       Set("enable_checks", enabled ? 1 : 0);
+}
+
+bool Service::GetForceNextCheck(void) const
+{
+       Value value = Get("force_next_check");
+
+       if (value.IsEmpty())
+               return false;
+
+       return static_cast<bool>(value);
+}
+
+void Service::SetForceNextCheck(bool forced)
+{
+       Set("force_next_check", forced ? 1 : 0);
+}
+
 void Service::ApplyCheckResult(const Dictionary::Ptr& cr)
 {
        ServiceState old_state = GetState();
index 65331c289d94b077976f43e798fa40f92ab2a795..99b14395040448715f8a7f6c7ad303fbfb91a607 100644 (file)
@@ -116,6 +116,12 @@ public:
        void SetLastHardStateChange(double ts);
        double GetLastHardStateChange(void) const;
 
+       bool GetEnableChecks(void) const;
+       void SetEnableChecks(bool enabled);
+
+       bool GetForceNextCheck(void) const;
+       void SetForceNextCheck(bool forced);
+
        void ApplyCheckResult(const Dictionary::Ptr& cr);
 
        void BeginExecuteCheck(const function<void (void)>& callback);