diff --git a/pandora_console/extras/mr/58.sql b/pandora_console/extras/mr/58.sql index 5fe303aa70..4d3193112c 100644 --- a/pandora_console/extras/mr/58.sql +++ b/pandora_console/extras/mr/58.sql @@ -20,4 +20,7 @@ ALTER TABLE `tagente_estado` ADD COLUMN `warning_count` int(10) UNSIGNED DEFAULT ALTER TABLE `tcredential_store` MODIFY COLUMN `product` ENUM('CUSTOM', 'AWS', 'AZURE', 'GOOGLE', 'SAP', 'WMI', 'SNMP') DEFAULT 'CUSTOM'; +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 ba0dcde7da..89fa418389 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -555,7 +555,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 bba8217b57..84f0065243 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -1150,10 +1150,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; }