diff --git a/pandora_console/godmode/agentes/agent_manager.php b/pandora_console/godmode/agentes/agent_manager.php index d486be0e7b..8e314f0293 100644 --- a/pandora_console/godmode/agentes/agent_manager.php +++ b/pandora_console/godmode/agentes/agent_manager.php @@ -500,7 +500,7 @@ if (enterprise_installed()) { false, // Delete_groups. // Do not show the primary group in this selection. - array_merge($secondary_groups_selected['plain'], [$agent['id_grupo']]) + array_merge(($secondary_groups_selected['plain'] ?? []), [$agent['id_grupo']]) // Include_groups. // Size. // Simple_multiple_options. diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 4bbe32c69b..23f3437bd8 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -2095,106 +2095,8 @@ if ($delete_module) { exit; } - enterprise_include_once('include/functions_config_agents.php'); - enterprise_hook('config_agents_delete_module_in_conf', [modules_get_agentmodule_agent($id_borrar_modulo), modules_get_agentmodule_name($id_borrar_modulo)]); - - // Init transaction. - $error = 0; - - // First delete from tagente_modulo -> if not successful, increment - // error. NOTICE that we don't delete all data here, just marking for deletion - // and delete some simple data. - $values = [ - 'nombre' => 'pendingdelete', - 'disabled' => 1, - 'delete_pending' => 1, - ]; - $result = db_process_sql_update( - 'tagente_modulo', - $values, - ['id_agente_modulo' => $id_borrar_modulo] - ); - if ($result === false) { - $error++; - } else { - // Set flag to update module status count. - db_process_sql( - 'UPDATE tagente - SET update_module_count = 1, update_alert_count = 1 - WHERE id_agente = '.$module_data['id_agente'] - ); - } - - $result = db_process_sql_delete( - 'tagente_estado', - ['id_agente_modulo' => $id_borrar_modulo] - ); - if ($result === false) { - $error++; - } - - $result = db_process_sql_delete( - 'tagente_datos_inc', - ['id_agente_modulo' => $id_borrar_modulo] - ); - if ($result === false) { - $error++; - } - - if (alerts_delete_alert_agent_module( - false, - ['id_agent_module' => $id_borrar_modulo] - ) === false - ) { - $error++; - } - - $result = db_process_delete_temp( - 'ttag_module', - 'id_agente_modulo', - $id_borrar_modulo - ); - if ($result === false) { - $error++; - } - - // Trick to detect if we are deleting a synthetic module (avg or arithmetic) - // If result is empty then module doesn't have this type of submodules. - $ops_json = enterprise_hook('modules_get_synthetic_operations', [$id_borrar_modulo]); - $result_ops_synthetic = json_decode($ops_json); - if (!empty($result_ops_synthetic)) { - $result = enterprise_hook('modules_delete_synthetic_operations', [$id_borrar_modulo]); - if ($result === false) { - $error++; - } - } else { - $result_components = enterprise_hook('modules_get_synthetic_components', [$id_borrar_modulo]); - $count_components = 1; - if (!empty($result_components)) { - // Get number of components pending to delete to know when it's needed to update orders. - $num_components = count($result_components); - $last_target_module = 0; - foreach ($result_components as $id_target_module) { - // Detects change of component or last component to update orders. - if (($count_components == $num_components) - || ($last_target_module != $id_target_module) - ) { - $update_orders = true; - } else { - $update_orders = false; - } - - $result = enterprise_hook('modules_delete_synthetic_operations', [$id_target_module, $id_borrar_modulo, $update_orders]); - - if ($result === false) { - $error++; - } - - $count_components++; - $last_target_module = $id_target_module; - } - } - } + // Also call base function to delete modules madafakas de los cojones. + modules_delete_agent_module($id_borrar_modulo); // Check for errors. if ($error != 0) { diff --git a/pandora_console/godmode/agentes/module_manager_editor_common.php b/pandora_console/godmode/agentes/module_manager_editor_common.php index 5e994499f3..dc0a0e4259 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_common.php +++ b/pandora_console/godmode/agentes/module_manager_editor_common.php @@ -36,6 +36,7 @@ include_javascript_d3(); global $config; + function prepend_table_simple($row, $id=false) { global $table_simple; @@ -598,8 +599,12 @@ if ($moduletype == MODULE_DATA) { $table_advanced->colspan[1][1] = 2; $interval_factor = 1; if (isset($id_agente)) { - $agent_interval = agents_get_interval($id_agente); - $interval_factor = ($interval / $agent_interval); + $agent_interval = (float) agents_get_interval($id_agente); + if ($agent_interval > 0) { + $interval = (float) $interval; + $interval_factor = ($interval / $agent_interval); + } + $table_advanced->data[1][1] = human_time_description_raw($interval).' ('.sprintf(__('Agent interval x %s'), $interval_factor).') '; } else { $table_advanced->data[1][1] = sprintf(__('Agent interval x %s'), $interval_factor); diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index c0f27fa0a6..6b6092b0a7 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -2417,6 +2417,30 @@ function agents_delete_agent($id_agents, $disableACL=false) $id_agent ); + // Process a controlled module ellimination, keeping the old behaviour + // a couple of lines below this section. + try { + $filter = ['id_agente' => $id_agent]; + $modules = []; + $rows = \db_get_all_rows_filter( + 'tagente_modulo', + $filter + ); + + if (is_array($rows) === true) { + foreach ($rows as $row) { + $modules[] = PandoraFMS\Module::build($row); + } + } + + foreach ($modules as $module) { + $module->delete(); + } + } catch (Exception $e) { + // Ignore. + error_log($e->getMessage().' in '.$e->getFile().':'.$e->getLine()); + } + // The status of the module db_process_delete_temp('tagente_estado', 'id_agente', $id_agent); @@ -2456,15 +2480,27 @@ function agents_delete_agent($id_agents, $disableACL=false) $target_filter ); - foreach ($commands as $command) { - $rcmd_id = $command['rcmd_id']; - $rcmd = new RCMDFile($rcmd_id); + if (is_array($commands) === true) { + foreach ($commands as $command) { + $rcmd_id = $command['rcmd_id']; + $rcmd = new RCMDFile($rcmd_id); - $command_targets = []; + $command_targets = []; - $command_targets = $rcmd->getTargets(false, $target_filter); - $rcmd->deleteTargets(array_keys($command_targets)); + $command_targets = $rcmd->getTargets(false, $target_filter); + $rcmd->deleteTargets(array_keys($command_targets)); + } } + + // Remove agents from service child list. + enterprise_include_once('include/functions_services.php'); + \enterprise_hook( + 'service_elements_removal_tool', + [ + $id_agent, + SERVICE_ELEMENT_AGENT, + ] + ); } // tagente_datos_inc diff --git a/pandora_console/include/functions_io.php b/pandora_console/include/functions_io.php index d22641d4c7..5edc3e5f49 100755 --- a/pandora_console/include/functions_io.php +++ b/pandora_console/include/functions_io.php @@ -524,10 +524,13 @@ function io_json_mb_encode($string, $encode_options=0) $v = json_encode($string, $encode_options); $v = preg_replace_callback( "/\\\\u([0-9a-zA-Z]{4})/", - create_function( - '$matches', - 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UTF-16");' - ), + function ($matches) { + return mb_convert_encoding( + pack('H*', $matches[1]), + 'UTF-8', + 'UTF-16' + ); + }, $v ); $v = preg_replace('/\\\\\//', '/', $v); diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index 1a7548174b..4bfde3ca0b 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -387,7 +387,7 @@ function modules_change_disabled($id_agent_module, $new_value=1) * * @param mixed Agent module id to be deleted. Accepts an array with ids. * - * @return True if the module was deleted. False if not. + * @return boolean True if the module was deleted. False if not. */ function modules_delete_agent_module($id_agent_module) { @@ -455,6 +455,16 @@ function modules_delete_agent_module($id_agent_module) } } + // Remove module from service child list. + enterprise_include_once('include/functions_services.php'); + \enterprise_hook( + 'service_elements_removal_tool', + [ + $id_agent_module, + SERVICE_ELEMENT_MODULE, + ] + ); + alerts_delete_alert_agent_module(0, $where); db_process_sql_delete('tgraph_source', $where); @@ -477,6 +487,130 @@ function modules_delete_agent_module($id_agent_module) ); db_process_sql_delete('ttag_module', $where); + $id_borrar_modulo = $id_agent_module; + + enterprise_include_once('include/functions_config_agents.php'); + enterprise_hook( + 'config_agents_delete_module_in_conf', + [ + modules_get_agentmodule_agent($id_borrar_modulo), + modules_get_agentmodule_name($id_borrar_modulo), + ] + ); + + // Init transaction. + $error = 0; + + // First delete from tagente_modulo -> if not successful, increment + // error. NOTICE that we don't delete all data here, just marking for deletion + // and delete some simple data. + $values = [ + 'nombre' => 'pendingdelete', + 'disabled' => 1, + 'delete_pending' => 1, + ]; + $result = db_process_sql_update( + 'tagente_modulo', + $values, + ['id_agente_modulo' => $id_borrar_modulo] + ); + if ($result === false) { + $error++; + } else { + // Set flag to update module status count. + db_process_sql( + 'UPDATE tagente + SET update_module_count = 1, update_alert_count = 1 + WHERE id_agente = '.$id_agent + ); + } + + $result = db_process_sql_delete( + 'tagente_estado', + ['id_agente_modulo' => $id_borrar_modulo] + ); + if ($result === false) { + $error++; + } + + $result = db_process_sql_delete( + 'tagente_datos_inc', + ['id_agente_modulo' => $id_borrar_modulo] + ); + if ($result === false) { + $error++; + } + + if (alerts_delete_alert_agent_module( + false, + ['id_agent_module' => $id_borrar_modulo] + ) === false + ) { + $error++; + } + + $result = db_process_delete_temp( + 'ttag_module', + 'id_agente_modulo', + $id_borrar_modulo + ); + if ($result === false) { + $error++; + } + + // Trick to detect if we are deleting a synthetic module (avg or arithmetic) + // If result is empty then module doesn't have this type of submodules. + $ops_json = enterprise_hook( + 'modules_get_synthetic_operations', + [$id_borrar_modulo] + ); + $result_ops_synthetic = json_decode($ops_json); + if (!empty($result_ops_synthetic)) { + $result = enterprise_hook( + 'modules_delete_synthetic_operations', + [$id_borrar_modulo] + ); + if ($result === false) { + $error++; + } + } else { + $result_components = enterprise_hook( + 'modules_get_synthetic_components', + [$id_borrar_modulo] + ); + $count_components = 1; + if (!empty($result_components)) { + // Get number of components pending to delete to know when it's needed to update orders. + $num_components = count($result_components); + $last_target_module = 0; + foreach ($result_components as $id_target_module) { + $update_orders = false; + // Detects change of component or last component to update orders. + if (($count_components == $num_components) + || ($last_target_module != $id_target_module) + ) { + $update_orders = true; + } + + $result = enterprise_hook( + 'modules_delete_synthetic_operations', + [ + $id_target_module, + $id_borrar_modulo, + $update_orders, + ] + ); + + if ($result === false) { + $error++; + } + + $count_components++; + $last_target_module = $id_target_module; + } + } + } + return true; } diff --git a/pandora_console/include/lib/Agent.php b/pandora_console/include/lib/Agent.php index 3d97b86c00..0063b071b8 100644 --- a/pandora_console/include/lib/Agent.php +++ b/pandora_console/include/lib/Agent.php @@ -588,6 +588,13 @@ class Agent extends Entity $this->fields['id_agente'] ); + // Delete modules. + if ($this->modules !== null) { + foreach ($this->modules as $module) { + $module->delete(); + } + } + unset($this->fields); unset($this->modules); } diff --git a/pandora_console/include/lib/Module.php b/pandora_console/include/lib/Module.php index 4a49bd5435..18f4366d3c 100644 --- a/pandora_console/include/lib/Module.php +++ b/pandora_console/include/lib/Module.php @@ -228,8 +228,10 @@ class Module extends Entity throw $e; } - if ($this->nombre() === 'delete_pending') { - return null; + if ($this->nombre() === 'delete_pending' + || $this->nombre() === 'pendingdelete' + ) { + throw new \Exception('Object is pending to be deleted', 1); } if ($link_agent === true) { @@ -314,6 +316,30 @@ class Module extends Entity } + /** + * Get/set for disable field, this method also takes in mind the status of + * assigned agent (if any). + * + * @param boolean|null $disabled Used in set operations. + * + * @return boolean|null Return disabled status for this module or null if + * set operation. + */ + public function disabled(?bool $disabled=null) + { + if ($disabled === null) { + if ($this->agent() !== null) { + return ((bool) $this->fields['disabled'] || (bool) $this->agent()->disabled()); + } + + return ((bool) $this->fields['disabled']); + } + + $this->fields['disabled'] = $disabled; + return null; + } + + /** * Dynamically call methods in this object. * @@ -392,9 +418,7 @@ class Module extends Entity } } - throw new \Exception( - get_class($this).' error, method '.$methodName.' does not exist' - ); + return parent::__call($methodName, $params); } diff --git a/pandora_console/index.php b/pandora_console/index.php index b42fb3e1fb..0f50391be1 100755 --- a/pandora_console/index.php +++ b/pandora_console/index.php @@ -1124,9 +1124,15 @@ if ($searchPage) { include 'general/noaccess.php'; } else { $sec = $main_sec; - if (file_exists($page)) { - if (! extensions_is_extension($page)) { - include_once $page; + if (file_exists($page) === true) { + if ((bool) extensions_is_extension($page) === false) { + try { + include_once $page; + } catch (Exception $e) { + ui_print_error_message( + $e->getMessage().' in '.$e->getFile().':'.$e->getLine() + ); + } } else { if ($sec[0] == 'g') { extensions_call_godmode_function(basename($page)); diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index 4c5b25a469..52083168e3 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -1325,7 +1325,14 @@ $agent_interfaces = agents_get_network_interfaces( ['id_agente' => $id_agente] ); -$agent_interfaces_count = count($agent_interfaces[$id_agente]['interfaces']); +if (is_array($agent_interfaces[$id_agente]['interfaces']) !== true + || is_object($agent_interfaces[$id_agente]['interfaces']) !== true +) { + $agent_interfaces_count = 0; +} else { + $agent_interfaces_count = count($agent_interfaces[$id_agente]['interfaces']); +} + if ($agent_interfaces_count > 0) { $interfacetab['text'] = ''.html_print_image(