[icinga-checkins] icinga.org: icinga2/master: Implement enable_checks property for services.

git at icinga.org git at icinga.org
Tue Jan 22 16:01:33 CET 2013


Module: icinga2
Branch: master
Commit: 71ec1d2b958d5d06afe88555f056053c871677d4
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=71ec1d2b958d5d06afe88555f056053c871677d4

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Jan 22 16:01:08 2013 +0100

Implement enable_checks property for services.

Fixes #3550

---

 components/checker/checkercomponent.cpp |   14 ++++++++++++
 components/compat/compatcomponent.cpp   |    4 +-
 docs/icinga2-config.txt                 |   12 +++++++++++
 lib/icinga/externalcommand.cpp          |   33 ++++++++++++++++++++++++++++-
 lib/icinga/externalcommand.h            |    2 +
 lib/icinga/host.cpp                     |    4 +++
 lib/icinga/service.cpp                  |   34 ++++++++++++++++++++++++++++++-
 lib/icinga/service.h                    |    6 +++++
 8 files changed, 104 insertions(+), 5 deletions(-)

diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp
index 98fc7f1..b35b516 100644
--- a/components/checker/checkercomponent.cpp
+++ b/components/checker/checkercomponent.cpp
@@ -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) {
diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp
index 85a7093..c777bea 100644
--- a/components/compat/compatcomponent.cpp
+++ b/components/compat/compatcomponent.cpp
@@ -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";
diff --git a/docs/icinga2-config.txt b/docs/icinga2-config.txt
index 8873a06..e3e5885 100644
--- a/docs/icinga2-config.txt
+++ b/docs/icinga2-config.txt
@@ -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
 ~~~~~~~~~~~~~~~
 
diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp
index 7fafe7f..fc583ae 100644
--- a/lib/icinga/externalcommand.cpp
+++ b/lib/icinga/externalcommand.cpp
@@ -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);
+}
+
diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h
index f46601e..dea2f72 100644
--- a/lib/icinga/externalcommand.h
+++ b/lib/icinga/externalcommand.h
@@ -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;
diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp
index 7fafd78..a289252 100644
--- a/lib/icinga/host.cpp
+++ b/lib/icinga/host.cpp
@@ -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)
diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp
index 704da88..ef9b94d 100644
--- a/lib/icinga/service.cpp
+++ b/lib/icinga/service.cpp
@@ -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();
diff --git a/lib/icinga/service.h b/lib/icinga/service.h
index 65331c2..99b1439 100644
--- a/lib/icinga/service.h
+++ b/lib/icinga/service.h
@@ -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);





More information about the icinga-checkins mailing list