From: Gunnar Beutner Date: Tue, 22 Jan 2013 14:13:51 +0000 (+0100) Subject: ExternalCommand: use exceptions instead of return values. X-Git-Tag: v0.0.2~684 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f6949545124cd4aa269ec92f21086a904444b647;p=icinga2 ExternalCommand: use exceptions instead of return values. Fixes #3549 --- diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index eb5befb0a..85a709351 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 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 8784e693a..7fafe7f2b 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 I2_EXPORT ExternalCommand::m_Commands; -int ExternalCommand::Execute(double time, const String& command, const vector& arguments) +void ExternalCommand::Execute(double time, const String& command, const vector& arguments) { if (!m_Initialized) { RegisterCommand("HELLO_WORLD", &ExternalCommand::HelloWorld); @@ -39,9 +39,9 @@ int ExternalCommand::Execute(double time, const String& command, const vectorsecond(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& arguments) +void ExternalCommand::HelloWorld(double time, const vector& arguments) { Logger::Write(LogInformation, "icinga", "HelloWorld external command called."); - - return 0; } -int ExternalCommand::ProcessServiceCheckResult(double time, const vector& arguments) +void ExternalCommand::ProcessServiceCheckResult(double time, const vector& 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 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& arguments) +void ExternalCommand::ScheduleSvcCheck(double time, const vector& 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& arguments) +void ExternalCommand::ScheduleForcedSvcCheck(double time, const vector& 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 4150c749a..f46601e5a 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& arguments); + static void Execute(double time, const String& command, const vector& arguments); - static int HelloWorld(double time, const vector& arguments); - static int ProcessServiceCheckResult(double time, const vector& arguments); - static int ScheduleSvcCheck(double time, const vector& arguments); - static int ScheduleForcedSvcCheck(double time, const vector& arguments); + static void HelloWorld(double time, const vector& arguments); + static void ProcessServiceCheckResult(double time, const vector& arguments); + static void ScheduleSvcCheck(double time, const vector& arguments); + static void ScheduleForcedSvcCheck(double time, const vector& arguments); private: - typedef function& arguments)> Callback; + typedef function& arguments)> Callback; static bool m_Initialized; static map m_Commands;