mirror of https://github.com/Icinga/icinga2.git
Implement notification execution
This commit is contained in:
parent
867da09fb4
commit
2e8b492f29
|
@ -676,6 +676,9 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||
MacroResolver::OverrideMacros = nullptr;
|
||||
});
|
||||
|
||||
/* Create execution parameters */
|
||||
Dictionary::Ptr execParams = new Dictionary();
|
||||
|
||||
if (command_type == "CheckCommand") {
|
||||
CheckCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
|
||||
if (!cmd)
|
||||
|
@ -707,6 +710,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||
User::Ptr user = GetSingleObjectByNameUsingPermissions(User::GetTypeName(), resolved_user, ActionsHandler::AuthenticatedApiUser);
|
||||
if (!user)
|
||||
return ApiActions::CreateResult(404, "Can't find a valid user for '" + resolved_user + "'.");
|
||||
execParams->Set("user", user->GetName());
|
||||
|
||||
/* Get notification */
|
||||
String notification_string = "";
|
||||
|
@ -722,6 +726,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||
Notification::Ptr notification = GetSingleObjectByNameUsingPermissions(Notification::GetTypeName(), resolved_notification, ActionsHandler::AuthenticatedApiUser);
|
||||
if (!notification)
|
||||
return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'.");
|
||||
execParams->Set("notification", notification->GetName());
|
||||
|
||||
cmd->Execute(notification, user, cr, NotificationType::NotificationCustom,
|
||||
ActionsHandler::AuthenticatedApiUser->GetName(), "", execMacros, false);
|
||||
|
@ -761,8 +766,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||
MessageOrigin::Ptr origin = new MessageOrigin();
|
||||
listener->RelayMessage(origin, checkable, updateMessage, true);
|
||||
|
||||
/* Create execution parameters */
|
||||
Dictionary::Ptr execParams = new Dictionary();
|
||||
/* Populate execution parameters */
|
||||
if (command_type == "CheckCommand")
|
||||
execParams->Set("command_type", "check_command");
|
||||
else if (command_type == "EventCommand")
|
||||
|
|
|
@ -241,11 +241,16 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
|||
} else if (command_type == "event_command") {
|
||||
if (!EventCommand::GetByName(command)) {
|
||||
Log(LogWarning, "ClusterEvents")
|
||||
<< "Event command '" << command << "' does not exist.";
|
||||
<< "Event command '" << command << "' does not exist.";
|
||||
return;
|
||||
}
|
||||
} else
|
||||
return;
|
||||
} else if (command_type == "notification_command") {
|
||||
if (!NotificationCommand::GetByName(command)) {
|
||||
Log(LogWarning, "ClusterEvents")
|
||||
<< "Notification command '" << command << "' does not exist.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
attrs->Set(command_type, params->Get("command"));
|
||||
attrs->Set("command_endpoint", sourceEndpoint->GetName());
|
||||
|
@ -279,6 +284,51 @@ void ClusterEvents::ExecuteCheckFromQueue(const MessageOrigin::Ptr& origin, cons
|
|||
}
|
||||
} else if (command_type == "event_command") {
|
||||
host->ExecuteEventHandler(macros, true);
|
||||
} else if (command_type == "notification_command") {
|
||||
/* Get user */
|
||||
User::Ptr user = new User();
|
||||
Dictionary::Ptr attrs = new Dictionary();
|
||||
attrs->Set("__name", params->Get("user"));
|
||||
attrs->Set("type", User::GetTypeName());
|
||||
|
||||
Deserialize(user, attrs, false, FAConfig);
|
||||
|
||||
/* Get notification */
|
||||
Notification::Ptr notification = new Notification();
|
||||
attrs->Clear();
|
||||
attrs->Set("__name", params->Get("notification"));
|
||||
attrs->Set("type", Notification::GetTypeName());
|
||||
attrs->Set("command", command);
|
||||
|
||||
Deserialize(notification, attrs, false, FAConfig);
|
||||
|
||||
try {
|
||||
CheckResult::Ptr cr = new CheckResult();
|
||||
String author = macros->Get("notification_author");
|
||||
NotificationCommand::Ptr notificationCommand = NotificationCommand::GetByName(command);
|
||||
|
||||
notificationCommand->Execute(notification, user, cr, NotificationType::NotificationCustom,
|
||||
author, "");
|
||||
} catch (const std::exception& ex) {
|
||||
String output = "Exception occurred during notification '" + notification->GetName()
|
||||
+ "' for checkable '" + notification->GetCheckable()->GetName()
|
||||
+ "' and user '" + user->GetName() + "' using command '" + command + "': "
|
||||
+ DiagnosticInformation(ex, false);
|
||||
double now = Utility::GetTime();
|
||||
|
||||
CheckResult::Ptr cr = new CheckResult();
|
||||
cr->SetState(ServiceUnknown);
|
||||
cr->SetOutput(output);
|
||||
cr->SetScheduleStart(now);
|
||||
cr->SetScheduleEnd(now);
|
||||
cr->SetExecutionStart(now);
|
||||
cr->SetExecutionEnd(now);
|
||||
|
||||
Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
|
||||
listener->SyncSendMessage(sourceEndpoint, message);
|
||||
|
||||
Log(LogCritical, "checker", output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue