Merge branch 'ent-3371-sistema-de-notificaciones-pandora' of brutus.artica.lan:artica/pandorafms into ent-3371-sistema-de-notificaciones-pandora

Former-commit-id: 9c321b0841261f1c44f7c809190c22acf20f5fa5
This commit is contained in:
fbsanchez 2019-02-12 12:27:48 +01:00
commit daa72a031a
3 changed files with 94 additions and 5 deletions

View File

@ -1974,4 +1974,9 @@ CREATE TABLE `tnotification_source_group_user` (
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (`id_group`) REFERENCES `tnotification_source_group`(`id_group`)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------------------------------------------------
-- Add alert command 'Generate notification'
-- ----------------------------------------------------------------------
INSERT INTO `talert_commands` (`name`, `command`, `description`, `internal`, `fields_descriptions`, `fields_values`) VALUES ('Generate Notification','Internal type','This command allows you to send an internal notification to any user or group.',1,'[\"Destination user\",\"Destination group\",\"Title\",\"Message\",\"Link\",\"Criticity\",\"\",\"\",\"\",\"\",\"\"]','[\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"]');

File diff suppressed because one or more lines are too long

View File

@ -249,6 +249,7 @@ our @EXPORT = qw(
pandora_delete_graph_source
pandora_delete_custom_graph
pandora_edit_custom_graph
notification_set_targets
);
# Some global variables
@ -1459,6 +1460,35 @@ sub pandora_execute_action ($$$$$$$$$;$) {
pandora_create_integria_ticket($pa_config, $api_path, $api_pass, $integria_user, $integria_user_pass, $ticket_name, $ticket_group_id, $ticket_priority, $ticket_email, $ticket_owner, $ticket_description);
# Generate notification
} elsif ($clean_name eq "Generate Notification") {
# Translate macros
$field3 = subst_alert_macros($field3, \%macros, $pa_config, $dbh, $agent, $module);
$field4 = subst_alert_macros($field4, \%macros, $pa_config, $dbh, $agent, $module);
# If no targets ignore notification
if (defined($field1) && defined($field2) && ($field1 ne "" || $field2 ne "")) {
my @user_list = map {clean_blank($_)} split /,/, $field1;
my @group_list = map {clean_blank($_)} split /,/, $field2;
my $notification = {};
$notification->{'subject'} = safe_input($field3);
$notification->{'mensaje'} = safe_input($field4);
$notification->{'id_source'} = get_db_value($dbh, 'SELECT id FROM tnotification_source WHERE description = ?', safe_input('System status'));
# Create message
my $notification_id = db_process_insert($dbh,'id_mensaje','tmensajes',$notification);
if (!$notification_id) {
logger($pa_config, "Failed action '" . $action->{'name'} . "' for alert '". $alert->{'name'} . "' agent '" . (defined($agent) ? $agent->{'alias'} : 'N/A') . "'.", 3);
} else {
notification_set_targets($pa_config, $dbh, $notification_id, \@user_list, \@group_list);
}
} else {
logger($pa_config, "Failed action '" . $action->{'name'} . "' for alert '". $alert->{'name'} . "' agent '" . (defined($agent) ? $agent->{'alias'} : 'N/A') . "' Empty targets. Ignored.", 3);
}
# Unknown
} else {
logger($pa_config, "Unknown action '" . $action->{'name'} . "' for alert '". $alert->{'name'} . "' agent '" . (defined ($agent) ? $agent->{'alias'} : 'N/A') . "'.", 3);
@ -5746,6 +5776,62 @@ sub pandora_safe_mode_modules_update {
}
}
##########################################################################
=head2 C<< message_set_targets (I<$dbh>, I<$pa_config>, I<$notification_id>, I<$users>, I<$groups>) >>
Set targets for given messaje (users and groups in hash ref)
=cut
##########################################################################
sub notification_set_targets {
my ($pa_config, $dbh, $notification_id, $users, $groups) = @_;
my $ret = undef;
if (!defined($pa_config)) {
return undef;
}
if (!defined($notification_id)) {
return undef;
}
if (ref($users) eq "ARRAY") {
my $values = {};
foreach my $user (@{$users}) {
if (defined($user) && $user eq "") {
next;
}
$values->{'id_mensaje'} = $notification_id;
$values->{'id_user'} = $user;
}
$ret = db_process_insert($dbh, '', 'tnotification_user', $values);
if (!$ret) {
return undef;
}
}
if (ref($groups) eq "ARRAY") {
my $values = {};
foreach my $group (@{$groups}) {
if ($group != 0 && empty($group)) {
next;
}
$values->{'id_mensaje'} = $notification_id;
$values->{'id_group'} = $group;
}
$ret = db_process_insert($dbh, '', 'tnotification_group', $values);
if (!$ret) {
return undef;
}
}
return 1;
}
# End of function declaration
# End of defined Code