ExternalCommand: use exceptions instead of return values.

Fixes #3549
This commit is contained in:
Gunnar Beutner 2013-01-22 15:13:51 +01:00
parent 9ed49bd6e1
commit f694954512
3 changed files with 37 additions and 32 deletions

View File

@ -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 */

View File

@ -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());
}

View File

@ -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;