From 6a1bf2fbce5261804344112f100ed6e6ad6deb90 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Mon, 25 Mar 2019 11:34:03 +0100 Subject: [PATCH] Added first version of live map Former-commit-id: c440a0d0785d83c17560438986b7b394223e4779 --- pandora_console/include/functions_netflow.php | 99 +++++++++ .../operation/network/network_usage_map.php | 208 ++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 pandora_console/operation/network/network_usage_map.php diff --git a/pandora_console/include/functions_netflow.php b/pandora_console/include/functions_netflow.php index 20176ac074..65b062068e 100644 --- a/pandora_console/include/functions_netflow.php +++ b/pandora_console/include/functions_netflow.php @@ -23,6 +23,7 @@ require_once $config['homedir'].'/include/functions_users.php'; require_once $config['homedir'].'/include/functions_io.php'; require_once $config['homedir'].'/include/functions_io.php'; require_once $config['homedir'].'/include/functions_network.php'; +require_once $config['homedir'].'/include/class/NetworkMap.class.php'; enterprise_include_once( $config['homedir'].'/enterprise/include/pdf_translator.php' ); @@ -1778,3 +1779,101 @@ function netflow_aggregate_is_ip($aggregate) { return in_array($aggregate, ['dstip', 'srcip']); } + + +/** + * Build netflow data structure to network map. + * + * @param integer $start_date Time in timestamp format. + * @param integer $end_date Time in timestamp format. + * @param integer $top Max data to show. + * + * @return array With map structure. + */ +function netflow_build_map_data($start_date, $end_date, $top) +{ + $data = netflow_get_relationships_raw_data( + $start_date, + $end_date, + [ + 'id_name' => '', + 'id_group' => 0, + 'aggregate' => 'srcip', + 'id_dst' => '', + 'ip_src' => '', + 'dst_port' => '', + 'src_port' => '', + 'advanced_filter' => '', + 'router_ip' => '', + ], + $top, + 'srcip' + ); + + $nodes = array_map( + function ($elem) { + return [ + 'name' => $elem, + 'type' => NODE_GENERIC, + 'width' => 20, + 'height' => 20, + 'status' => '#82B92E', + ]; + }, + $data['sources'] + ); + + $relations = []; + + $inverse_nodes = array_flip($data['sources']); + + // Port are situated in a different places from addreses. + $is_ip = true; + $src_key = ($is_ip === true) ? 3 : 5; + $dst_key = ($is_ip === true) ? 4 : 6; + + foreach ($data['lines'] as $line) { + if (empty($line) === true) { + continue; + } + + // Parse the line. + $items = explode(',', $line); + + // Get the required data. + $src_item = $inverse_nodes[$items[$src_key]]; + $dst_item = $inverse_nodes[$items[$dst_key]]; + $value = $items[12]; + $index_rel = $src_item.'-'.$dst_item; + + // Check if valid data. + if (!isset($value) || !isset($src_item) || !isset($dst_item)) { + continue; + } + + if (isset($relations[$index_rel])) { + $relations[$index_rel]['text_start'] += $value; + } else { + // Update the value. + $relations[$index_rel] = [ + 'id_parent' => $src_item, + 'parent_type' => NODE_GENERIC, + 'child_type' => NODE_GENERIC, + 'id_child' => $dst_item, + 'link_color' => '#82B92E', + 'text_start' => $value, + ]; + } + } + + return [ + 'nodes' => $nodes, + 'relations' => $relations, + 'pure' => 1, + 'no_pandora_node' => 1, + 'map_options' => [ + 'generation_method' => LAYOUT_SPRING1, + 'map_filter' => ['node_radius' => 40], + ], + ]; +} diff --git a/pandora_console/operation/network/network_usage_map.php b/pandora_console/operation/network/network_usage_map.php new file mode 100644 index 0000000000..e3cc5f7fca --- /dev/null +++ b/pandora_console/operation/network/network_usage_map.php @@ -0,0 +1,208 @@ + + + +class = 'databox'; +$table->styleTable = 'width: 100%'; + +$table->data['0']['0'] = '
'; +$table->data['0']['0'] .= '
'; +$table->data['0']['0'] .= __('Start date').'  '; +$table->data['0']['0'] .= html_print_input_text('date_lower', $date_lower, '', 10, 7, true); +$table->data['0']['0'] .= '  '; +$table->data['0']['0'] .= html_print_input_text('time_lower', $time_lower, '', 7, 8, true); +$table->data['0']['0'] .= '
'; + +$table->data['0']['0'] .= '
'; +$table->data['0']['0'] .= __('Time Period').'  '; +$table->data['0']['0'] .= html_print_extended_select_for_time('period', $period, '', '', 0, false, true); +$table->data['0']['0'] .= '
'; +$table->data['0']['0'] .= html_print_checkbox( + 'is_period', + 1, + ($is_period === true) ? 1 : 0, + true, + false, + 'network_report_click_period(event)' +); +$table->data['0']['0'] .= '
'; + +$table->data['0']['1'] = __('End date').'  '; +$table->data['0']['1'] .= html_print_input_text('date_greater', $date_greater, '', 10, 7, true); +$table->data['0']['1'] .= '  '; +$table->data['0']['1'] .= html_print_input_text('time_greater', $time_greater, '', 7, 8, true); + +$table->data['0']['2'] = __('Number of result to show').'  '; +$table->data['0']['2'] .= html_print_select( + [ + '5' => 5, + '10' => 10, + '15' => 15, + '20' => 20, + '25' => 25, + '50' => 50, + '100' => 100, + '250' => 250, + ], + 'top', + $top, + '', + '', + 0, + true +); + +$table->data['1']['0'] = ''; +$table->data['1']['1'] = ''; + +$netflow_button = ''; +if ((bool) $config['activate_netflow'] === true) { + $netflow_button = html_print_submit_button( + __('Show netflow map'), + 'update_netflow', + false, + 'class="sub upd"', + true + ); +} + +$nta_button = ''; +if ((bool) $config['activate_nta'] === true) { + $nta_button = html_print_submit_button( + __('Show NTA map'), + 'update_nta', + false, + 'class="sub upd"', + true + ); +} + +$table->data['1']['2'] .= implode( + '  ', + [ + $netflow_button, + $nta_button, + ] +); + +echo '
'; +html_print_input_hidden('order_by', $order_by); + +html_print_table($table); +echo '
'; + +$has_data = true; +if ((bool) get_parameter('update_netflow') === true) { + $map_data = netflow_build_map_data( + $utimestamp_lower, + $utimestamp_greater, + $top + ); + $has_data = !empty($map_data['nodes']); +} else if ((bool) get_parameter('update_nta') === true) { + // TODOS. + $has_data = false; +} else { + return; +} + +if ($has_data === true) { + $map_manager = new NetworkMap($map_data); + $map_manager->printMap(); +} else { + ui_print_info_message(__('No data retrieved')); +} +