Implement override_check_command.

Refs #4746
This commit is contained in:
Gunnar Beutner 2013-11-26 12:56:42 +01:00
parent b9f9cd03d2
commit 1c8fb7a198
6 changed files with 77 additions and 2 deletions

View File

@ -193,6 +193,8 @@ void ExternalCommandProcessor::Initialize(void)
RegisterCommand("DISABLE_SVC_EVENT_HANDLER", &ExternalCommandProcessor::DisableSvcEventHandler); RegisterCommand("DISABLE_SVC_EVENT_HANDLER", &ExternalCommandProcessor::DisableSvcEventHandler);
RegisterCommand("CHANGE_HOST_EVENT_HANDLER", &ExternalCommandProcessor::ChangeHostEventHandler); RegisterCommand("CHANGE_HOST_EVENT_HANDLER", &ExternalCommandProcessor::ChangeHostEventHandler);
RegisterCommand("CHANGE_SVC_EVENT_HANDLER", &ExternalCommandProcessor::ChangeSvcEventHandler); 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) 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]); Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
if (!service) 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]); EventCommand::Ptr command = EventCommand::GetByName(arguments[2]);
@ -2069,3 +2071,53 @@ void ExternalCommandProcessor::ChangeSvcEventHandler(double time, const std::vec
service->SetEventCommand(command); 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);
}
}

View File

@ -143,6 +143,8 @@ private:
static void DisableSvcEventHandler(double time, const std::vector<String>& arguments); static void DisableSvcEventHandler(double time, const std::vector<String>& arguments);
static void ChangeHostEventHandler(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 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);
}; };
} }

View File

@ -47,7 +47,19 @@ boost::signals2::signal<void (const Service::Ptr&, FlappingState)> Service::OnFl
CheckCommand::Ptr Service::GetCheckCommand(void) const 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 TimePeriod::Ptr Service::GetCheckPeriod(void) const

View File

@ -304,6 +304,9 @@ int Service::GetModifiedAttributes(void) const
if (!GetOverrideEventCommand().IsEmpty()) if (!GetOverrideEventCommand().IsEmpty())
attrs |= ModAttrEventHandlerCommand; attrs |= ModAttrEventHandlerCommand;
if (!GetOverrideCheckCommand().IsEmpty())
attrs |= ModAttrCheckCommand;
// TODO: finish // TODO: finish
return attrs; return attrs;
@ -337,6 +340,9 @@ void Service::SetModifiedAttributes(int flags)
if ((flags & ModAttrEventHandlerCommand) == 0) if ((flags & ModAttrEventHandlerCommand) == 0)
SetOverrideEventCommand(Empty); SetOverrideEventCommand(Empty);
if ((flags & ModAttrCheckCommand) == 0)
SetOverrideCheckCommand(Empty);
} }
bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const

View File

@ -109,6 +109,8 @@ public:
/* Checks */ /* Checks */
shared_ptr<CheckCommand> GetCheckCommand(void) const; shared_ptr<CheckCommand> GetCheckCommand(void) const;
void SetCheckCommand(const shared_ptr<CheckCommand>& command);
TimePeriod::Ptr GetCheckPeriod(void) const; TimePeriod::Ptr GetCheckPeriod(void) const;
double GetCheckInterval(void) const; double GetCheckInterval(void) const;

View File

@ -137,6 +137,7 @@ class Service : DynamicObject
[state] Value override_retry_interval; [state] Value override_retry_interval;
[state] Value override_enable_event_handler; [state] Value override_enable_event_handler;
[state] Value override_event_command; [state] Value override_event_command;
[state] Value override_check_command;
}; };
} }