diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index 9726d8fcdd..41b50df748 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -78,6 +78,14 @@ $agents_inventory_display_options['estado'] = __('Status'); $agents_inventory_display_options['agent_version'] = __('Version'); $agents_inventory_display_options['remote'] = __('Remote configuration'); +// Modules inventory display options. +$modules_inventory_display_options = []; +$modules_inventory_display_options['alias'] = __('Name'); +$modules_inventory_display_options['direccion'] = __('Description'); +$modules_inventory_display_options['id_os'] = __('Tags'); +$modules_inventory_display_options['id_grupo'] = __('Module groups'); +$modules_inventory_display_options['secondary_groups'] = __('Group'); + enterprise_include('/godmode/reporting/reporting_builder.item_editor.php'); require_once $config['homedir'].'/include/functions_agents.php'; if (enterprise_include_once('include/functions_metaconsole.php')) { @@ -901,6 +909,14 @@ switch ($action) { $idAgentModule = $inventory_modules; break; + case 'modules_inventory': + $description = $item['description']; + $es = json_decode($item['external_source'], true); + + $selected_agent_group_filter = $es['agent_group_filter']; + $selected_module_group = $es['module_group']; + break; + case 'inventory': $description = $item['description']; $es = json_decode($item['external_source'], true); @@ -3644,7 +3660,22 @@ $class = 'databox filters'; true, 'agent_group_filter', $selected_agent_group_filter, - '' + '', + '', + 0, + false, + false, + false, + '', + false, + false, + false, + false, + 'id_grupo', + false, + false, + false, + 120 ); ?> @@ -3820,6 +3851,47 @@ $class = 'databox filters'; + + + + + + + + @@ -6139,6 +6211,8 @@ function chooseType() { $("#row_agents_inventory_display_options").hide(); $("#row_agent_server_filter").hide(); $("#row_agent_group_filter").hide(); + $("#row_module_group_filter").hide(); + $("#row_module_group_filter").hide(); $("#row_os").hide(); $("#row_custom_field_filter").hide(); $("#row_custom_field").hide(); @@ -6768,6 +6842,14 @@ function chooseType() { break; + case 'modules_inventory': + $("#row_group").show(); + $("#row_agent_server_filter").show(); + $("#row_agent_group_filter").show(); + $("#row_module_group_filter").show(); + + break; + case 'inventory': $("#row_description").show(); $("#row_group").show(); diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php index 2b03c1a771..24575eb857 100755 --- a/pandora_console/godmode/reporting/reporting_builder.php +++ b/pandora_console/godmode/reporting/reporting_builder.php @@ -2313,6 +2313,14 @@ switch ($action) { $values['external_source'] = json_encode($es); break; + case 'modules_inventory': + $es['agent_server_filter'] = get_parameter('agent_server_filter'); + $es['module_group'] = get_parameter('module_group'); + $es['agent_group_filter'] = get_parameter('agent_group_filter'); + + $values['external_source'] = json_encode($es); + break; + case 'IPAM_network': $es['network_filter'] = get_parameter('network_filter'); $es['alive_ip'] = get_parameter('alive_ip'); @@ -3081,6 +3089,14 @@ switch ($action) { $values['external_source'] = json_encode($es); break; + case 'modules_inventory': + $es['agent_server_filter'] = get_parameter('agent_server_filter'); + $es['module_group'] = get_parameter('module_group'); + $es['agent_group_filter'] = get_parameter('agent_group_filter'); + + $values['external_source'] = json_encode($es); + break; + case 'IPAM_network': $es['network_filter'] = get_parameter('network_filter'); $es['alive_ip'] = get_parameter('alive_ip'); diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index c52b2bf5eb..60061b9f31 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -774,6 +774,13 @@ function reporting_make_reporting_data( ); break; + case 'modules_inventory': + $report['contents'][] = reporting_modules_inventory( + $report, + $content + ); + break; + case 'inventory': $report['contents'][] = reporting_inventory( $report, @@ -2438,6 +2445,14 @@ function reporting_event_report_module( } +/** + * Generate agents inventory report. + * + * @param array $report Report info. + * @param array $content Content info. + * + * @return array + */ function reporting_agents_inventory($report, $content) { global $config; @@ -2470,7 +2485,7 @@ function reporting_agents_inventory($report, $content) $custom_field_sql = ''; $search_sql = ''; - if (!empty($es_agent_custom_fields)) { + if (empty(array_filter($es_agent_custom_fields)) === false) { $custom_field_sql = 'INNER JOIN tagent_custom_data tacd ON tacd.id_agent = tagente.id_agente'; if ($es_agent_custom_fields[0] != 0) { $custom_field_sql .= ' AND tacd.id_field IN ('.implode(',', $es_agent_custom_fields).')'; @@ -2629,6 +2644,146 @@ function reporting_agents_inventory($report, $content) } +/** + * Generate modules inventory report. + * + * @param array $report Report info. + * @param array $content Content info. + * + * @return array + */ +function reporting_modules_inventory($report, $content) +{ + global $config; + + $return['name'] = $content['name']; + $return['type'] = 'modules_inventory'; + $return['title'] = $content['name']; + $return['landscape'] = $content['landscape']; + $return['pagebreak'] = $content['pagebreak']; + $return['description'] = $content['description']; + $return['date'] = reporting_get_date_text($report, $content); + + $external_source = io_safe_input(json_decode($content['external_source'], true)); + $es_agent_group_filter = $external_source['agent_group_filter']; + $module_group = $external_source['module_group']; + $es_agent_server_filter = $external_source['agent_server_filter']; + + $search_sql = ''; + + if (empty($es_agent_group_filter) === false) { + $search_sql .= ' AND (ta.id_grupo = '.$es_agent_group_filter.' OR tasg.id_group = '.$es_agent_group_filter.')'; + } + + if (empty($module_group) === false && $module_group > -1) { + $search_sql .= ' AND tam.id_module_group = '.$module_group; + } + + $user_groups_to_sql = ''; + + $user_groupsAR = users_get_groups($config['id_user'], 'AR'); + + $user_groups = $user_groupsAR; + $user_groups_to_sql = implode(',', array_keys($user_groups)); + + $sql = sprintf( + 'SELECT tam.id_agente_modulo, + tam.nombre, + tam.descripcion, + tam.id_module_group, + ttm.id_tag, + ta.id_grupo AS group_id, + tasg.id_group AS sec_group_id + FROM tagente_modulo tam + INNER JOIN tagente ta + ON tam.id_agente = ta.id_agente + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + LEFT JOIN ttag_module ttm + ON ttm.id_agente_modulo = tam.id_agente_modulo + WHERE (ta.id_grupo IN (%s) OR tasg.id_group IN (%s)) %s', + $user_groups_to_sql, + $user_groups_to_sql, + $search_sql + ); + + if (is_metaconsole()) { + $servers_ids = array_column(metaconsole_get_servers(), 'id'); + } else { + $servers_ids = [0]; + } + + $return_data = []; + + foreach ($servers_ids as $server_id) { + if (is_metaconsole()) { + $server = metaconsole_get_connection_by_id($server_id); + + if ((int) $es_agent_server_filter !== 0 + && (int) $es_agent_server_filter !== (int) $server_id + ) { + continue; + } + + metaconsole_connect($server); + } + + $agents = db_get_all_rows_sql($sql); + + $return_data[$server_id] = $agents; + + if (is_metaconsole()) { + metaconsole_restore_db(); + } + } + + $all_data = []; + + foreach ($return_data as $server_agents) { + foreach ($server_agents as $agent) { + $all_data[] = $agent; + } + } + + $return['data'] = $all_data; + + $module_row_data = []; + + foreach ($return['data'] as $row) { + if (is_array($module_row_data[$row['id_agente_modulo']]) === false) { + $module_row_data[$row['id_agente_modulo']]['nombre'] = $row['nombre']; + $module_row_data[$row['id_agente_modulo']]['descripcion'] = $row['descripcion']; + $module_row_data[$row['id_agente_modulo']]['id_module_group'] = $row['id_module_group']; + $module_row_data[$row['id_agente_modulo']]['id_tag'] = []; + $module_row_data[$row['id_agente_modulo']]['group_id'] = []; + $module_row_data[$row['id_agente_modulo']]['sec_group_id'] = []; + } + + if (in_array($row['id_tag'], $module_row_data[$row['id_agente_modulo']]['id_tag']) === false + && $row['id_tag'] !== null + ) { + $module_row_data[$row['id_agente_modulo']]['id_tag'][] = $row['id_tag']; + } + + if (in_array($row['group_id'], $module_row_data[$row['id_agente_modulo']]['group_id']) === false + && $row['group_id'] !== null + ) { + $module_row_data[$row['id_agente_modulo']]['group_id'][] = $row['group_id']; + } + + if (in_array($row['sec_group_id'], $module_row_data[$row['id_agente_modulo']]['sec_group_id']) === false + && $row['sec_group_id'] !== null + ) { + $module_row_data[$row['id_agente_modulo']]['sec_group_id'][] = $row['sec_group_id']; + } + } + + $return['data'] = $module_row_data; + + return reporting_check_structure_content($return); +} + + function reporting_inventory_changes($report, $content, $type) { global $config; diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index 7cce81cd4d..69d85d448d 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -394,6 +394,10 @@ function reporting_html_print_report($report, $mini=false, $report_info=1) reporting_html_agents_inventory($table, $item); break; + case 'modules_inventory': + reporting_html_modules_inventory($table, $item); + break; + case 'inventory': reporting_html_inventory($table, $item); break; @@ -1677,6 +1681,116 @@ function reporting_html_agents_inventory($table, $item, $pdf=0) } +/** + * Print html modules inventory + * + * @param object $table Head table or false if it comes from pdf. + * @param array $item Items data. + * @param boolean $pdf Print pdf true or false. + * + * @return string HTML code. + */ +function reporting_html_modules_inventory($table, $item, $pdf=0) +{ + global $config; + + $table1 = new stdClass(); + $table1->width = '100%'; + + $table1->style[0] = 'text-align: left;vertical-align: top;min-width: 100px;'; + $table1->class = 'databox data'; + $table1->cellpadding = 1; + $table1->cellspacing = 1; + $table1->styleTable = 'overflow: wrap; table-layout: fixed;'; + + $table1->style[0] = 'text-align: left;vertical-align: top;min-width: 100px;'; + $table1->style[1] = 'text-align: left;vertical-align: top;min-width: 100px;'; + $table1->style[2] = 'text-align: left;vertical-align: top; min-width: 100px'; + $table1->style[3] = 'text-align: left;vertical-align: top;min-width: 100px;'; + $table1->style[4] = 'text-align: left;vertical-align: top;min-width: 100px;'; + $table1->style[5] = 'text-align: left;vertical-align: top; min-width: 100px'; + + $table1->head = []; + + $table1->head[] = __('Name'); + $table1->head[] = __('Description'); + $table1->head[] = __('Module group'); + $table1->head[] = __('Tags'); + $table1->head[] = __('Agent group'); + $table1->head[] = __('Agent secondary groups'); + + $table1->headstyle[0] = 'text-align: left'; + $table1->headstyle[1] = 'text-align: left'; + $table1->headstyle[2] = 'text-align: left'; + $table1->headstyle[3] = 'text-align: left'; + $table1->headstyle[4] = 'text-align: left'; + $table1->headstyle[5] = 'text-align: left'; + + $table1->data = []; + + foreach ($item['data'] as $module_id => $module_data) { + $row = []; + $first_item = array_pop(array_reverse($module_data)); + + foreach ($module_data as $data_field_key => $data_field_value) { + if ($data_field_key === 'nombre') { + $column_value = $data_field_value; + } else if ($data_field_key === 'descripcion') { + $column_value = $data_field_value; + } else if ($data_field_key === 'id_module_group') { + $module_group_name = modules_get_modulegroup_name($data_field_value); + + if ($module_group_name === '') { + $module_group_name = '-'; + } + + $column_value = $module_group_name; + } else if ($data_field_key === 'id_tag') { + $tags_names = array_map( + function ($tag_id) { + return db_get_value('name', 'ttag', 'id_tag', $tag_id); + }, + $data_field_value + ); + $column_value = implode('
', $tags_names); + } else if ($data_field_key === 'group_id') { + $column_value = groups_get_name($data_field_value[0]); + } else if ($data_field_key === 'sec_group_id') { + $sec_groups_names = array_map(function ($group_id) { + return groups_get_name($group_id); + }, + $data_field_value + ); + + $column_value = implode('
', $sec_groups_names); + } + + $row[] = $column_value; + } + + $table1->data[] = $row; + + if ($pdf !== 0) { + $table1->data[] = '
'; + } + } + + if ($pdf === 0) { + $table->colspan['permissions']['cell'] = 3; + $table->cellstyle['permissions']['cell'] = 'text-align: center;'; + $table->data['permissions']['cell'] = html_print_table( + $table1, + true + ); + } else { + return html_print_table( + $table1, + true + ); + } +} + + /** * Print in html inventory changes reports * diff --git a/pandora_console/include/functions_reports.php b/pandora_console/include/functions_reports.php index 9f67a9f83a..d52aa434f4 100755 --- a/pandora_console/include/functions_reports.php +++ b/pandora_console/include/functions_reports.php @@ -880,6 +880,13 @@ function reports_get_report_types($template=false, $not_editor=false) ]; } + if (!$template) { + $types['modules_inventory'] = [ + 'optgroup' => __('Inventory'), + 'name' => __('Modules inventory'), + ]; + } + if ($config['enterprise_installed']) { $types['inventory'] = [ 'optgroup' => __('Inventory'),