From 55e272c360d1c5a655aa5a6a2be3950783920634 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 6 Apr 2021 12:53:12 +0200 Subject: [PATCH] MC dashboards agent_module fixes --- pandora_console/include/functions_agents.php | 30 ++++-- pandora_console/include/functions_html.php | 24 ++++- .../lib/Dashboard/Widgets/agent_module.php | 71 ++++++++++---- pandora_console/include/lib/Module.php | 2 +- .../operation/agentes/ver_agente.php | 92 ++++++++++++++++++- 5 files changed, 192 insertions(+), 27 deletions(-) diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index bbf7fb5455..a40bf0bab5 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -1084,6 +1084,7 @@ function agents_common_modules($id_agent, $filter=false, $indexed=true, $get_not * @param string $separator Only in metaconsole. Separator for the serialized data. By default |. * @param boolean $add_alert_bulk_op //TODO documentation * @param boolean $force_serialized. If the agent has not id_server (typically in node) put 0 as . + * @param boolean $meta_fields If true, then id_agente is returned instead id_tagente. * * @return array An array with all agents in the group or an empty array */ @@ -1096,7 +1097,8 @@ function agents_get_group_agents( $serialized=false, $separator='|', $add_alert_bulk_op=false, - $force_serialized=false + $force_serialized=false, + $meta_fields=false ) { global $config; @@ -1268,11 +1270,19 @@ function agents_get_group_agents( if (is_metaconsole()) { $table_name = 'tmetaconsole_agent ta LEFT JOIN tmetaconsole_agent_secondary_group tasg ON ta.id_agente = tasg.id_agent'; - $fields = [ - 'ta.id_tagente AS id_agente', - 'alias', - 'ta.id_tmetaconsole_setup AS id_server', - ]; + if ($meta_fields === true) { + $fields = [ + 'id_agente', + 'alias', + 'ta.id_tmetaconsole_setup AS id_server', + ]; + } else { + $fields = [ + 'ta.id_tagente AS id_agente', + 'alias', + 'ta.id_tmetaconsole_setup AS id_server', + ]; + } } else { $table_name = 'tagente LEFT JOIN tagent_secondary_group ON id_agente=id_agent'; @@ -3253,6 +3263,7 @@ function agents_get_agent_custom_field($agent_id, $custom_field_name) * @param boolean $selection Show common (false) or all modules (true). * @param boolean $return Return (false) or dump to output (true). * @param boolean $index_by_name Use module name as key. + * @param boolean $pure_return Return as retrieved from DB. * * @return array With modules or null if error. */ @@ -3261,7 +3272,8 @@ function select_modules_for_agent_group( $id_agents, $selection, $return=true, - $index_by_name=false + $index_by_name=false, + $pure_return=false ) { global $config; $agents = (empty($id_agents)) ? [] : implode(',', $id_agents); @@ -3341,6 +3353,10 @@ function select_modules_for_agent_group( return; } + if ($pure_return === true) { + return $modules; + } + $modules_array = []; foreach ($modules as $value) { if ($index_by_name) { diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 67263574bc..284adc9486 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -1351,7 +1351,29 @@ function html_print_select_multiple_modules_filtered(array $data):string $output .= '
'; // Agent. - $agents = agents_get_group_agents($data['mGroup']); + $agents = agents_get_group_agents( + // Id_group. + $data['mGroup'], + // Search. + false, + // Case. + 'lower', + // NoACL. + false, + // ChildGroups. + false, + // Serialized. + false, + // Separator. + '|', + // Add_alert_bulk_op. + false, + // Force_serialized. + false, + // Meta_fields. + $data['mMetaFields'] + ); + if ((empty($agents)) === true || $agents == -1) { $agents = []; } diff --git a/pandora_console/include/lib/Dashboard/Widgets/agent_module.php b/pandora_console/include/lib/Dashboard/Widgets/agent_module.php index b736c0e409..a06a730141 100644 --- a/pandora_console/include/lib/Dashboard/Widgets/agent_module.php +++ b/pandora_console/include/lib/Dashboard/Widgets/agent_module.php @@ -266,6 +266,7 @@ class AgentModuleWidget extends Widget 'mModules' => $this->values['mModules'], 'mShowSelectedOtherGroups' => true, 'mReturnAllGroup' => $return_all_group, + 'mMetaFields' => ((bool) is_metaconsole()), ], ]; @@ -329,7 +330,7 @@ class AgentModuleWidget extends Widget $name = $module; $modules_by_name[$cont]['name'] = $name; $modules_by_name[$cont]['id'][] = $key; - $cont ++; + $cont++; } } @@ -351,7 +352,10 @@ class AgentModuleWidget extends Widget $agent_modules = db_get_all_rows_sql($sql); - $agent_modules = array_combine(array_column($agent_modules, 'id_agente_modulo'), array_column($agent_modules, 'nombre')); + $agent_modules = array_combine( + array_column($agent_modules, 'id_agente_modulo'), + array_column($agent_modules, 'nombre') + ); $row['modules'] = []; foreach ($modules_by_name as $module) { @@ -449,6 +453,10 @@ class AgentModuleWidget extends Widget $table_data .= $file_name; $table_data .= ''; + if ($row['modules'] === null) { + $row['modules'] = []; + } + foreach ($row['modules'] as $module_name => $module) { if ($module === null) { if (in_array($module_name, $allModules) === true) { @@ -591,6 +599,7 @@ class AgentModuleWidget extends Widget { global $config; + $output = ''; if (check_acl($config['id_user'], 0, 'AR') === 0) { $output .= '
'; $output .= ui_print_error_message( @@ -602,27 +611,44 @@ class AgentModuleWidget extends Widget return $output; } - // Estract info all modules selected. + // Extract info all modules selected. $target_modules = explode(',', $this->values['mModules']); $all_modules = Module::search( ['id_agente_modulo' => $target_modules] ); - $reduceAllModules = array_reduce( - $all_modules, - function ($carry, $item) { - $carry[$item->name()] = null; - return $carry; - } - ); - - \ksort($reduceAllModules); + if ($all_modules !== null) { + $reduceAllModules = array_reduce( + $all_modules, + function ($carry, $item) { + $carry[$item->name()] = null; + return $carry; + } + ); + } else { + $reduceAllModules = []; + } $visualData = []; - // Estract info agents selected. + // Extract info agents selected. $target_agents = explode(',', $this->values['mAgents']); foreach ($target_agents as $agent_id) { try { - $agent = new Agent($agent_id); + $id_agente = $agent_id; + if ((bool) is_metaconsole() === true) { + $tmeta_agent = db_get_row_filter( + 'tmetaconsole_agent', + [ 'id_agente' => $id_agente ] + ); + + $id_agente = $tmeta_agent['id_tagente']; + $tserver = $tmeta_agent['id_tmetaconsole_setup']; + + if (metaconsole_connect(null, $tserver) !== NOERR) { + continue; + } + } + + $agent = new Agent((int) $id_agente); $visualData[$agent_id]['agent_status'] = $agent->lastStatus(); $visualData[$agent_id]['agent_name'] = $agent->name(); $visualData[$agent_id]['agent_alias'] = $agent->alias(); @@ -633,14 +659,27 @@ class AgentModuleWidget extends Widget $visualData[$agent_id]['modules'] = $reduceAllModules; foreach ($modules as $module) { + if ((bool) is_metaconsole() === true) { + $reduceAllModules[$module->name()] = null; + } + $visualData[$agent_id]['modules'][$module->name()] = $module->getStatus()->estado(); } - } catch (Exception $e) { - echo 'Error: '.$e->getMessage(); + + if ((bool) is_metaconsole() === true) { + metaconsole_restore_db(); + } + } catch (\Exception $e) { + echo 'Error: ['.$agent_id.']'.$e->getMessage(); } } + ksort($reduceAllModules); $allModules = array_keys($reduceAllModules); + if ($allModules === null) { + $allModules = []; + } + $output = $this->generateViewAgentModule( $visualData, $allModules diff --git a/pandora_console/include/lib/Module.php b/pandora_console/include/lib/Module.php index f63f3e0492..3e11480c3e 100644 --- a/pandora_console/include/lib/Module.php +++ b/pandora_console/include/lib/Module.php @@ -88,7 +88,7 @@ class Module extends Entity * @param array $params Search parameters (fields from tagente_modulo). * @param integer $limit Limit results to N rows. * - * @return PandoraFMS\Module found or null if not found. + * @return array|null of PandoraFMS\Module found or null if not found. * @throws \Exception On error. */ public static function search( diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index 871ef7b73f..e2c7949f76 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -163,7 +163,9 @@ if (is_ajax()) { // Add_alert_bulk_op. $cluster_mode, // Force_serialized. - false + false, + // Meta fields. + (bool) is_metaconsole() ); if (empty($agents)) { @@ -192,7 +194,93 @@ if (is_ajax()) { $id_agents = get_parameter('id_agents'); $selection = get_parameter('selection'); - select_modules_for_agent_group($id_group, $id_agents, $selection); + if ((bool) is_metaconsole() === true) { + if (count($id_agents) > 0) { + $rows = db_get_all_rows_sql( + sprintf( + 'SELECT `id_agente`, `id_tagente`, `id_tmetaconsole_setup` + FROM `tmetaconsole_agent` + WHERE `id_agente` IN (%s)', + implode(',', $id_agents) + ) + ); + } else { + $rows = []; + } + + $agents = array_reduce( + $rows, + function ($carry, $item) { + if ($carry[$item['id_tmetaconsole_setup']] === null) { + $carry[$item['id_tmetaconsole_setup']] = []; + } + + $carry[$item['id_tmetaconsole_setup']][] = $item['id_tagente']; + return $carry; + }, + [] + ); + + $modules = []; + + foreach ($agents as $tserver => $id_agents) { + if (metaconsole_connect(null, $tserver) == NOERR) { + $modules[$tserver] = select_modules_for_agent_group( + $id_group, + $id_agents, + $selection, + false, + false, + true + ); + + metaconsole_restore_db(); + } + } + + + if (!$selection) { + // Common modules. + $final_modules = []; + $nodes_consulted = count($modules); + + foreach ($modules as $tserver => $mods) { + foreach ($mods as $module) { + if ($final_modules[$module['nombre']] === null) { + $final_modules[$module['nombre']] = 0; + } + + $final_modules[$module['nombre']]++; + } + } + + $modules = []; + $i = 1; + foreach ($final_modules as $module_name => $occurrences) { + if ($occurrences === $nodes_consulted) { + // Module already present in ALL nodes. + $modules[] = [ + 'id_agente_modulo' => ($i++), + 'nombre' => $module_name, + ]; + } + } + } else { + // All modules. + $modules = array_reduce( + $modules[$tserver], + function ($carry, $item) { + $carry[] = $item; + return $carry; + }, + [] + ); + } + + echo json_encode($modules); + } else { + select_modules_for_agent_group($id_group, $id_agents, $selection); + } } if ($get_modules_group_value_name_json) {