[icinga-checkins] icinga.org: icinga2/feature/modified-attributes-4746: Implement override_check_command.

git at icinga.org git at icinga.org
Tue Nov 26 12:56:52 CET 2013


Module: icinga2
Branch: feature/modified-attributes-4746
Commit: ab48c5035f3f7079c9de279c2ecb43a229685d8b
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=ab48c5035f3f7079c9de279c2ecb43a229685d8b

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Nov 26 12:56:42 2013 +0100

Implement override_check_command.

Refs #4746

---

 lib/icinga/externalcommandprocessor.cpp |   54 ++++++++++++++++++++++++++++++-
 lib/icinga/externalcommandprocessor.h   |    2 ++
 lib/icinga/service-check.cpp            |   14 +++++++-
 lib/icinga/service.h                    |    2 ++
 lib/icinga/service.ti                   |    1 +
 5 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp
index 8a79342..77d5d5b 100644
--- a/lib/icinga/externalcommandprocessor.cpp
+++ b/lib/icinga/externalcommandprocessor.cpp
@@ -193,6 +193,8 @@ void ExternalCommandProcessor::Initialize(void)
 	RegisterCommand("DISABLE_SVC_EVENT_HANDLER", &ExternalCommandProcessor::DisableSvcEventHandler);
 	RegisterCommand("CHANGE_HOST_EVENT_HANDLER", &ExternalCommandProcessor::ChangeHostEventHandler);
 	RegisterCommand("CHANGE_SVC_EVENT_HANDLER", &ExternalCommandProcessor::ChangeSvcEventHandler);
+	RegisterCommand("CHANGE_HOST_CHECK_COMMAND", &ExternalCommandProcessor::ChangeHostCheckCommand);
+	RegisterCommand("CHANGE_SVC_CHECK_COMMAND", &ExternalCommandProcessor::ChangeSvcCheckCommand);
 }
 
 void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandProcessor::Callback& callback)
@@ -2054,7 +2056,7 @@ void ExternalCommandProcessor::ChangeSvcEventHandler(double time, const std::vec
 	Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
 	if (!service)
-		BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot change event handler non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot change event handler for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
 
 	EventCommand::Ptr command = EventCommand::GetByName(arguments[2]);
 
@@ -2069,3 +2071,53 @@ void ExternalCommandProcessor::ChangeSvcEventHandler(double time, const std::vec
 		service->SetEventCommand(command);
 	}
 }
+
+void ExternalCommandProcessor::ChangeHostCheckCommand(double time, const std::vector<String>& arguments)
+{
+	if (arguments.size() < 2)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 2 arguments."));
+
+	Host::Ptr host = Host::GetByName(arguments[0]);
+
+	if (!host)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot change check command for non-existent host '" + arguments[0] + "'"));
+
+	Service::Ptr hc = host->GetCheckService();
+
+	CheckCommand::Ptr command = CheckCommand::GetByName(arguments[2]);
+
+	if (!command)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Check command '" + arguments[1] + "' does not exist."));
+
+	Log(LogInformation, "icinga", "Changing check command for host '" + arguments[0] + "' to '" + arguments[1] + "'");
+
+	{
+		ObjectLock olock(hc);
+
+		hc->SetCheckCommand(command);
+	}
+}
+
+void ExternalCommandProcessor::ChangeSvcCheckCommand(double time, const std::vector<String>& arguments)
+{
+	if (arguments.size() < 2)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 3 arguments."));
+
+	Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
+
+	if (!service)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot change check command for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
+	CheckCommand::Ptr command = CheckCommand::GetByName(arguments[2]);
+
+	if (!command)
+		BOOST_THROW_EXCEPTION(std::invalid_argument("Check command '" + arguments[2] + "' does not exist."));
+
+	Log(LogInformation, "icinga", "Changing check command for service '" + arguments[1] + "' to '" + arguments[2] + "'");
+
+	{
+		ObjectLock olock(service);
+
+		service->SetCheckCommand(command);
+	}
+}
diff --git a/lib/icinga/externalcommandprocessor.h b/lib/icinga/externalcommandprocessor.h
index 86679de..a8efbf3 100644
--- a/lib/icinga/externalcommandprocessor.h
+++ b/lib/icinga/externalcommandprocessor.h
@@ -143,6 +143,8 @@ private:
 	static void DisableSvcEventHandler(double time, const std::vector<String>& arguments);
 	static void ChangeHostEventHandler(double time, const std::vector<String>& arguments);
 	static void ChangeSvcEventHandler(double time, const std::vector<String>& arguments);
+	static void ChangeHostCheckCommand(double time, const std::vector<String>& arguments);
+	static void ChangeSvcCheckCommand(double time, const std::vector<String>& arguments);
 };
 
 }
diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp
index 2190861..2c4e293 100644
--- a/lib/icinga/service-check.cpp
+++ b/lib/icinga/service-check.cpp
@@ -47,7 +47,19 @@ boost::signals2::signal<void (const Service::Ptr&, FlappingState)> Service::OnFl
 
 CheckCommand::Ptr Service::GetCheckCommand(void) const
 {
-	return CheckCommand::GetByName(GetCheckCommandRaw());
+	String command;
+
+	if (!GetOverrideCheckCommand().IsEmpty())
+		command = GetOverrideCheckCommand();
+	else
+		command = GetCheckCommandRaw();
+
+	return CheckCommand::GetByName(command);
+}
+
+void Service::SetCheckCommand(const CheckCommand::Ptr& command)
+{
+	SetOverrideCheckCommand(command->GetName());
 }
 
 TimePeriod::Ptr Service::GetCheckPeriod(void) const
diff --git a/lib/icinga/service.h b/lib/icinga/service.h
index a2cda9a..4d23742 100644
--- a/lib/icinga/service.h
+++ b/lib/icinga/service.h
@@ -109,6 +109,8 @@ public:
 
 	/* Checks */
 	shared_ptr<CheckCommand> GetCheckCommand(void) const;
+	void SetCheckCommand(const shared_ptr<CheckCommand>& command);
+
 	TimePeriod::Ptr GetCheckPeriod(void) const;
 
 	double GetCheckInterval(void) const;
diff --git a/lib/icinga/service.ti b/lib/icinga/service.ti
index 04bc047..c59528a 100644
--- a/lib/icinga/service.ti
+++ b/lib/icinga/service.ti
@@ -137,6 +137,7 @@ class Service : DynamicObject
 	[state] Value override_retry_interval;
 	[state] Value override_enable_event_handler;
 	[state] Value override_event_command;
+	[state] Value override_check_command;
 };
 
 }





More information about the icinga-checkins mailing list