[icinga-checkins] icinga.org: icinga2/master: ExternalCommand: use exceptions instead of return values.

git at icinga.org git at icinga.org
Tue Jan 22 15:15:09 CET 2013


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

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Jan 22 15:13:51 2013 +0100

ExternalCommand: use exceptions instead of return values.

Fixes #3549

---

 components/compat/compatcomponent.cpp |   15 ++++++++----
 lib/icinga/externalcommand.cpp        |   42 ++++++++++++++++----------------
 lib/icinga/externalcommand.h          |   12 ++++----
 3 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp
index eb5befb..85a7093 100644
--- a/components/compat/compatcomponent.cpp
+++ b/components/compat/compatcomponent.cpp
@@ -174,12 +174,17 @@ void CompatComponent::ProcessCommand(const String& command)
 		return;
 	}
 
-	stringstream msgbuf;
-	msgbuf << "Received command (@" << ts << "), command: " << argv[0] << ", " << argv.size() - 1 << " arguments; raw: " << command;
-	Logger::Write(LogInformation, "compat", msgbuf.str());
-
 	vector<String> argvExtra(argv.begin() + 1, argv.end());
-	ExternalCommand::Execute(ts, argv[0], argvExtra);
+
+	try {
+		Logger::Write(LogInformation, "compat", "Executing external command: " + command);
+
+		ExternalCommand::Execute(ts, argv[0], argvExtra);
+	} catch (const exception& ex) {
+		stringstream msgbuf;
+		msgbuf << "External command failed: " << ex.what();
+		Logger::Write(LogWarning, "compat", msgbuf.str());
+	}
 }
 #endif /* _WIN32 */
 
diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp
index 8784e69..7fafe7f 100644
--- a/lib/icinga/externalcommand.cpp
+++ b/lib/icinga/externalcommand.cpp
@@ -24,7 +24,7 @@ using namespace icinga;
 bool I2_EXPORT ExternalCommand::m_Initialized;
 map<String, ExternalCommand::Callback> I2_EXPORT ExternalCommand::m_Commands;
 
-int ExternalCommand::Execute(double time, const String& command, const vector<String>& arguments)
+void ExternalCommand::Execute(double time, const String& command, const vector<String>& arguments)
 {
 	if (!m_Initialized) {
 		RegisterCommand("HELLO_WORLD", &ExternalCommand::HelloWorld);
@@ -39,9 +39,9 @@ int ExternalCommand::Execute(double time, const String& command, const vector<St
 	it = m_Commands.find(command);
 
 	if (it == m_Commands.end())
-		return -1;
+		throw_exception(invalid_argument("The external command '" + command + "' does not exist."));
 
-	return it->second(time, arguments);
+	it->second(time, arguments);
 }
 
 void ExternalCommand::RegisterCommand(const String& command, const ExternalCommand::Callback& callback)
@@ -49,20 +49,18 @@ void ExternalCommand::RegisterCommand(const String& command, const ExternalComma
 	m_Commands[command] = callback;
 }
 
-int ExternalCommand::HelloWorld(double time, const vector<String>& arguments)
+void ExternalCommand::HelloWorld(double time, const vector<String>& arguments)
 {
 	Logger::Write(LogInformation, "icinga", "HelloWorld external command called.");
-
-	return 0;
 }
 
-int ExternalCommand::ProcessServiceCheckResult(double time, const vector<String>& arguments)
+void ExternalCommand::ProcessServiceCheckResult(double time, const vector<String>& arguments)
 {
 	if (arguments.size() < 4)
-		return -1;
+		throw_exception(invalid_argument("Expected 4 arguments."));
 
 	if (!Service::Exists(arguments[1]))
-		return -1;
+		throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
 
 	Service::Ptr service = Service::GetByName(arguments[1]);
 
@@ -76,43 +74,45 @@ int ExternalCommand::ProcessServiceCheckResult(double time, const vector<String>
 	result->Set("execution_start", time);
 	result->Set("execution_end", time);
 
+	Logger::Write(LogInformation, "icinga", "Processing passive check result for service '" + arguments[1] + "'");
 	service->ProcessCheckResult(result);
-
-	return 0;
 }
 
-int ExternalCommand::ScheduleSvcCheck(double time, const vector<String>& arguments)
+void ExternalCommand::ScheduleSvcCheck(double time, const vector<String>& arguments)
 {
 	if (arguments.size() < 3)
-		return -1;
+		throw_exception(invalid_argument("Expected 3 arguments."));
 
 	if (!Service::Exists(arguments[1]))
-		return -1;
+		throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
 
 	Service::Ptr service = Service::GetByName(arguments[1]);
 
 	double planned_check = arguments[2].ToDouble();
 
-	if (planned_check > service->GetNextCheck())
-		return -1;
+	if (planned_check > service->GetNextCheck()) {
+		Logger::Write(LogInformation, "icinga", "Ignoring reschedule request for service '" +
+		    arguments[1] + "' (next check is already sooner than requested check time)");
+		return;
+	}
 
+	Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'");
 	service->SetNextCheck(planned_check);
-
-	return 0;
 }
 
-int ExternalCommand::ScheduleForcedSvcCheck(double time, const vector<String>& arguments)
+void ExternalCommand::ScheduleForcedSvcCheck(double time, const vector<String>& arguments)
 {
 	if (arguments.size() < 3)
-		return -1;
+		throw_exception(invalid_argument("Expected 3 arguments."));
 
 	if (!Service::Exists(arguments[1]))
-		return -1;
+		throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
 
 	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->SetNextCheck(arguments[2].ToDouble());
 }
 
diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h
index 4150c74..f46601e 100644
--- a/lib/icinga/externalcommand.h
+++ b/lib/icinga/externalcommand.h
@@ -26,15 +26,15 @@ namespace icinga
 class I2_ICINGA_API ExternalCommand {
 public:
 
-	static int Execute(double time, const String& command, const vector<String>& arguments);
+	static void Execute(double time, const String& command, const vector<String>& arguments);
 
-	static int HelloWorld(double time, const vector<String>& arguments);
-	static int ProcessServiceCheckResult(double time, const vector<String>& arguments);
-	static int ScheduleSvcCheck(double time, const vector<String>& arguments);
-	static int ScheduleForcedSvcCheck(double time, const vector<String>& arguments);
+	static void HelloWorld(double time, const vector<String>& arguments);
+	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);
 
 private:
-	typedef function<int (double time, const vector<String>& arguments)> Callback;
+	typedef function<void (double time, const vector<String>& arguments)> Callback;
 
 	static bool m_Initialized;
 	static map<String, Callback> m_Commands;





More information about the icinga-checkins mailing list