From 68525f0a1227f8e489f51acdf8d93194d936ad78 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 7 Aug 2023 17:03:25 +0200 Subject: [PATCH 1/2] preserve previous disabled or quiet condition when downtime is over --- pandora_console/extras/mr/65.sql | 4 + .../include/functions_planned_downtimes.php | 76 ++++++++++++++++--- pandora_console/pandoradb.sql | 4 + pandora_server/lib/PandoraFMS/Core.pm | 27 ++++++- 4 files changed, 100 insertions(+), 11 deletions(-) diff --git a/pandora_console/extras/mr/65.sql b/pandora_console/extras/mr/65.sql index 31e221f3b0..563d00e82c 100644 --- a/pandora_console/extras/mr/65.sql +++ b/pandora_console/extras/mr/65.sql @@ -70,6 +70,10 @@ ALTER TABLE `tlayout_template` ADD COLUMN `grid_color` VARCHAR(45) NOT NULL DEFAULT '#cccccc' AFTER `maintenance_mode`, ADD COLUMN `grid_size` VARCHAR(45) NOT NULL DEFAULT '10' AFTER `grid_color`; +ALTER TABLE `tagente_modulo` ADD COLUMN `quiet_by_downtime` TINYINT NOT NULL DEFAULT 0; +ALTER TABLE `tagente_modulo` ADD COLUMN `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0; +ALTER TABLE `talert_template_modules` ADD COLUMN `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0; +ALTER TABLE `tagente` ADD COLUMN `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0; DELETE FROM tconfig WHERE token = 'refr'; diff --git a/pandora_console/include/functions_planned_downtimes.php b/pandora_console/include/functions_planned_downtimes.php index d9925a24b2..c2ca5bf427 100644 --- a/pandora_console/include/functions_planned_downtimes.php +++ b/pandora_console/include/functions_planned_downtimes.php @@ -645,9 +645,13 @@ function planned_downtimes_stop($downtime) foreach ($modules as $module) { $result = db_process_sql_update( 'tagente_modulo', - ['quiet' => 0], [ - 'id_agente_modulo' => $module['id_agent_module'], + 'quiet' => 0, + 'quiet_by_downtime' => 0, + ], + [ + 'quiet_by_downtime' => 1, + 'id_agente_modulo' => $module['id_agent_module'], ] ); @@ -673,20 +677,31 @@ function planned_downtimes_stop($downtime) $result = db_process_sql_update( 'tagente', [ - 'disabled' => 0, 'update_module_count' => 1, ], ['id_agente' => $agent['id_agent']] ); - if ($result) { + $result_disabled = db_process_sql_update( + 'tagente', + [ + 'disabled' => 0, + 'disabled_by_downtime' => 0, + ], + [ + 'disabled_by_downtime' => 1, + 'id_agente' => $agent['id_agent'], + ] + ); + + if ($result !== false && $result_disabled !== false) { $count++; } } break; case 'disable_agent_modules': - $update_sql = sprintf( + /*$update_sql = sprintf( 'UPDATE tagente_modulo tam, tagente ta, tplanned_downtime_modules tpdm SET tam.disabled = 0, ta.update_module_count = 1 WHERE tpdm.id_agent_module = tam.id_agente_modulo AND @@ -697,7 +712,46 @@ function planned_downtimes_stop($downtime) db_process_sql($update_sql); - $count = ''; + $count = '';*/ + $agents = db_get_all_rows_filter( + 'tplanned_downtime_agents', + ['id_downtime' => $id_downtime] + ); + if (empty($agents)) { + $agents = []; + } + + $count = 0; + foreach ($agents as $agent) { + $modules = db_get_all_rows_filter( + 'tplanned_downtime_modules', + [ + 'id_agent' => $agent['id_agent'], + 'id_downtime' => $id_downtime, + ] + ); + if (empty($modules)) { + $modules = []; + } + + foreach ($modules as $module) { + $result = db_process_sql_update( + 'tagente_modulo', + [ + 'disabled' => 0, + 'disabled_by_downtime' => 0, + ], + [ + 'disabled_by_downtime' => 1, + 'id_agente_modulo' => $module['id_agent_module'], + ] + ); + + if ($result !== false) { + $count++; + } + } + } break; case 'disable_agents_alerts': @@ -722,13 +776,17 @@ function planned_downtimes_stop($downtime) foreach ($modules as $module) { $result = db_process_sql_update( 'talert_template_modules', - ['disabled' => 0], [ - 'id_agent_module' => $module['id_agente_modulo'], + 'disabled' => 0, + 'disabled_by_downtime' => 0, + ], + [ + 'disabled_by_downtime' => 1, + 'id_agent_module' => $module['id_agente_modulo'], ] ); - if ($result) { + if ($result !== false) { $count++; } } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 440199f901..01bc7179c6 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -91,6 +91,7 @@ CREATE TABLE IF NOT EXISTS `tagente` ( `cps` INT NOT NULL DEFAULT 0, `satellite_server` INT NOT NULL DEFAULT 0, `fixed_ip` TINYINT NOT NULL DEFAULT 0, + `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`id_agente`), KEY `nombre` (`nombre`(255)), KEY `direccion` (`direccion`), @@ -274,6 +275,8 @@ CREATE TABLE IF NOT EXISTS `tagente_modulo` ( `percentage_critical` TINYINT UNSIGNED DEFAULT 0, `percentage_warning` TINYINT UNSIGNED DEFAULT 0, `warning_time` INT UNSIGNED DEFAULT 0, + `quiet_by_downtime` TINYINT NOT NULL DEFAULT 0, + `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`id_agente_modulo`), KEY `main_idx` (`id_agente_modulo`,`id_agente`), KEY `tam_agente` (`id_agente`), @@ -550,6 +553,7 @@ CREATE TABLE IF NOT EXISTS `talert_template_modules` ( `standby` TINYINT DEFAULT 0, `priority` TINYINT DEFAULT 0, `force_execution` TINYINT DEFAULT 0, + `disabled_by_downtime` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`id`), KEY `idx_template_module` (`id_agent_module`), FOREIGN KEY (`id_agent_module`) REFERENCES tagente_modulo(`id_agente_modulo`) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index b90044db58..7eaa25ffeb 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -2544,6 +2544,12 @@ sub pandora_planned_downtime_set_disabled_elements($$$) { if ($only_alerts == 0) { if ($downtime->{'type_downtime'} eq 'disable_agent_modules') { + db_do($dbh,'UPDATE tagente_modulo tam, tagente ta, tplanned_downtime_modules tpdm + SET tam.disabled_by_downtime = 1 + WHERE tam.disabled = 0 AND tpdm.id_agent_module = tam.id_agente_modulo AND + ta.id_agente = tam.id_agente AND + tpdm.id_downtime = ?', $downtime->{'id'}); + db_do($dbh,'UPDATE tagente_modulo tam, tagente ta, tplanned_downtime_modules tpdm SET tam.disabled = 1, ta.update_module_count = 1 WHERE tpdm.id_agent_module = tam.id_agente_modulo AND @@ -2553,7 +2559,12 @@ sub pandora_planned_downtime_set_disabled_elements($$$) { db_do($dbh,'UPDATE tplanned_downtime_agents tp, tagente ta SET tp.manually_disabled = ta.disabled WHERE tp.id_agent = ta.id_agente AND tp.id_downtime = ?',$downtime->{'id'}); - + + db_do($dbh,'UPDATE tagente ta, tplanned_downtime_agents tpa + SET ta.disabled_by_downtime = 1 + WHERE ta.disabled = 0 AND tpa.id_agent = ta.id_agente AND + tpa.id_downtime = ?',$downtime->{'id'}); + db_do($dbh,'UPDATE tagente ta, tplanned_downtime_agents tpa SET ta.disabled = 1, ta.update_module_count = 1 WHERE tpa.id_agent = ta.id_agente AND @@ -2565,6 +2576,11 @@ sub pandora_planned_downtime_set_disabled_elements($$$) { WHERE id_downtime = ' . $downtime->{'id'}); foreach my $downtime_agent (@downtime_agents) { + db_do ($dbh, 'UPDATE talert_template_modules tat, tagente_modulo tam + SET tat.disabled_by_downtime = 1 + WHERE tat.disabled = 0 AND tat.id_agent_module = tam.id_agente_modulo + AND tam.id_agente = ?', $downtime_agent->{'id_agent'}); + db_do ($dbh, 'UPDATE talert_template_modules tat, tagente_modulo tam SET tat.disabled = 1 WHERE tat.id_agent_module = tam.id_agente_modulo @@ -2645,6 +2661,13 @@ sub pandora_planned_downtime_set_quiet_elements($$$) { AND id_downtime = ' . $downtime_id); foreach my $downtime_module (@downtime_modules) { + # If traversed module was already quiet, do not set quiet_by_downtime flag. + # quiet_by_downtime is used to avoid setting the module back to quiet=0 when downtime is over for those modules that were quiet before the downtime. + db_do ($dbh, 'UPDATE tagente_modulo + SET quiet_by_downtime = 1 + WHERE quiet = 0 && id_agente_modulo = ?', + $downtime_module->{'id_agent_module'}); + db_do ($dbh, 'UPDATE tagente_modulo SET quiet = 1 WHERE id_agente_modulo = ?', @@ -2667,7 +2690,7 @@ sub pandora_planned_downtime_unset_quiet_elements($$$) { my @downtime_agents = get_db_rows($dbh, 'SELECT * FROM tplanned_downtime_agents WHERE id_downtime = ' . $downtime_id); - + foreach my $downtime_agent (@downtime_agents) { if ($downtime_agent->{'all_modules'}) { db_do ($dbh, 'UPDATE tagente From a4e186ae84049450936009d3910c7af16357a385 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 7 Aug 2023 17:04:48 +0200 Subject: [PATCH 2/2] preserve previous disabled or quiet condition when downtime is over --- .../include/functions_planned_downtimes.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/pandora_console/include/functions_planned_downtimes.php b/pandora_console/include/functions_planned_downtimes.php index c2ca5bf427..3d9417aa2b 100644 --- a/pandora_console/include/functions_planned_downtimes.php +++ b/pandora_console/include/functions_planned_downtimes.php @@ -701,18 +701,6 @@ function planned_downtimes_stop($downtime) break; case 'disable_agent_modules': - /*$update_sql = sprintf( - 'UPDATE tagente_modulo tam, tagente ta, tplanned_downtime_modules tpdm - SET tam.disabled = 0, ta.update_module_count = 1 - WHERE tpdm.id_agent_module = tam.id_agente_modulo AND - ta.id_agente = tam.id_agente AND - tpdm.id_downtime = %d', - $id_downtime - ); - - db_process_sql($update_sql); - - $count = '';*/ $agents = db_get_all_rows_filter( 'tplanned_downtime_agents', ['id_downtime' => $id_downtime]