From 8a336d6cfeaaa4c92cfd1d0128a3ac10f101b0d3 Mon Sep 17 00:00:00 2001 From: Fermin Date: Wed, 13 Mar 2019 18:55:16 +0100 Subject: [PATCH] [Netflow live] Implemented resolution on area graphs and fixed end date Former-commit-id: 86d55ac7645e62fcae6a07cbf1bd50f92d5d37a1 --- pandora_console/include/functions_graph.php | 3 +- pandora_console/include/functions_netflow.php | 104 +++++++++--------- .../operation/netflow/nf_live_view.php | 21 +++- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php index 814ad6d1c8..4d9ae9eca6 100644 --- a/pandora_console/include/functions_graph.php +++ b/pandora_console/include/functions_graph.php @@ -4109,7 +4109,7 @@ function fullscale_data( /** * Print an area graph with netflow aggregated */ -function graph_netflow_aggregate_area($data, $period, $width, $height, $unit='', $ttl=1, $only_image=false) +function graph_netflow_aggregate_area($data, $period, $width, $height, $unit='', $ttl=1, $only_image=false, $date=null) { global $config; global $graphic_type; @@ -4165,6 +4165,7 @@ function graph_netflow_aggregate_area($data, $period, $width, $height, $unit='', 'font_size' => $config['font_size'], 'array_data_create' => $chart, 'stacked' => 1, + 'date' => $date, ]; return grafico_modulo_sparse($params); diff --git a/pandora_console/include/functions_netflow.php b/pandora_console/include/functions_netflow.php index 747a4ab17e..6bb23ecc72 100644 --- a/pandora_console/include/functions_netflow.php +++ b/pandora_console/include/functions_netflow.php @@ -17,6 +17,13 @@ require_once $config['homedir'].'/include/functions_io.php'; enterprise_include_once($config['homedir'].'/enterprise/include/pdf_translator.php'); enterprise_include_once($config['homedir'].'/enterprise/include/functions_metaconsole.php'); +define('NETFLOW_RES_LOWD', 6); +define('NETFLOW_RES_MEDD', 12); +define('NETFLOW_RES_HID', 24); +define('NETFLOW_RES_ULTRAD', 30); +define('NETFLOW_RES_HOURLY', 'hourly'); +define('NETFLOW_RES_DAILY', 'daily'); + // Date format for nfdump global $nfdump_date_format; $nfdump_date_format = 'Y/m/d.H:i:s'; @@ -461,21 +468,41 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag return json_decode($data, true); } - // Calculate the number of intervals - if ($interval_length <= 0) { - // $num_intervals = $config['graph_res'] * 50; - $num_intervals = 250; - $period = ($end_date - $start_date); - $interval_length = (int) ($period / $num_intervals); - } else { - $period = ($end_date - $start_date); - $num_intervals = (int) ($period / $interval_length); + if ($start_date > $end_date) { + return []; } - // Set a max number of intervals - if ($num_intervals > $config['netflow_max_resolution']) { - $num_intervals = $config['netflow_max_resolution']; - $interval_length = (int) ($period / $num_intervals); + // Calculate the number of intervals. + $multiplier_time = ($end_date - $start_date); + switch ($interval_length) { + case NETFLOW_RES_LOWD: + case NETFLOW_RES_MEDD: + case NETFLOW_RES_HID: + case NETFLOW_RES_ULTRAD: + $multiplier_time = ceil(($end_date - $start_date) / $interval_length); + break; + + case NETFLOW_RES_HOURLY: + $multiplier_time = SECONDS_1HOUR; + break; + + case NETFLOW_RES_DAILY: + $multiplier_time = SECONDS_1DAY; + break; + + default: + $multiplier_time = ($end_date - $start_date); + break; + } + + // Put all points into an array. + $intervals = [($start_date - $multiplier_time)]; + while ((end($intervals) < $end_date) === true) { + $intervals[] = (end($intervals) + $multiplier_time); + } + + if (end($intervals) != $end_date) { + $intervals[] = $end_date; } // If there is aggregation calculate the top n @@ -569,14 +596,15 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag $values['sources'] = $sources; } - // Address resolution end - $interval_start = $start_date; - for ($i = 0; $i < $num_intervals; $i++, $interval_start += ($interval_length + 1)) { - $interval_end = ($interval_start + $interval_length); - if ($interval_end > $end_date) { - $interval_end = $end_date; + // Address resolution end. + foreach ($intervals as $k => $time) { + $interval_start = $time; + if (!isset($intervals[($k + 1)])) { + continue; } + $interval_end = $intervals[($k + 1)]; + if ($aggregate == 'none') { $data = netflow_get_summary($interval_start, $interval_end, $filter, $connection_name); if (! isset($data['totalbytes'])) { @@ -608,7 +636,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag } else { // Set default values foreach ($values['sources'] as $source => $discard) { - $values['data'][$interval_start][$source] = 0; + $values['data'][$interval_end][$source] = 0; } $data = netflow_get_stats( @@ -654,7 +682,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag continue; } - $values['data'][$interval_start][$line['agg']] = $line['data']; + $values['data'][$interval_end][$line['agg']] = $line['data']; } } } @@ -1144,36 +1172,6 @@ function netflow_get_valid_intervals() } -/** - * Gets valid intervals for a netflow chart in the format: - * - * interval_length => interval_description - * - * @return array of valid intervals. - */ -function netflow_get_valid_subintervals() -{ - return [ - (string) SECONDS_1MINUTE => __('1 min'), - (string) SECONDS_2MINUTES => __('2 mins'), - (string) SECONDS_5MINUTES => __('5 mins'), - (string) SECONDS_10MINUTES => __('10 mins'), - (string) SECONDS_15MINUTES => __('15 mins'), - (string) SECONDS_30MINUTES => __('30 mins'), - (string) SECONDS_1HOUR => __('1 hour'), - (string) SECONDS_2HOUR => __('2 hours'), - (string) SECONDS_5HOUR => __('5 hours'), - (string) SECONDS_12HOURS => __('12 hours'), - (string) SECONDS_1DAY => __('1 day'), - (string) SECONDS_2DAY => __('2 days'), - (string) SECONDS_5DAY => __('5 days'), - (string) SECONDS_15DAYS => __('15 days'), - (string) SECONDS_1WEEK => __('1 week'), - (string) SECONDS_1MONTH => __('1 month'), - ]; -} - - /** * Draw a netflow report item. * @@ -1217,7 +1215,7 @@ function netflow_draw_item($start_date, $end_date, $interval_length, $type, $fil $html .= ' '._('Resolution').": $interval_length ".__('seconds'); } - $html .= graph_netflow_aggregate_area($data, $interval, $width, $height, netflow_format_unit($unit)); + $html .= graph_netflow_aggregate_area($data, $interval, $width, $height, netflow_format_unit($unit), 1, false, $end_date); return $html; } else if ($output == 'PDF') { $html = ''.__('Unit').': '.netflow_format_unit($unit); @@ -1226,7 +1224,7 @@ function netflow_draw_item($start_date, $end_date, $interval_length, $type, $fil $html .= ' '._('Resolution').": $interval_length ".__('seconds'); } - $html .= graph_netflow_aggregate_area($data, $interval, $width, $height, netflow_format_unit($unit), 2, true); + $html .= graph_netflow_aggregate_area($data, $interval, $width, $height, netflow_format_unit($unit), 2, true, $end_date); return $html; } else if ($output == 'XML') { $xml = "$unit\n"; diff --git a/pandora_console/operation/netflow/nf_live_view.php b/pandora_console/operation/netflow/nf_live_view.php index 707b6f59a4..f8c27b5954 100644 --- a/pandora_console/operation/netflow/nf_live_view.php +++ b/pandora_console/operation/netflow/nf_live_view.php @@ -96,7 +96,7 @@ $update_date = (int) get_parameter('update_date', 0); $date = get_parameter_post('date', date(DATE_FORMAT, get_system_time())); $time = get_parameter_post('time', date(TIME_FORMAT, get_system_time())); $connection_name = get_parameter('connection_name', ''); -$interval_length = (int) get_parameter('interval_length', 300); +$interval_length = get_parameter('interval_length', NETFLOW_RES_MEDD); $address_resolution = (int) get_parameter('address_resolution', $config['netflow_get_ip_hostname']); $filter_selected = (int) get_parameter('filter_selected', 0); @@ -260,7 +260,24 @@ if (is_metaconsole()) { echo ''.html_print_select(netflow_get_valid_intervals(), 'period', $period, '', '', 0, true, false, false).''; echo ''.__('Resolution').ui_print_help_tip(__('The interval will be divided in chunks the length of the resolution.'), true).''; - echo ''.html_print_select(netflow_get_valid_subintervals(), 'interval_length', $interval_length, '', '', 0, true, false, false).''; + echo ''.html_print_select( + [ + NETFLOW_RES_LOWD => __('Low'), + NETFLOW_RES_MEDD => __('Medium'), + NETFLOW_RES_HID => __('High'), + NETFLOW_RES_ULTRAD => __('Ultra High'), + NETFLOW_RES_HOURLY => __('Hourly'), + NETFLOW_RES_DAILY => __('Daily'), + ], + 'interval_length', + $interval_length, + '', + '', + 0, + true, + false, + false + ).''; echo ''; echo '';