From 688597a00602d5081701bb1fdb49663417af11e5 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Date: Thu, 20 Oct 2022 16:42:26 +0200 Subject: [PATCH] multiple external alert policies pandora_enterprise#8733 --- pandora_console/extras/mr/58.sql | 3 ++ pandora_console/godmode/alerts/alert_list.php | 21 +++++++++----- pandora_console/include/functions_alerts.php | 28 +++++++++++++++---- pandora_console/pandoradb.sql | 2 +- pandora_server/lib/PandoraFMS/DB.pm | 6 ++-- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/pandora_console/extras/mr/58.sql b/pandora_console/extras/mr/58.sql index 32b5255170..83d16c09fc 100644 --- a/pandora_console/extras/mr/58.sql +++ b/pandora_console/extras/mr/58.sql @@ -7,4 +7,7 @@ ALTER TABLE `tmodule_inventory` ADD COLUMN `script_path` VARCHAR(1000) DEFAULT ' ALTER TABLE `tevent_filter` ADD COLUMN `search_recursive_groups` INT NOT NULL DEFAULT 0; +ALTER TABLE `talert_template_modules` DROP INDEX `id_agent_module`; +ALTER TABLE `talert_template_modules` ADD UNIQUE (`id_agent_module`, `id_alert_template`, `id_policy_alerts`); + COMMIT; diff --git a/pandora_console/godmode/alerts/alert_list.php b/pandora_console/godmode/alerts/alert_list.php index 15c55050da..3b5656323a 100644 --- a/pandora_console/godmode/alerts/alert_list.php +++ b/pandora_console/godmode/alerts/alert_list.php @@ -102,13 +102,20 @@ if ($create_alert) { $id_alert_template = (int) get_parameter('template'); $id_agent_module = (int) get_parameter('id_agent_module'); - if (db_get_value_sql( - 'SELECT COUNT(id) - FROM talert_template_modules - WHERE id_agent_module = '.$id_agent_module.' - AND id_alert_template = '.$id_alert_template - ) > 0 - ) { + $exist = db_get_value_sql( + sprintf( + 'SELECT COUNT(id) + FROM talert_template_modules + WHERE id_agent_module = %d + AND id_alert_template = %d + AND id_policy_alerts = 0 + ', + $id_agent_module, + $id_alert_template + ) + ); + + if ($exist > 0) { $messageAction = ui_print_result_message( false, '', diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index a3b0bcb2fb..0a8628e54d 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -1147,13 +1147,31 @@ function alerts_create_alert_agent_module($id_agent_module, $id_alert_template, $values['id_alert_template'] = (int) $id_alert_template; $values['last_reference'] = time(); - $sql = sprintf( - 'INSERT IGNORE INTO talert_template_modules(%s) VALUES(%s)', - implode(', ', array_keys($values)), - implode(', ', array_values($values)) + $exist = db_get_value_sql( + sprintf( + 'SELECT COUNT(id) + FROM talert_template_modules + WHERE id_agent_module = %d + AND id_alert_template = %d + AND id_policy_alerts = 0 + ', + $id_agent_module, + $id_alert_template + ) ); - return @db_process_sql($sql, 'insert_id'); + $result = false; + if ((int) $exist === 0) { + $sql = sprintf( + 'INSERT INTO talert_template_modules(%s) VALUES(%s)', + implode(', ', array_keys($values)), + implode(', ', array_values($values)) + ); + + $result = db_process_sql($sql, 'insert_id'); + } + + return $result; } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index efabbd05ae..9ac9392d8f 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -553,7 +553,7 @@ CREATE TABLE IF NOT EXISTS `talert_template_modules` ( ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (`id_alert_template`) REFERENCES talert_templates(`id`) ON DELETE CASCADE ON UPDATE CASCADE, - UNIQUE (`id_agent_module`, `id_alert_template`), + UNIQUE (`id_agent_module`, `id_alert_template`, `id_policy_alerts`), INDEX force_execution (`force_execution`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index 9aae480433..688ca367cb 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -1146,10 +1146,10 @@ sub db_update ($$;@) { ########################################################################## ## Return alert template-module ID given the module and template ids. ########################################################################## -sub get_alert_template_module_id ($$$) { - my ($dbh, $id_module, $id_template) = @_; +sub get_alert_template_module_id ($$$$) { + my ($dbh, $id_module, $id_template, $id_policy_alerts) = @_; - my $rc = get_db_value ($dbh, "SELECT id FROM talert_template_modules WHERE id_agent_module = ? AND id_alert_template = ?", $id_module, $id_template); + my $rc = get_db_value ($dbh, "SELECT id FROM talert_template_modules WHERE id_agent_module = ? AND id_alert_template = ? AND id_policy_alerts = ?", $id_module, $id_template, $id_policy_alerts); return defined ($rc) ? $rc : -1; }