diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index dc7600f8af..9fd5316eb1 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -1702,30 +1702,12 @@ $class = 'databox filters'; $group], ['id_agente', 'alias']); - foreach ($all_agent_log as $key => $value) { - $agents2[$value['id_agente']] = $value['alias']; - } - - if ((empty($agents2)) || $agents2 == -1) { - $agents = []; - } - - $agents_select = []; - if (is_array($id_agents) || is_object($id_agents)) { - foreach ($id_agents as $id) { - foreach ($agents2 as $key => $a) { - if ($key == (int) $id) { - $agents_select[$key] = $key; - } - } - } - } + $all_agents = agents_get_agents_selected($group); html_print_select( - $agents2, + $all_agents, 'id_agents2[]', - $agents_select, + $id_agents, $script = '', '', 0, @@ -1789,39 +1771,20 @@ $class = 'databox filters'; $group], + [ + 'id_tagente', + 'id_tmetaconsole_setup', + 'id_agente', + 'alias', + ], + 'AR', + [ + 'field' => 'alias', + 'order' => 'ASC', + ], + false, + 0, + true + ); + + $all = array_reduce( + $all, + function ($carry, $item) { + $carry[$item['id_tmetaconsole_setup'].'|'.$item['id_tagente']] = $item['alias']; + return $carry; + }, + [] + ); + } else { + $all = agents_get_agents( + ['id_grupo' => $group], + [ + 'id_agente', + 'alias', + ], + 'AR', + [ + 'field' => 'alias', + 'order' => 'ASC', + ] + ); + + $all = array_reduce( + $all, + function ($carry, $item) { + $carry[$item['id_agente']] = $item['alias']; + return $carry; + }, + [] + ); + } + + return $all; +} + + /** * Get all the alerts of an agent, simple and combined. * @@ -1662,27 +1719,67 @@ function agents_get_alias_array($array_ids) return []; } - $sql = sprintf( - 'SELECT id_agente as id, alias as `name` - FROM tagente - WHERE id_agente IN (%s)', - implode(',', $array_ids) - ); + if ((bool) is_metaconsole() === true) { + $agents = array_reduce( + $array_ids, + function ($carry, $item) { + $explode = explode('|', $item); - $result = db_get_all_rows_sql($sql); + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); - if ($result === false) { $result = []; - } + foreach ($agents as $tserver => $id_agents) { + $sql = sprintf( + 'SELECT id_tagente as id, alias as `name` + FROM tmetaconsole_agent + WHERE id_tagente IN (%s) AND id_tmetaconsole_setup = %d', + implode(',', $id_agents), + $tserver + ); - $result = array_reduce( - $result, - function ($carry, $item) { - $carry[$item['id']] = $item['name']; - return $carry; - }, - [] - ); + $data_server = db_get_all_rows_sql($sql); + + if ($data_server === false) { + $data_server = []; + } + + $data_server = array_reduce( + $data_server, + function ($carry, $item) { + $carry[$item['id']] = $item['name']; + return $carry; + }, + [] + ); + + $result[$tserver] = $data_server; + } + } else { + $sql = sprintf( + 'SELECT id_agente as id, alias as `name` + FROM tagente + WHERE id_agente IN (%s)', + implode(',', $array_ids) + ); + + $result = db_get_all_rows_sql($sql); + + if ($result === false) { + $result = []; + } + + $result = array_reduce( + $result, + function ($carry, $item) { + $carry[$item['id']] = $item['name']; + return $carry; + }, + [] + ); + } return $result; } diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index 1ccfa9ada5..b1f6226a7f 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2887,12 +2887,18 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) hd(5); + $table = 'tevento'; + if (is_metaconsole() === true) { + $table = 'tmetaconsole_event'; + } + $filter_date = ''; if (isset($filters['period']) === true && empty($filters['period']) === false ) { $filter_date = sprintf( - 'AND tevento.utimestamp > %d', + 'AND %s.utimestamp > %d', + $table, (time() - $filters['period']) ); } @@ -2902,7 +2908,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) && empty($filters['group']) === false ) { $filter_group = sprintf( - 'AND tevento.id_grupo = %d', + 'AND %s.id_grupo = %d', + $table, $filters['group'] ); } @@ -2911,30 +2918,98 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) if (isset($filters['agents']) === true && empty($filters['agents']) === false ) { - $filter_agents = sprintf( - 'AND tevento.id_agente IN (%s)', - implode(',', $filters['agents']) - ); + if (is_metaconsole() === true) { + $agents = array_reduce( + $filters['agents'], + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + + $filter_agents .= ' AND ( '; + $i = 0; + foreach ($agents as $tserver => $agent) { + if ($i !== 0) { + $filter_agents .= ' OR '; + } + + $filter_agents .= sprintf( + '( %s.id_agente IN (%s) AND %s.server_id = %d )', + $table, + implode(',', $agent), + $table, + (int) $tserver + ); + + $i++; + } + + $filter_agents .= ' )'; + } else { + $filter_agents = sprintf( + 'AND %s.id_agente IN (%s)', + $table, + implode(',', $filters['agents']) + ); + } } $filter_modules = ''; if (isset($filters['modules']) === true && empty($filters['modules']) === false ) { - $filter_modules = sprintf( - 'AND tevento.id_agentmodule IN (%s)', - implode(',', $filters['modules']) - ); + if (is_metaconsole() === true) { + $modules = array_reduce( + $filters['modules'], + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + + $filter_modules .= ' AND ( '; + $i = 0; + foreach ($modules as $tserver => $module) { + if ($i !== 0) { + $filter_modules .= ' OR '; + } + + $filter_modules .= sprintf( + '( %s.id_agentmodule IN (%s) AND %s.server_id = %d )', + $table, + implode(',', $module), + $table, + (int) $tserver + ); + + $i++; + } + + $filter_modules .= ' )'; + } else { + $filter_modules = sprintf( + 'AND %s.id_agentmodule IN (%s)', + $table, + implode(',', $filters['modules']) + ); + } } $filter_templates = ''; if (isset($filters['templates']) === true && empty($filters['templates']) === false ) { - $filter_templates = sprintf( - 'AND talert_template_modules.id_alert_template IN (%s)', - implode(',', $filters['templates']) - ); + if (is_metaconsole() === false) { + $filter_templates = sprintf( + 'AND talert_template_modules.id_alert_template IN (%s)', + implode(',', $filters['templates']) + ); + } } $actions_names = alerts_get_actions_names($filters['actions'], true); @@ -2954,12 +3029,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) } $filter_actions .= sprintf( - "JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')", + "JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')", + $table, io_safe_output($name_action) ); $fields_actions[$name_action] = sprintf( - "SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'", + "SUM(JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')) as '%s'", + $table, io_safe_output($name_action), io_safe_output($name_action) ); @@ -2971,7 +3048,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) } else { foreach ($actions_names as $name_action) { $fields[] = sprintf( - "SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'", + "SUM(JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')) as '%s'", + $table, io_safe_output($name_action), io_safe_output($name_action) ); @@ -2987,35 +3065,49 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) } $names_search = []; + $names_server = []; if (isset($groupsBy['group_by']) === true) { switch ($groupsBy['group_by']) { case 'module': - $fields[] = 'tevento.id_agentmodule as module'; - $group_array[] = 'tevento.id_agentmodule'; + $fields[] = $table.'.id_agentmodule as module'; + $group_array[] = $table.'.id_agentmodule'; $names_search = modules_get_agentmodule_name_array( array_values($filters['modules']) ); + + if (is_metaconsole() === true && $total === false) { + $fields[] = $table.'.server_id as server'; + $group_array[] = $table.'.server_id'; + $names_server = metaconsole_get_names(); + } break; case 'template': - $fields[] = 'talert_template_modules.id_alert_template as template'; - $group_array[] = 'talert_template_modules.id_alert_template'; - $names_search = alerts_get_templates_name_array( - array_values($filters['templates']) - ); + if (is_metaconsole() === false) { + $fields[] = 'talert_template_modules.id_alert_template as template'; + $group_array[] = 'talert_template_modules.id_alert_template'; + $names_search = alerts_get_templates_name_array( + array_values($filters['templates']) + ); + } break; case 'agent': - $fields[] = 'tevento.id_agente as agent'; - $group_array[] = 'tevento.id_agente'; + $fields[] = $table.'.id_agente as agent'; + $group_array[] = $table.'.id_agente'; $names_search = agents_get_alias_array( array_values($filters['agents']) ); + if (is_metaconsole() === true && $total === false) { + $fields[] = $table.'.server_id as server'; + $group_array[] = $table.'.server_id'; + $names_server = metaconsole_get_names(); + } break; case 'group': - $fields[] = 'tevento.id_grupo as `group`'; - $group_array[] = 'tevento.id_grupo'; + $fields[] = $table.'.id_grupo as `group`'; + $group_array[] = $table.'.id_grupo'; $names_search = users_get_groups($config['user'], 'AR', false); break; @@ -3030,7 +3122,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) && empty($groupsBy['lapse']) === false ) { $fields[] = sprintf( - 'tevento.utimestamp AS Period' + '%s.utimestamp AS Period', + $table ); $group_array[] = 'period'; } @@ -3041,16 +3134,22 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) $group_by = sprintf(' GROUP BY %s', implode(", \n", $group_array)); } + $innerJoin = ''; + if (is_metaconsole() === false) { + $innerJoin = sprintf( + 'INNER JOIN talert_template_modules + ON talert_template_modules.id = %s.id_alert_am', + $table + ); + } + $query = sprintf( 'SELECT %s - FROM tevento - INNER JOIN talert_template_modules - ON talert_template_modules.id = tevento.id_alert_am - INNER JOIN talert_templates - ON talert_templates.id = talert_template_modules.id_alert_template + FROM %s + %s WHERE custom_data != "" - AND tevento.event_type="alert_fired" + AND %s.event_type="alert_fired" %s %s %s @@ -3059,6 +3158,9 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) %s %s', implode(", \n", $fields), + $table, + $innerJoin, + $table, $filter_date, $filter_group, $filter_agents, @@ -3080,10 +3182,23 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) $data, function ($carry, $item) use ($groupsBy) { $period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0; - $grby = $item[$groupsBy['group_by']]; - unset($item['Period']); - unset($item[$groupsBy['group_by']]); - $carry[$period][$grby] = $item; + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + $grby = $item[$groupsBy['group_by']]; + $server = $item['server']; + unset($item['Period']); + unset($item[$groupsBy['group_by']]); + unset($item['server']); + $carry[$period][$server][$grby] = $item; + } else { + $grby = $item[$groupsBy['group_by']]; + unset($item['Period']); + unset($item[$groupsBy['group_by']]); + $carry[$period][$grby] = $item; + } + return $carry; }, [] @@ -3095,13 +3210,20 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) ) { $tend = time(); $tstart = ($tend - (int) $filters['period']); - for ($current_time = $tstart; $current_time <= $tend; ($current_time += $groupsBy['lapse'])) { + for ($current_time = $tstart; $current_time < $tend; ($current_time += $groupsBy['lapse'])) { $intervals[] = (int) $current_time; } } $first_element = reset($data); $first_element = reset($first_element); + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + $first_element = reset($first_element); + } + $clone = []; foreach ($first_element as $key_clone => $value_clone) { $clone[$key_clone] = 0; @@ -3110,13 +3232,30 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) $result = []; if (empty($intervals) === true) { foreach ($data as $period => $array_data) { - foreach ($names_search as $id => $name) { - if (isset($array_data[$id]) === true) { - $result[$period][$id] = $array_data[$id]; - $result[$period][$id][$groupsBy['group_by']] = $name; - } else { - $clone[$groupsBy['group_by']] = $name; - $result[$period][$id] = $clone; + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($names_search as $server => $names) { + foreach ($names as $id => $name) { + if (isset($array_data[$server][$id]) === true) { + $result[$period][$server.'|'.$id] = $array_data[$server][$id]; + $result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name; + } else { + $clone[$groupsBy['group_by']] = $name; + $result[$period][$server.'|'.$id] = $clone; + } + } + } + } else { + foreach ($names_search as $id => $name) { + if (isset($array_data[$id]) === true) { + $result[$period][$id] = $array_data[$id]; + $result[$period][$id][$groupsBy['group_by']] = $name; + } else { + $clone[$groupsBy['group_by']] = $name; + $result[$period][$id] = $clone; + } } } } @@ -3125,21 +3264,54 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) foreach ($intervals as $interval) { $start_interval = $interval; $end_interval = ($interval + $period_lapse); - foreach ($names_search as $id => $name) { - $result[$start_interval][$id] = $clone; - $result[$start_interval][$id][$groupsBy['group_by']] = $name; + + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($names_search as $server => $names) { + foreach ($names as $id => $name) { + $result_name = $names_server[$server].' » '.$name; + $result[$start_interval][$server.'|'.$id] = $clone; + $result[$start_interval][$server.'|'.$id][$groupsBy['group_by']] = $result_name; + } + } + } else { + foreach ($names_search as $id => $name) { + $result[$start_interval][$id] = $clone; + $result[$start_interval][$id][$groupsBy['group_by']] = $name; + } } foreach ($data as $period => $array_data) { $period_time = (int) $period; if ($start_interval < $period_time && $period_time <= $end_interval) { - foreach ($array_data as $id_data => $value_data) { - foreach ($value_data as $key_data => $v) { - if ($key_data !== $groupsBy['group_by']) { - if (isset($result[$start_interval][$id_data][$key_data])) { - $result[$start_interval][$id_data][$key_data] += $v; - } else { - $result[$start_interval][$id_data][$key_data] = $v; + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($array_data as $server => $datas) { + foreach ($datas as $id_data => $value_data) { + foreach ($value_data as $key_data => $v) { + if ($key_data !== $groupsBy['group_by']) { + if (isset($result[$start_interval][$server.'|'.$id_data][$key_data])) { + $result[$start_interval][$server.'|'.$id_data][$key_data] += $v; + } else { + $result[$start_interval][$server.'|'.$id_data][$key_data] = $v; + } + } + } + } + } + } else { + foreach ($array_data as $id_data => $value_data) { + foreach ($value_data as $key_data => $v) { + if ($key_data !== $groupsBy['group_by']) { + if (isset($result[$start_interval][$id_data][$key_data])) { + $result[$start_interval][$id_data][$key_data] += $v; + } else { + $result[$start_interval][$id_data][$key_data] = $v; + } } } } diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index 5993c60202..4f27b17896 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -26,6 +26,8 @@ * ============================================================================ */ +use PandoraFMS\Enterprise\Metaconsole\Node; + // Begin. require_once $config['homedir'].'/include/functions_agents.php'; require_once $config['homedir'].'/include/functions_users.php'; @@ -1448,6 +1450,51 @@ function modules_get_agentmodule_name_array($array_ids) return []; } + if ((bool) is_metaconsole() === true) { + $modules = array_reduce( + $array_ids, + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + + $result = []; + foreach ($modules as $tserver => $id_modules) { + if (metaconsole_connect(null, $tserver) == NOERR) { + $result_modules = modules_get_agentmodule_name_array_data( + $id_modules + ); + + $result[$tserver] = $result_modules; + metaconsole_restore_db(); + } + } + } else { + $result = modules_get_agentmodule_name_array_data( + $array_ids + ); + } + + return $result; +} + + +/** + * Data names. + * + * @param array $array_ids Ids. + * + * @return array + */ +function modules_get_agentmodule_name_array_data($array_ids) +{ + if (is_array($array_ids) === false || empty($array_ids) === true) { + return []; + } + $sql = sprintf( 'SELECT id_agente_modulo as id, nombre as `name` FROM tagente_modulo @@ -3508,6 +3555,145 @@ function modules_get_agentmodule_mininterval_no_async($id_agent) } +function get_modules_agents($id_module_group, $id_agents, $selection, $select_mode=true) +{ + if ((bool) is_metaconsole() === true) { + if ($select_mode === true) { + $agents = array_reduce( + $id_agents, + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + } else { + 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_module_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 = []; + foreach ($final_modules as $module_name => $occurrences) { + if ($occurrences === $nodes_consulted) { + // Module already present in ALL nodes. + $modules[] = [ + 'id_agente_modulo' => $module_name, + 'nombre' => $module_name, + ]; + } + } + } else { + // All modules. + $return = []; + $nodes = []; + foreach ($agents as $tserver => $id_agents) { + try { + $nodes[$tserver] = new Node($tserver); + } catch (Exception $e) { + hd($e); + } + + $return = array_reduce( + $modules[$tserver], + function ($carry, $item) use ($tserver, $nodes) { + $t = []; + foreach ($item as $k => $v) { + $t[$k] = $v; + } + + $t['id_node'] = $tserver; + if ($nodes[$tserver] !== null) { + $t['nombre'] = io_safe_output( + $nodes[$tserver]->server_name().' » '.$t['nombre'] + ); + } + + $carry[] = $t; + return $carry; + }, + $return + ); + } + + $modules = $return; + } + + $modules = array_reduce( + $modules, + function ($carry, $item) { + $carry[$item['id_node'].'|'.$item['id_agente_modulo']] = $item['nombre']; + return $carry; + }, + [] + ); + } else { + $modules = select_modules_for_agent_group( + $id_module_group, + $id_agents, + $selection, + false + ); + } + + return $modules; +} + + /** * List all modules in agents selection. * @@ -3522,7 +3708,9 @@ function get_same_modules($agents, $modules) return []; } - $name_modules = modules_get_agentmodule_name_array(array_values($modules)); + $name_modules = modules_get_agentmodule_name_array_data( + array_values($modules) + ); $sql = sprintf( 'SELECT id_agente_modulo as id, @@ -3557,6 +3745,84 @@ function get_same_modules($agents, $modules) } +/** + * List all modules in agents selection to metaconsole or node. + * + * @param array $agents Agents ids array. + * @param array $modules Modules ids array. + * + * @return array List modules [server|id_module, ...]. + */ +function get_same_modules_all($agents, $modules, $select_mode=true) +{ + if (is_array($agents) === false || empty($agents) === true) { + return []; + } + + if (is_metaconsole() === true) { + $modules = array_reduce( + $modules, + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + + if ($select_mode === true) { + $agents = array_reduce( + $agents, + function ($carry, $item) { + $explode = explode('|', $item); + + $carry[$explode[0]][] = $explode[1]; + return $carry; + } + ); + } else { + $rows = db_get_all_rows_sql( + sprintf( + 'SELECT `id_agente`, `id_tagente`, `id_tmetaconsole_setup` + FROM `tmetaconsole_agent` + WHERE `id_agente` IN (%s)', + implode(',', $agents) + ) + ); + + $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; + }, + [] + ); + } + + $result = []; + foreach ($agents as $tserver => $id_agents) { + if (metaconsole_connect(null, $tserver) == NOERR) { + $same_modules = get_same_modules($id_agents, $modules[$tserver]); + foreach ($same_modules as $id_module) { + $result[] = $tserver.'|'.$id_module; + } + + metaconsole_restore_db(); + } + } + } else { + $result = get_same_modules($agents, $modules); + } + + return $result; +} + + function get_hierachy_modules_tree($modules) { $new_modules = []; diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index cbbdd9e925..617f62b97b 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -192,127 +192,25 @@ if (is_ajax()) { return; } - if ($get_modules_group_json) { + if ($get_modules_group_json === true) { $id_group = (int) get_parameter('id_module_group', 0); $id_agents = get_parameter('id_agents', null); $selection = get_parameter('selection'); + $select_mode = (bool) get_parameter('select_mode', 0); if ($id_agents === null) { echo '[]'; return; } - 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 = []; - foreach ($final_modules as $module_name => $occurrences) { - if ($occurrences === $nodes_consulted) { - // Module already present in ALL nodes. - $modules[] = [ - 'id_agente_modulo' => $module_name, - 'nombre' => $module_name, - ]; - } - } - } else { - // All modules. - $return = []; - $nodes = []; - foreach ($agents as $tserver => $id_agents) { - try { - $nodes[$tserver] = new Node($tserver); - } catch (Exception $e) { - hd($e); - } - - $return = array_reduce( - $modules[$tserver], - function ($carry, $item) use ($tserver, $nodes) { - $t = []; - foreach ($item as $k => $v) { - $t[$k] = $v; - } - - $t['id_node'] = $tserver; - if ($nodes[$tserver] !== null) { - $t['nombre'] = io_safe_output( - $nodes[$tserver]->server_name().' » '.$t['nombre'] - ); - } - - $carry[] = $t; - return $carry; - }, - $return - ); - } - - $modules = $return; - } - - echo json_encode($modules); - } else { - $modules = select_modules_for_agent_group($id_group, $id_agents, $selection, false); - echo json_encode($modules); - } + $modules = get_modules_agents( + $id_group, + $id_agents, + $selection, + $select_mode + ); + echo json_encode($modules); + return; } if ($filter_modules_group_json) {