From b07a161fb08cfdc72ac187b63f7d082b7dfffd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Gonz=C3=A1lez?= Date: Mon, 29 Aug 2022 14:05:30 +0200 Subject: [PATCH] Added control for clean not initialized modules --- pandora_console/godmode/setup/performance.php | 25 ++++++--- pandora_console/include/functions_config.php | 4 ++ pandora_console/pandoradb_data.sql | 1 + pandora_server/util/pandora_db.pl | 53 +++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/pandora_console/godmode/setup/performance.php b/pandora_console/godmode/setup/performance.php index 0b89688759..89b745cfca 100644 --- a/pandora_console/godmode/setup/performance.php +++ b/pandora_console/godmode/setup/performance.php @@ -340,8 +340,19 @@ $table->data[8][1] = html_print_input_text( true ); -$table->data[9][0] = __('Max. days before delete autodisabled agents'); + +$table->data[9][0] = __('Max. days before delete not initialized modules'); $table->data[9][1] = html_print_input_text( + 'days_delete_not_initialized', + $config['days_delete_not_initialized'], + '', + 5, + 5, + true +); + +$table->data[10][0] = __('Max. days before delete autodisabled agents'); +$table->data[10][1] = html_print_input_text( 'days_autodisable_deletion', $config['days_autodisable_deletion'], '', @@ -350,8 +361,8 @@ $table->data[9][1] = html_print_input_text( true ); -$table->data[10][0] = __('Retention period of past special days'); -$table->data[10][1] = html_print_input_text( +$table->data[11][0] = __('Retention period of past special days'); +$table->data[11][1] = html_print_input_text( 'num_past_special_days', $config['num_past_special_days'], '', @@ -360,8 +371,8 @@ $table->data[10][1] = html_print_input_text( true ); -$table->data[11][0] = __('Max. macro data fields'); -$table->data[11][1] = html_print_input_text( +$table->data[12][0] = __('Max. macro data fields'); +$table->data[12][1] = html_print_input_text( 'max_macro_fields', $config['max_macro_fields'], '', @@ -374,8 +385,8 @@ $table->data[11][1] = html_print_input_text( ); if (enterprise_installed()) { - $table->data[12][0] = __('Max. days before delete inventory data'); - $table->data[12][1] = html_print_input_text( + $table->data[13][0] = __('Max. days before delete inventory data'); + $table->data[13][1] = html_print_input_text( 'inventory_purge', $config['inventory_purge'], '', diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 23710c6622..fbe12e27f8 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -816,6 +816,10 @@ function config_update_config() $error_update[] = __('Max. days before delete unknown modules'); } + if (config_update_value('days_delete_not_initialized', (int) get_parameter('days_delete_not_initialized'), true) === false) { + $error_update[] = __('Max. days before delete not initialized modules'); + } + if (config_update_value('days_compact', (int) get_parameter('days_compact'), true) === false) { $error_update[] = __('Max. days before compact data'); } diff --git a/pandora_console/pandoradb_data.sql b/pandora_console/pandoradb_data.sql index dc1dc8dac3..8be0ff55c0 100644 --- a/pandora_console/pandoradb_data.sql +++ b/pandora_console/pandoradb_data.sql @@ -37,6 +37,7 @@ INSERT INTO `tconfig` (`token`, `value`) VALUES ('block_size','20'), ('days_purge','45'), ('days_delete_unknown','0'), +('days_delete_not_initialized','0'), ('days_compact','0'), ('days_autodisable_deletion','30'), ('graph_res','5'), diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index e789075ca5..eae25ff9c9 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -648,6 +648,7 @@ sub pandora_load_config_pdb ($) { $conf->{'_history_db_step'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_step'"); $conf->{'_history_db_delay'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_delay'"); $conf->{'_days_delete_unknown'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'days_delete_unknown'"); + $conf->{'_days_delete_not_initialized'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'days_delete_not_initialized'"); $conf->{'_inventory_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'inventory_purge'"); $conf->{'_delete_old_messages'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'delete_old_messages'"); $conf->{'_delete_old_network_matrix'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'delete_old_network_matrix'"); @@ -836,6 +837,58 @@ sub pandora_checkdb_consistency { usleep (100000); } } + + # Perform a clean of not initialized modules. + if (defined($conf{'_days_delete_not_initialized'}) && $conf{'_days_delete_not_initialized'} > 0) { + log_message ('CHECKDB', + "Deleting not initialized modules (More than " . $conf{'_days_delete_not_initialized'} . " days)."); + + my @modules = get_db_rows($dbh, + 'SELECT tagente_modulo.id_agente_modulo, tagente_modulo.id_agente + FROM tagente_modulo, tagente_estado + WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + AND (estado = 4 OR estado = 5) + AND utimestamp < UNIX_TIMESTAMP() - ?', + 86400 * $conf{'_days_delete_not_initialized'}); + + foreach my $module (@modules) { + my $id_agente = $module->{'id_agente'}; + my $id_agente_modulo = $module->{'id_agente_modulo'}; + + # Skip policy modules + my $is_policy_module = enterprise_hook('is_policy_module', + [$dbh, $id_agente_modulo]); + next if (defined($is_policy_module) && $is_policy_module); + + # Mark the agent for module and alert counters update + db_do ($dbh, + 'UPDATE tagente + SET update_module_count = 1, update_alert_count = 1 + WHERE id_agente = ?', $id_agente); + + # Delete the module + db_do ($dbh, + 'DELETE FROM tagente_modulo + WHERE disabled = 0 + AND id_agente_modulo = ?', $id_agente_modulo); + + # Do a nanosleep here for 0,001 sec + usleep (100000); + + # Delete any alerts associated to the module + db_do ($dbh, 'DELETE FROM talert_template_modules + WHERE id_agent_module = ? + AND NOT EXISTS (SELECT id_agente_modulo + FROM tagente_modulo + WHERE id_agente_modulo = ?)', + $id_agente_modulo, $id_agente_modulo); + + # Do a nanosleep here for 0,001 sec + usleep (100000); + } + } + + log_message ('CHECKDB', "Checking database consistency (Missing status).");