From ac890ee1653cd3f9254623a0d13f99f90e89939d Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 8 Nov 2021 14:00:26 +0100 Subject: [PATCH 1/8] some tools for policy-auto-application --- pandora_server/lib/PandoraFMS/Core.pm | 3 +++ pandora_server/lib/PandoraFMS/Tools.pm | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index c57df509ff..110ef74710 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -5440,6 +5440,9 @@ sub pandora_process_policy_queue ($) { next; } + # Refresh policy agents. + enterprise_hook('pandora_apply_policy_groups', [$pa_config, $dbh]); + my $operation = enterprise_hook('get_first_policy_queue', [$dbh]); next unless (defined ($operation) && $operation ne ''); diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index e4d605ff12..19f5e1a377 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -126,6 +126,7 @@ our @EXPORT = qw( is_offline is_empty is_in_array + array_diff add_hashes to_number clean_blank @@ -714,6 +715,19 @@ sub is_in_array { return 0; } +################################################################################ +# Check if a value is in an array +################################################################################ +sub array_diff($$) { + my ($a, $b) = @_; + + my %diff; + @diff{ @{$a} } = @{$a}; + delete @diff{ @{$b} }; + + return keys %diff; +} + ################################################################################ # Mix hashses ################################################################################ From e9249d550aedf81c0a8febc2af9f10c0a93eea65 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 7 Feb 2022 18:53:31 +0100 Subject: [PATCH 2/8] removed policy stuff from configurar_agente --- .../godmode/agentes/configurar_agente.php | 77 ------------------- 1 file changed, 77 deletions(-) diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 7965dbc566..4d17837cc9 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -30,7 +30,6 @@ global $config; enterprise_include('godmode/agentes/configurar_agente.php'); -enterprise_include('include/functions_policies.php'); enterprise_include_once('include/functions_modules.php'); require_once $config['homedir'].'/include/functions_agents.php'; require_once $config['homedir'].'/include/functions_cron.php'; @@ -304,23 +303,6 @@ if ($create_agent) { $agent_created_ok = true; - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$grupo - ); - - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $old_group) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $old_group['id_policy'], - 'id_agent' => $id_agente, - ] - ); - } - } - $info = '{"Name":"'.$nombre_agente.'", "IP":"'.$direccion_agente.'", "Group":"'.$grupo.'", @@ -1114,12 +1096,6 @@ if ($update_agent) { // Force an update of the agent cache. } - $group_old = db_get_sql('SELECT id_grupo FROM tagente WHERE id_agente ='.$id_agente); - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$group_old - ); - $result = db_process_sql_update('tagente', $values, ['id_agente' => $id_agente]); if ($result === false && $update_custom_result == false) { @@ -1158,59 +1134,6 @@ if ($update_agent) { } } - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $value) { - $tpolicy_agents_old = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agente - ); - - if ($tpolicy_agents_old) { - $result2 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 1], - [ - 'id_agent' => $id_agente, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - - $tpolicy_group = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$grupo - ); - - if ($tpolicy_group) { - foreach ($tpolicy_group as $key => $value) { - $tpolicy_agents = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agente - ); - - if (!$tpolicy_agents) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $value['id_policy'], - 'id_agent' => $id_agente, - ] - ); - } else { - $result3 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 0], - [ - 'id_agent' => $id_agente, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - $info = '{ "id_agente":"'.$id_agente.'", "alias":"'.$alias.'", From d6b6357e98815b62368b01cc17121ce1cc7a2ba6 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 7 Feb 2022 18:54:24 +0100 Subject: [PATCH 3/8] removed policy stuff from api update_agent --- pandora_console/include/functions_api.php | 53 ----------------------- 1 file changed, 53 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index b077a14654..a781fa80e2 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -1577,59 +1577,6 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) ] ); } - - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $value) { - $tpolicy_agents_old = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent - ); - - if ($tpolicy_agents_old) { - $result2 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 1], - [ - 'id_agent' => $id_agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - - $tpolicy_group = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$idGroup - ); - - if ($tpolicy_group) { - foreach ($tpolicy_group as $key => $value) { - $tpolicy_agents = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent - ); - - if (!$tpolicy_agents) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $value['id_policy'], - 'id_agent' => $id_agent, - ] - ); - } else { - $result3 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 0], - [ - 'id_agent' => $id_agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } } returnData( From 3f526799695cd75e790030d63ff374e800fd2121 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 7 Feb 2022 18:56:58 +0100 Subject: [PATCH 4/8] removed policy stuff from api update_agent_field --- pandora_console/include/functions_api.php | 58 ----------------------- 1 file changed, 58 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index a781fa80e2..3780b4141b 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -1776,11 +1776,6 @@ function api_set_update_agent_field($id_agent, $use_agent_alias, $params) ] ); - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$values_old['id_grupo'] - ); - $return = db_process_sql_update( 'tagente', $fields, @@ -1808,59 +1803,6 @@ function api_set_update_agent_field($id_agent, $use_agent_alias, $params) ] ); } - - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $value) { - $tpolicy_agents_old = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$agent - ); - - if ($tpolicy_agents_old) { - $result2 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 1], - [ - 'id_agent' => $agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - - $tpolicy_group = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$field['id_grupo'] - ); - - if ($tpolicy_group) { - foreach ($tpolicy_group as $key => $value) { - $tpolicy_agents = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$agent - ); - - if (!$tpolicy_agents) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $value['id_policy'], - 'id_agent' => $agent, - ] - ); - } else { - $result3 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 0], - [ - 'id_agent' => $agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } } } From f1a3d4030901739d0a5c21102519faf0b24fb9f1 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 7 Feb 2022 18:58:00 +0100 Subject: [PATCH 5/8] removed policy stuff from massive edition (agents) --- .../godmode/massive/massive_edit_agents.php | 73 ------------------- 1 file changed, 73 deletions(-) diff --git a/pandora_console/godmode/massive/massive_edit_agents.php b/pandora_console/godmode/massive/massive_edit_agents.php index 6ba5a3c848..f3944a6354 100755 --- a/pandora_console/godmode/massive/massive_edit_agents.php +++ b/pandora_console/godmode/massive/massive_edit_agents.php @@ -212,14 +212,9 @@ if ($update_agents) { $old_interval_value = db_get_value_filter('intervalo', 'tagente', ['id_agente' => $id_agent]); if (!empty($values)) { - $group_old = false; $disabled_old = false; if ($values['id_grupo'] || isset($values['disabled'])) { $values_old = db_get_row_filter('tagente', ['id_agente' => $id_agent], ['id_grupo', 'disabled']); - if ($values_old['id_grupo']) { - $group_old = $values_old['id_grupo']; - } - if (isset($values['disabled'])) { $disabled_old = $values_old['disabled']; } @@ -268,74 +263,6 @@ if ($update_agents) { alerts_validate_alert_agent($id_agent); } } - - if ($group_old || $result) { - if ($group_old && $group_old != null) { - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$group_old - ); - } else { - $tpolicy_group_old = db_get_all_rows_sql('SELECT id_policy FROM tpolicy_groups'); - } - - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $value) { - $tpolicy_agents_old = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent - ); - - if ($tpolicy_agents_old) { - $result2 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 1], - [ - 'id_agent' => $id_agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - - if ($values['id_grupo'] && $values['id_grupo'] != null) { - $tpolicy_group_new = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$values['id_grupo'] - ); - } else { - $tpolicy_group_new = db_get_all_rows_sql('SELECT id_policy FROM tpolicy_groups'); - } - - if ($tpolicy_group_new) { - foreach ($tpolicy_group_new as $key => $value) { - $tpolicy_agents_new = db_get_sql( - 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent - ); - - if (!$tpolicy_agents_new) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $value['id_policy'], - 'id_agent' => $id_agent, - ] - ); - } else { - $result3 = db_process_sql_update( - 'tpolicy_agents', - ['pending_delete' => 0], - [ - 'id_agent' => $id_agent, - 'id_policy' => $value['id_policy'], - ] - ); - } - } - } - } } $info = []; From 83d8b5348f458ed42407a417cdd4150a45b39a19 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 8 Feb 2022 17:54:14 +0100 Subject: [PATCH 6/8] pol group api create agent auto-assign fix --- pandora_console/include/functions_api.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 3780b4141b..2b7e045844 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -1535,10 +1535,6 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) 'disabled', ] ); - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$values_old['id_grupo'] - ); $return = db_process_sql_update( 'tagente', @@ -1955,23 +1951,6 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) agents_add_address($id_agente, $direccion_agente); } - $tpolicy_group_old = db_get_all_rows_sql( - 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$grupo - ); - - if ($tpolicy_group_old) { - foreach ($tpolicy_group_old as $key => $old_group) { - db_process_sql_insert( - 'tpolicy_agents', - [ - 'id_policy' => $old_group['id_policy'], - 'id_agent' => $id_agente, - ] - ); - } - } - $info = '{"Name":"'.$nombre_agente.'", "IP":"'.$direccion_agente.'", "Group":"'.$grupo.'", From 4a61efd7b6c4fba794d246d4d4a11a934e3b854f Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 8 Feb 2022 22:52:54 +0100 Subject: [PATCH 7/8] added tpolicy_group_agents --- pandora_console/extras/mr/52.sql | 11 +++++++++++ .../extras/pandoradb_migrate_6.0_to_7.0.mysql.sql | 14 ++++++++++++++ pandora_console/pandoradb.sql | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/pandora_console/extras/mr/52.sql b/pandora_console/extras/mr/52.sql index 2d0cd15fa4..a77be76ddc 100644 --- a/pandora_console/extras/mr/52.sql +++ b/pandora_console/extras/mr/52.sql @@ -14,4 +14,15 @@ SOURCE procedures/alertEvents.sql; CALL `migrateEventRanges`(); DROP PROCEDURE `migrateEventRanges`; +CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` ( + `id` SERIAL, + `id_policy` INT UNSIGNED, + `id_agent` INT UNSIGNED, + `direct` TINYINT UNSIGNED DEFAULT 0, + FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`) + ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) + ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + COMMIT; \ No newline at end of file diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index 3848aad972..59e0a15547 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -213,6 +213,20 @@ CREATE TABLE IF NOT EXISTS `tpolicy_groups` ( UNIQUE (`id_policy`, `id_group`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-- ----------------------------------------------------- +-- Table `tpolicy_group_agents` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` ( + `id` SERIAL, + `id_policy` INT UNSIGNED, + `id_agent` INT UNSIGNED, + `direct` TINYINT UNSIGNED DEFAULT 0, + FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`) + ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) + ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- --------------------------------------------------------------------- -- Table `tdashboard` -- --------------------------------------------------------------------- diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 40b6770737..9994c0f11d 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -2592,6 +2592,21 @@ CREATE TABLE IF NOT EXISTS `tpolicy_groups` ( UNIQUE (`id_policy`, `id_group`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- ----------------------------------------------------- +-- Table `tpolicy_group_agents` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` ( + `id` SERIAL, + `id_policy` INT UNSIGNED, + `id_agent` INT UNSIGNED, + `direct` TINYINT UNSIGNED DEFAULT 0, + FOREIGN KEY (`id_policy`) REFERENCES `tpolicies`(`id`) + ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) + ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- --------------------------------------------------------------------- -- Table `tdashboard` -- --------------------------------------------------------------------- From bed223e27dadf7e8d68341b19109ddee668dc5c9 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 14 Feb 2022 11:04:01 +0100 Subject: [PATCH 8/8] latest changes --- pandora_console/extras/mr/52.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandora_console/extras/mr/52.sql b/pandora_console/extras/mr/52.sql index a77be76ddc..2fc946515e 100644 --- a/pandora_console/extras/mr/52.sql +++ b/pandora_console/extras/mr/52.sql @@ -14,6 +14,8 @@ SOURCE procedures/alertEvents.sql; CALL `migrateEventRanges`(); DROP PROCEDURE `migrateEventRanges`; +DELETE FROM `tpolicy_groups`; + CREATE TABLE IF NOT EXISTS `tpolicy_group_agents` ( `id` SERIAL, `id_policy` INT UNSIGNED,