Use ExecuteOverride to override the command

This commit is contained in:
Mattia Codato 2020-07-31 17:28:33 +02:00
parent d7dadbfc66
commit 9c4a3aed1b
16 changed files with 74 additions and 11 deletions

View File

@ -686,14 +686,24 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
CheckCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd)
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
else
else {
CheckCommand::ExecuteOverride = cmd;
Defer resetCheckCommandOverride([]() {
CheckCommand::ExecuteOverride = nullptr;
});
cmd->Execute(checkable, cr, execMacros, false);
}
} else if (command_type == "EventCommand") {
EventCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(EventCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd)
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
else
else {
EventCommand::ExecuteOverride = cmd;
Defer resetCheckCommandOverride([]() {
EventCommand::ExecuteOverride = nullptr;
});
cmd->Execute(checkable, execMacros, false);
}
} else if (command_type == "NotificationCommand") {
NotificationCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(NotificationCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd)
@ -731,6 +741,11 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'.");
execParams->Set("notification", notification->GetName());
NotificationCommand::ExecuteOverride = cmd;
Defer resetCheckCommandOverride([]() {
NotificationCommand::ExecuteOverride = nullptr;
});
cmd->Execute(notification, user, cr, NotificationType::NotificationCustom,
ActionsHandler::AuthenticatedApiUser->GetName(), "", execMacros, false);
}

View File

@ -8,6 +8,8 @@ using namespace icinga;
REGISTER_TYPE(CheckCommand);
thread_local CheckCommand::Ptr CheckCommand::ExecuteOverride;
void CheckCommand::Execute(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{

View File

@ -20,6 +20,8 @@ public:
DECLARE_OBJECT(CheckCommand);
DECLARE_OBJECTNAME(CheckCommand);
static thread_local CheckCommand::Ptr ExecuteOverride;
virtual void Execute(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros = nullptr,
bool useResolvedMacros = false);

View File

@ -7,6 +7,8 @@ using namespace icinga;
REGISTER_TYPE(EventCommand);
thread_local EventCommand::Ptr EventCommand::ExecuteOverride;
void EventCommand::Execute(const Checkable::Ptr& checkable,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{

View File

@ -20,6 +20,8 @@ public:
DECLARE_OBJECT(EventCommand);
DECLARE_OBJECTNAME(EventCommand);
static thread_local EventCommand::Ptr ExecuteOverride;
virtual void Execute(const Checkable::Ptr& checkable,
const Dictionary::Ptr& resolvedMacros = nullptr,
bool useResolvedMacros = false);

View File

@ -7,6 +7,8 @@ using namespace icinga;
REGISTER_TYPE(NotificationCommand);
thread_local NotificationCommand::Ptr NotificationCommand::ExecuteOverride;
Dictionary::Ptr NotificationCommand::Execute(const Notification::Ptr& notification,
const User::Ptr& user, const CheckResult::Ptr& cr, const NotificationType& type,
const String& author, const String& comment, const Dictionary::Ptr& resolvedMacros,

View File

@ -22,6 +22,8 @@ public:
DECLARE_OBJECT(NotificationCommand);
DECLARE_OBJECTNAME(NotificationCommand);
static thread_local NotificationCommand::Ptr ExecuteOverride;
virtual Dictionary::Ptr Execute(const intrusive_ptr<Notification>& notification,
const User::Ptr& user, const CheckResult::Ptr& cr, const NotificationType& type,
const String& author, const String& comment,

View File

@ -28,7 +28,11 @@ void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRe
if (resolvedMacros && !useResolvedMacros)
return;
CheckCommand::Ptr command = checkable->GetCheckCommand();
CheckCommand::Ptr command;
if (CheckCommand::ExecuteOverride)
command = CheckCommand::ExecuteOverride;
else
command = checkable->GetCheckCommand();
String commandName = command->GetName();
ApiListener::Ptr listener = ApiListener::GetInstance();

View File

@ -21,7 +21,11 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
REQUIRE_NOT_NULL(cr);
ApiListener::Ptr listener = ApiListener::GetInstance();
CheckCommand::Ptr command = checkable->GetCheckCommand();
CheckCommand::Ptr command;
if (CheckCommand::ExecuteOverride)
command = CheckCommand::ExecuteOverride;
else
command = checkable->GetCheckCommand();
String commandName = command->GetName();
if (!listener) {

View File

@ -22,7 +22,11 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
REQUIRE_NOT_NULL(checkable);
REQUIRE_NOT_NULL(cr);
CheckCommand::Ptr command = checkable->GetCheckCommand();
CheckCommand::Ptr command;
if (CheckCommand::ExecuteOverride)
command = CheckCommand::ExecuteOverride;
else
command = checkable->GetCheckCommand();
Host::Ptr host;
Service::Ptr service;

View File

@ -26,7 +26,11 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
REQUIRE_NOT_NULL(checkable);
REQUIRE_NOT_NULL(cr);
CheckCommand::Ptr command = checkable->GetCheckCommand();
CheckCommand::Ptr command;
if (CheckCommand::ExecuteOverride)
command = CheckCommand::ExecuteOverride;
else
command = checkable->GetCheckCommand();
Host::Ptr host;
Service::Ptr service;

View File

@ -22,7 +22,11 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
REQUIRE_NOT_NULL(checkable);
REQUIRE_NOT_NULL(cr);
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
CheckCommand::Ptr commandObj;
if (CheckCommand::ExecuteOverride)
commandObj = CheckCommand::ExecuteOverride;
else
commandObj = checkable->GetCheckCommand();
Host::Ptr host;
Service::Ptr service;

View File

@ -21,7 +21,11 @@ void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable,
{
REQUIRE_NOT_NULL(checkable);
EventCommand::Ptr commandObj = checkable->GetEventCommand();
EventCommand::Ptr commandObj;
if (EventCommand::ExecuteOverride)
commandObj = EventCommand::ExecuteOverride;
else
commandObj = checkable->GetEventCommand();
Host::Ptr host;
Service::Ptr service;

View File

@ -25,7 +25,11 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
REQUIRE_NOT_NULL(notification);
REQUIRE_NOT_NULL(user);
NotificationCommand::Ptr commandObj = notification->GetCommand();
NotificationCommand::Ptr commandObj;
if (NotificationCommand::ExecuteOverride)
commandObj = NotificationCommand::ExecuteOverride;
else
commandObj = notification->GetCommand();
auto type = static_cast<NotificationType>(itype);

View File

@ -31,7 +31,11 @@ void RandomCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
+ ". Icinga 2 has been running for " + Utility::FormatDuration(uptime)
+ ". Version: " + Application::GetAppVersion();
CheckCommand::Ptr command = checkable->GetCheckCommand();
CheckCommand::Ptr command;
if (CheckCommand::ExecuteOverride)
command = CheckCommand::ExecuteOverride;
else
command = checkable->GetCheckCommand();
String commandName = command->GetName();
ServiceState state = static_cast<ServiceState>(Utility::Random() % 4);

View File

@ -18,7 +18,11 @@ void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
REQUIRE_NOT_NULL(checkable);
REQUIRE_NOT_NULL(cr);
CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
CheckCommand::Ptr commandObj;
if (CheckCommand::ExecuteOverride)
commandObj = CheckCommand::ExecuteOverride;
else
commandObj = checkable->GetCheckCommand();
Host::Ptr host;
Service::Ptr service;