From b8bd4b5d7caeccb50dc0b64ba5a7505503f03d27 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Fri, 21 Sep 2012 13:42:13 +0000 Subject: [PATCH] 2012-09-21 Ramon Novoa * include/functions_netflow.php, operation/netflow/nf_view.php, godmode/netflow/nf_item_list.php: Added netflow traffic summary reports. Small performance improvements. Fixed a bug in the scale of area charts. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6994 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 8 ++ .../godmode/netflow/nf_item_list.php | 4 +- pandora_console/include/functions_netflow.php | 135 ++++++++++++++++-- pandora_console/operation/netflow/nf_view.php | 7 +- 4 files changed, 134 insertions(+), 20 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 73f4627eb7..c7030a3c77 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,11 @@ +2012-09-21 Ramon Novoa + + * include/functions_netflow.php, + operation/netflow/nf_view.php, + godmode/netflow/nf_item_list.php: Added netflow traffic summary + reports. Small performance improvements. Fixed a bug in the + scale of area charts. + 2012-09-21 Miguel de Dios * operation/agentes/estado_monitores.php: added the small form to diff --git a/pandora_console/godmode/netflow/nf_item_list.php b/pandora_console/godmode/netflow/nf_item_list.php index 5f234ec8c3..a6a9ec533a 100644 --- a/pandora_console/godmode/netflow/nf_item_list.php +++ b/pandora_console/godmode/netflow/nf_item_list.php @@ -138,7 +138,6 @@ $table->head[0] = __('Order'); $table->head[1] = __('Filter'); $table->head[2] = __('Max. values'); $table->head[3] = __('Chart type'); -$table->head[3] = __('Chart type'); $table->head[4] = __('Action') . html_print_checkbox('all_delete', 0, false, true, false, 'check_all_checkboxes();'); $table->style = array (); @@ -209,6 +208,9 @@ foreach ($reports_item as $item) { case 3: $data[3] = 'Statistics table'; break; + case 4: + $data[3] = 'Summary table'; + break; } $data[4] = "size = array ('50%'); + $table->class = 'databox'; + $table->data = array(); + + $table->data[0][0] = ''.__('Total flows').''; + $table->data[0][1] = format_numeric ($data['totalflows']); + $table->data[1][0] = ''.__('Total megabytes').''; + $table->data[1][1] = format_numeric ((int)($data['totalbytes'] / 1048576)); + $table->data[2][0] = ''.__('Total packets').''; + $table->data[2][1] = format_numeric ($data['totalpackets']); + $table->data[3][0] = ''.__('Average bits per second'). ''; + $table->data[3][1] = format_numeric ($data['avgbps']); + $table->data[4][0] = ''.__('Average packets per second').''; + $table->data[4][1] = format_numeric ($data['avgpps']); + $table->data[5][0] = ''.__('Average bytes per packet').''; + $table->data[5][1] = format_numeric ($data['avgbpp']); + + + html_print_table($table); +} + /** * Returns 1 if the given address is a network address. * @@ -325,6 +368,9 @@ function netflow_get_data ($start_date, $end_date, $command, $unique_id, $aggreg global $nfdump_date_format; global $config; + // Suppress the header line and the statistics at the bottom + $command .= ' -q'; + // If there is aggregation calculate the top n if ($aggregate != 'none') { $values['data'] = array (); @@ -357,11 +403,11 @@ function netflow_get_data ($start_date, $end_date, $command, $unique_id, $aggreg $temp_file = $config['attachment_store'] . '/netflow_' . $unique_id . '.tmp'; $command .= ' -t '.date($nfdump_date_format, $last_timestamp).'-'.date($nfdump_date_format, $end_date); exec("$command > $temp_file"); - + // Parse data file // We must parse from $start_date to avoid creating new intervals! netflow_parse_file ($start_date, $end_date, $temp_file, $values, $aggregate, $unit); - + unlink ($temp_file); } @@ -385,10 +431,10 @@ function netflow_get_data ($start_date, $end_date, $command, $unique_id, $aggreg * * @return An array with netflow stats. */ -function netflow_get_stats ($start_date, $end_date, $command, $aggregate, $max, $unit){ +function netflow_get_stats ($start_date, $end_date, $command, $aggregate, $max, $unit) { global $nfdump_date_format; - $command .= " -s $aggregate/$unit -n $max -t " .date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date); + $command .= " -q -s $aggregate/$unit -n $max -t " .date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date); exec($command, $string); if (! is_array($string)) { @@ -435,6 +481,34 @@ function netflow_get_stats ($start_date, $end_date, $command, $aggregate, $max, return $values; } +/** + * Returns a traffic summary for the given period in an array. + * + * @param string start_date Period start date. + * @param string end_date Period end date. + * @param string command Command used to retrieve netflow data. + * @param string unique_id A unique number that is used to generate a cache file. + * + * @return An array with netflow stats. + */ +function netflow_get_summary ($start_date, $end_date, $command, $unique_id) { + global $nfdump_date_format; + global $config; + + // Execute nfdump and save its output in a temporary file + $temp_file = $config['attachment_store'] . '/netflow_' . $unique_id . '.tmp'; + $command .= " -o \"fmt: \" -t " .date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date); + exec("$command > $temp_file"); + + // Parse data file + // We must parse from $start_date to avoid creating new intervals! + $values = array (); + netflow_parse_file ($start_date, $end_date, $temp_file, $values); + unlink ($temp_file); + + return $values; +} + /** * Returns the command needed to run nfdump for the given filter. * @@ -447,7 +521,7 @@ function netflow_get_command ($filter) { global $config; // Build command - $command = 'nfdump -q -N -m'; + $command = 'nfdump -N -m'; // Netflow data path if (isset($config['netflow_path']) && $config['netflow_path'] != '') { @@ -570,7 +644,7 @@ function netflow_get_filter_arguments ($filter) { * @return Timestamp of the last data read. * */ -function netflow_parse_file ($start_date, $end_date, $file, &$values, $aggregate, $unit) { +function netflow_parse_file ($start_date, $end_date, $file, &$values, $aggregate = '', $unit = '') { global $config; // Last timestamp read @@ -587,6 +661,32 @@ function netflow_parse_file ($start_date, $end_date, $file, &$values, $aggregate $period = $end_date - $start_date; $interval_length = (int) ($period / $num_intervals); + // Parse the summary and exit + if ($aggregate == '' && $unit == '') { + while ($line = fgets ($fh)) { + $line = preg_replace('/\s+/','',$line); + $idx = strpos ($line, 'Summary:'); + if ($idx === FALSE) { + continue; + } + + // Parse summary items + $idx += strlen ('Summary:'); + $line = substr ($line, $idx); + $summary = explode(',', $line); + foreach ($summary as $item) { + $val = explode (':', $item); + if (! isset ($val[1])) { + continue; + } + $values[$val[0]] = $val[1]; + } + } + + fclose ($fh); + return; + } + // Parse flow data $read_flag = 1; $flow = array (); @@ -694,11 +794,11 @@ function netflow_parse_file ($start_date, $end_date, $file, &$values, $aggregate } // Calculate interval data - $values['data'][$timestamp][$agg] = (int) ($interval_total[$agg] / $interval_count[$agg]); + $values['data'][$timestamp][$agg] = (int) $interval_total[$agg]; - // Average with previous data + // Add previous data if ($previous_value != 0) { - $values['data'][$timestamp][$agg] = (int) (($values['data'][$timestamp][$agg] + $previous_data) / 2); + $values['data'][$timestamp][$agg] = (int) ($values['data'][$timestamp][$agg] + $previous_data); } } } @@ -718,11 +818,11 @@ function netflow_parse_file ($start_date, $end_date, $file, &$values, $aggregate } // Calculate interval data - $values[$timestamp]['data'] = (int) ($interval_total / $interval_count); + $values[$timestamp]['data'] = (int) $interval_total; - // Average with previous data + // Add previous data if ($previous_value != 0) { - $values[$timestamp]['data'] = (int) (($values[$timestamp]['data'] + $previous_value) / 2); + $values[$timestamp]['data'] = (int) ($values[$timestamp]['data'] + $previous_value); } @@ -829,7 +929,8 @@ function netflow_get_chart_types () { __('Area graph'), __('Pie graph'), __('Data table'), - __('Statistics table')); + __('Statistics table'), + __('Summary table')); } /** @@ -903,6 +1004,10 @@ function netflow_draw_item ($start_date, $end_date, $type, $filter, $command, $f $data = netflow_get_stats ($start_date, $end_date, $command, $aggregate, $max_aggregates, $unit); echo netflow_stat_table ($data, $start_date, $end_date, $aggregate, $unit); break; + case '4': + $data = netflow_get_summary ($start_date, $end_date, $command, $unique_id); + netflow_summary_table ($data); + break; default: echo fs_error_image(); break; diff --git a/pandora_console/operation/netflow/nf_view.php b/pandora_console/operation/netflow/nf_view.php index ed3f4f3438..800dccc9a4 100644 --- a/pandora_console/operation/netflow/nf_view.php +++ b/pandora_console/operation/netflow/nf_view.php @@ -99,18 +99,17 @@ if (empty ($id)){ $report_name = db_get_value('id_name', 'tnetflow_report', 'id_report', $id); echo"

$report_name

"; -$all_rcs = db_get_all_rows_sql("SELECT id_rc FROM tnetflow_report_content WHERE id_report='$id'"); -if (empty ($all_rcs)) { +$report_contents = db_get_all_rows_sql("SELECT * FROM tnetflow_report_content WHERE id_report='$id' ORDER BY `order`"); +if (empty ($report_contents)) { echo fs_error_image(); return; } // Process report items -for ($x = 0; isset($all_rcs[$x]['id_rc']); $x++) { +foreach ($report_contents as $content_report) { // Get report item $report_id = $all_rcs[$x]['id_rc']; - $content_report = db_get_row_sql("SELECT * FROM tnetflow_report_content WHERE id_rc='$report_id'"); $content_id = $content_report['id_rc']; $max_aggregates= $content_report['max']; $type = $content_report['show_graph'];