From 4380251694421cd08377a13c16081a5c65519932 Mon Sep 17 00:00:00 2001
From: vgilc <vanessa.gil@artica.es>
Date: Wed, 21 Dec 2011 15:28:01 +0000
Subject: [PATCH] 2011-12-21  Vanessa Gil  <vanessa.gil@artica.es> 	*
 pandoradb.sql: added mysql tables for traffic netflow.

git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5291 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |   8 +
 .../godmode/netflow/nf_item_list.php          |   2 +-
 pandora_console/godmode/netflow/nf_report.php |   6 +-
 pandora_console/include/functions_graph.php   |  30 +-
 pandora_console/include/functions_netflow.php | 311 ++++++++++++++++++
 pandora_console/index.php                     |   2 +-
 .../operation/netflow/nf_reporting.php        |   6 +-
 pandora_console/operation/netflow/nf_view.php |   7 +-
 pandora_console/pandoradb.sql                 |  62 ++++
 9 files changed, 420 insertions(+), 14 deletions(-)
 create mode 100644 pandora_console/include/functions_netflow.php

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 0c941d54ce..3a4dec9cf0 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,11 @@
+2011-12-21  Vanessa Gil  <vanessa.gil@artica.es>
+	* pandoradb.sql: added mysql tables for traffic netflow.
+
+2011-12-21  Vanessa Gil  <vanessa.gil@artica.es>
+	* include/functions_netflow.php
+	  include/help/en/help_aggregate_by.php: added files to netflow
+	traffic monitoring.
+
 2011-12-21  Vanessa Gil  <vanessa.gil@artica.es>
 
 	* include/functions_config.php
diff --git a/pandora_console/godmode/netflow/nf_item_list.php b/pandora_console/godmode/netflow/nf_item_list.php
index 6ead5d40e0..39ffb657ec 100644
--- a/pandora_console/godmode/netflow/nf_item_list.php
+++ b/pandora_console/godmode/netflow/nf_item_list.php
@@ -128,7 +128,7 @@ $table->data = array ();
 $total_reports_item = db_get_all_rows_filter ('tnetflow_report_content', false, 'COUNT(*) AS total');
 $total_reports_item = $total_reports_item[0]['total'];
 
-ui_pagination ($total_reports_item, $url);
+//ui_pagination ($total_reports_item, $url);
 
  foreach ($reports_item as $item) {
 
diff --git a/pandora_console/godmode/netflow/nf_report.php b/pandora_console/godmode/netflow/nf_report.php
index 2f51cda508..3394eb6674 100644
--- a/pandora_console/godmode/netflow/nf_report.php
+++ b/pandora_console/godmode/netflow/nf_report.php
@@ -32,7 +32,7 @@ if (! check_acl ($config["id_user"], 0, "IW")) {
 }
 
 //Header
-ui_print_page_header (__('Report Manager'), "images/networkmap/so_cisco_new.png", false, "", true, $buttons);
+ui_print_page_header (__('Report Manager'), "images/networkmap/so_cisco_new.png", false, "", true);
 
 $delete = (bool) get_parameter ('delete');
 $multiple_delete = (bool)get_parameter('multiple_delete', 0);
@@ -85,7 +85,7 @@ $filter['limit'] = (int) $config['block_size'];
 
 $reports = db_get_all_rows_filter ('tnetflow_report', $filter);
 
-if ($options === false)
+if ($reports === false)
 	$filter = array ();
 	
 $table->width = '80%';
@@ -108,7 +108,7 @@ $table->data = array ();
 $total_reports = db_get_all_rows_filter ('tnetflow_report', false, 'COUNT(*) AS total');
 $total_reports = $total_reports[0]['total'];
 
-ui_pagination ($total_reports, $url);
+//ui_pagination ($total_reports, $url);
 
  foreach ($reports as $report) {
 
diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php
index fccb59d31d..b4ae0af347 100755
--- a/pandora_console/include/functions_graph.php
+++ b/pandora_console/include/functions_graph.php
@@ -2002,14 +2002,17 @@ echo"<h4>Gráfica de área</h4>";
 		}
 		$timestamp_short = date($time_format, $date);
 
+/*
 		$long_index[$timestamp_short] = date(
 			html_entity_decode($config['date_format'], ENT_QUOTES, "UTF-8"), $timestamp);
 		$timestamp = $timestamp_short;
+*/
 		/////////////////////////////////////////////////////////////////
 
 	
 ///////////////COMBINED
 	$aggs = array();
+	$ag ='';
 	// Calculate data for each agg
 	$j = 0;
 	for ($i = 0; $i < $resolution; $i++) {
@@ -2068,7 +2071,7 @@ echo"<h4>Gráfica de área</h4>";
 	}
 
 	$color = array();
-
+	$flash_chart = '';
 	return area_graph($flash_chart, $chart, $width, $height, $color, $aggs,
 		$long_index, "images/image_problem.opaque.png", "", "", $homeurl,
 		 $config['homedir'] .  "/images/logo_vertical_water.png",
@@ -2117,9 +2120,6 @@ function grafico_netflow_total_area ($data, $period,$width, $height , $title, $u
 		}
 		$timestamp_short = date($time_format, $date);
 
-		$long_index[$timestamp_short] = date(
-			html_entity_decode($config['date_format'], ENT_QUOTES, "UTF-8"), $timestamp);
-		$timestamp = $timestamp_short;
 		/////////////////////////////////////////////////////////////////
 
 	$aggs = array();
@@ -2143,6 +2143,7 @@ function grafico_netflow_total_area ($data, $period,$width, $height , $title, $u
 						$data[$j]['data'] *= 1024;
 					}
 
+/*
 					if(!isset($chart[$timestamp_short][$ip])) {
 						$chart[$timestamp_short][$ip] = $data[$j]['data'];
 						$count++;
@@ -2150,6 +2151,15 @@ function grafico_netflow_total_area ($data, $period,$width, $height , $title, $u
 						$chart[$timestamp_short][$ip] += $data[$j]['data'];
 						$count++;
 					}
+*/
+
+					if(!isset($chart[$timestamp_short]['data'])) {
+						$chart[$timestamp_short]['data'] = $data[$j]['data'];
+						$count++;
+					} else {
+						$chart[$timestamp_short]['data'] += $data[$j]['data'];
+						$count++;
+					}
 				} else { 
 					break;
 				}
@@ -2157,13 +2167,22 @@ function grafico_netflow_total_area ($data, $period,$width, $height , $title, $u
 			}	
 		
 		// Average
+/*
 		if ($count > 0) {
 			$chart[$timestamp_short][$ip] = $chart[$timestamp_short][$ip]/$count;
 		} else {
 			$chart[$timestamp_short][$ip] = 0;
 		}
+*/
+
+		if ($count > 0) {
+			$chart[$timestamp_short]['data'] = $chart[$timestamp_short]['data']/$count;
+		} else {
+			$chart[$timestamp_short]['data'] = 0;
+		}
 	}
 	
+/*
 	foreach($chart as $key => $value) {
 		foreach($ips as $ip) {
 			if(!isset($chart[$key][$ip])) {
@@ -2171,6 +2190,7 @@ function grafico_netflow_total_area ($data, $period,$width, $height , $title, $u
 			}
 		}
 	}
+*/
 
 //////////FIN COMBINED
 	
@@ -2197,6 +2217,8 @@ function grafico_netflow_aggregate_pie ($data) {
 	echo"<h4>Gráfica totalizada</h4>";
 	
 	$i = 0;
+	$values = array();
+	$agg = array();
 	while (isset ($data[$i])) {
 		$agg = $data[$i]['agg'];
 		if ($data[$i]['unit'] == 'G') {
diff --git a/pandora_console/include/functions_netflow.php b/pandora_console/include/functions_netflow.php
new file mode 100644
index 0000000000..3c62a06182
--- /dev/null
+++ b/pandora_console/include/functions_netflow.php
@@ -0,0 +1,311 @@
+<?php
+
+// Pandora FMS - http://pandorafms.com
+// ==================================================
+// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
+// Please see http://pandorafms.org for full contribution list
+
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the  GNU Lesser General Public License
+// as published by the Free Software Foundation; version 2
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+
+/**
+ * Selects all netflow filters (array (id_name => id_name)) or filters filtered
+ *
+ * @param mixed Array with filter conditions to retrieve filters or false.  
+ *
+ * @return array List of all filters
+ */
+function netflow_get_filters ($filter = false) {
+	if ($filter === false) { 
+		$filters = db_get_all_rows_in_table ("tnetflow_filter", "id_name");
+	}
+	else {
+		$filters = db_get_all_rows_filter ("tnetflow_filter", $filter);
+	}
+	$return = array ();
+	if ($filters === false) {
+		return $return;
+	}
+	foreach ($filters as $filter) {
+		$return[$filter["id_name"]] = $filter["id_name"];
+	}
+	return $return;
+}
+
+
+/**
+ * Selects all netflow reports (array (id_name => id_name)) or filters filtered
+ *
+ * @param mixed Array with filter conditions to retrieve filters or false.  
+ *
+ * @return array List of all filters
+ */
+function netflow_get_reports ($filter = false) {
+	if ($filter === false) { 
+		$filters = db_get_all_rows_in_table ("tnetflow_report", "id_name");
+	}
+	else {
+		$filters = db_get_all_rows_filter ("tnetflow_report", $filter);
+	}
+	$return = array ();
+	if ($filters === false) {
+		return $return;
+	}
+	foreach ($filters as $filter) {
+		$return[$filter["id_name"]] = $filter["id_name"];
+	}
+	return $return;
+}
+
+
+/**
+ * Selects all netflow filters (array (id_sg => id_sg)) or filters filtered
+ *
+ * @param mixed Array with filter conditions to retrieve filters or false.  
+ *
+ * @return array List of all filters
+ */
+/*
+function netflow_get_filters_id ($filter = false) {
+	if ($filter === false) { 
+		$filters = db_get_all_rows_in_table ("tnetflow_filter", "id_sg");
+	}
+	else {
+		$filters = db_get_all_rows_filter ("tnetflow_filter", $filter);
+	}
+	$return = array ();
+	if ($filters === false) {
+		return $return;
+	}
+	foreach ($filters as $filter) {
+		$return[$filter["id_sg"]] = $filter["id_sg"];
+	}
+	return $return;
+}
+*/
+
+/**
+ * Get a filter.
+ *
+ * @param int filter id to be fetched.
+ * @param array Extra filter.
+ * @param array Fields to be fetched.
+ *
+ * @return array A netflow filter matching id and filter.
+ */
+function netflow_filter_get_filter ($id_sg, $filter = false, $fields = false) {
+	if (empty ($id_sg))
+		return false;
+	if (! is_array ($filter))
+		$filter = array ();
+	$filter['id_sg'] = (int) $id_sg;
+	
+	return db_get_row_filter ('tnetflow_filter', $filter, $fields);
+}
+
+/**
+ * Selects all netflow filters (array (id_name => id_name)) or filters filtered
+ *
+ * @param mixed Array with filter conditions to retrieve filters or false.  
+ *
+ * @return array List of all filters
+ */
+function netflow_get_options ($filter = false) {
+	if ($filter === false) { 
+		$filters = db_get_all_rows_in_table ("tnetflow_options", "id_name");
+	}
+	else {
+		$filters = db_get_all_rows_filter ("tnetflow_options", $filter);
+	}
+	$return = array ();
+	if ($filters === false) {
+		return $return;
+	}
+	foreach ($filters as $filter) {
+		$return[$filter["id_name"]] = $filter["id_name"];
+	}
+	return $return;
+}
+
+/**
+ * Get options.
+ *
+ * @param int filter id to be fetched.
+ * @param array Extra filter.
+ * @param array Fields to be fetched.
+ *
+ * @return array A netflow filter matching id and filter.
+ */
+function netflow_options_get_options ($id_option, $filter = false, $fields = false) {
+	if (empty ($id_option))
+		return false;
+	if (! is_array ($filter))
+		$filter = array ();
+	$filter['id_option'] = (int) $id_option;
+	
+	return db_get_row_filter ('tnetflow_options', $filter, $fields);
+}
+
+/**
+ * Get options.
+ *
+ * @param int filter id to be fetched.
+ * @param array Extra filter.
+ * @param array Fields to be fetched.
+ *
+ * @return array A netflow filter matching id and filter.
+ */
+function netflow_reports_get_reports ($id_report, $filter = false, $fields = false) {
+	if (empty ($id_report))
+		return false;
+	if (! is_array ($filter))
+		$filter = array ();
+	$filter['id_report'] = (int) $id_report;
+	
+	return db_get_row_filter ('tnetflow_report', $filter, $fields);
+}
+
+function netflow_reports_get_content ($id_rc, $filter = false, $fields = false){
+	if (empty ($id_rc))
+		return false;
+	if (! is_array ($filter))
+		$filter = array ();
+	$filter['id_rc'] = (int) $id_rc;
+	
+	return db_get_row_filter ('tnetflow_report_content', $filter, $fields);
+}
+
+//sort array multidimensional
+
+function orderMultiDimensionalArray ($toOrderArray, $field, $inverse = false) {
+     $position = array(); 
+     $newRow = array(); 
+     foreach ($toOrderArray as $key => $row) { 
+             $position[$key]  = $row[$field]; 
+             $newRow[$key] = $row; 
+     } 
+     if ($inverse) { 
+         arsort($position); 
+     } 
+     else { 
+         asort($position); 
+     } 
+     $returnArray = array(); 
+     foreach ($position as $key => $pos) {      
+       $returnArray[] = $newRow[$key]; 
+     } 
+     return $returnArray; 
+}
+
+function netflow_show_total_period($data, $date_limit, $date_time){
+	$values = array();
+	$table->width = '50%';
+	$table->class = 'databox';
+	$table->data = array();
+	$title = "Desde $date_limit hasta $date_time";
+	$j = 0;
+	$x = 1;
+	
+	echo"<h4>Suma por periodo</h4>";
+	$table->data[0][0] = '<b>'.__('Rango').'</b>';
+	$table->data[0][1] = '<b>'.$title.'</b>';
+	
+	while (isset ($data[$j])) {
+		$agg = $data[$j]['agg'];
+		if (!isset($values[$agg])){
+			$values[$agg] = $data[$j]['data'];
+			$table->data[$x][0] = $agg;
+			$table->data[$x][1] = $data[$j]['data'].' '.$data[$j]['unit'];
+		} else {
+			$values[$agg] += $data[$j]['data'];
+			$table->data[$x][0] = $agg;
+			$table->data[$x][1] = $data[$j]['data'].' '.$data[$j]['unit'];
+		}
+		$j++;
+		$x++;
+	}
+html_print_table($table);
+}
+
+function netflow_show_table_values($data, $date_limit, $date_time){
+	$values = array();
+	$table->width = '50%';
+	$table->class = 'databox';
+	$table->data = array();
+	
+	$j = 0;
+	$x = 1;
+	$y = 1;
+	
+	echo"<h4>Tabla de valores</h4>";
+	$table->data[0][0] = '<b>'.__('Rango').'</b>';
+
+	$coordx = array();
+	$coordy = array();
+	
+	while (isset ($data[$j])) {
+		$date = $data[$j]['date'];
+		$time = $data[$j]['time'];
+		$agg = $data[$j]['agg'];
+		
+		if ($data[$j]['unit'] == 'G'){
+			$data[$j]['data'] *= 1024;
+		}
+		
+		if (!isset($values[$agg])){
+			$values['data'] = $data[$j]['data'];			
+		} else {
+			$values['data'] += $data[$j]['data'];
+		}
+		
+		$values['agg'] = $agg;
+		$values['datetime'] = $date.'.'.$time;
+		
+		if(isset($coordy[$agg])) {
+			$cy = $coordy[$agg];
+		}
+		else {
+			$cy = $y;
+			$coordy[$agg] = $cy;
+			$y++;
+		}
+		
+		if(isset($coordx[$date.'.'.$time])) {
+			$cx = $coordx[$date.'.'.$time];
+		}
+		else {
+			$cx = $x;
+			$coordx[$date.'.'.$time] = $cx;
+			$x++;
+		}
+		
+		$table->data[0][$cy] = $agg;
+		$table->data[$cx][0] = $date.'.'.$time;
+		$table->data[$cx][$cy] = $values['data'];
+
+		$j++;
+	}
+	//si la coordenada no tiene valor, se rellena con 0
+	foreach($coordx as $x) {
+		foreach($coordy as $y) {
+			if(!isset($table->data[$x][$y])) {
+				$table->data[$x][$y] = 0;
+			}
+		}
+	}
+	//ordenar los indices
+	foreach($coordx as $x) {
+		ksort($table->data[$x]);
+	}	
+
+html_print_table($table);
+}
+
+?>
diff --git a/pandora_console/index.php b/pandora_console/index.php
index 05e50b03cd..671fe7b289 100644
--- a/pandora_console/index.php
+++ b/pandora_console/index.php
@@ -21,7 +21,7 @@ if (function_exists ('mb_internal_encoding')) {
 
 // Set to 1 to do not check for installer or config file (for development!).
 // Activate gives more error information, not useful for production sites
-$develop_bypass = 0;
+$develop_bypass = 1;
 
 if ($develop_bypass != 1) {
 	// If no config file, automatically try to install
diff --git a/pandora_console/operation/netflow/nf_reporting.php b/pandora_console/operation/netflow/nf_reporting.php
index 4d2d8b2def..fa2a0cff2a 100644
--- a/pandora_console/operation/netflow/nf_reporting.php
+++ b/pandora_console/operation/netflow/nf_reporting.php
@@ -31,7 +31,7 @@ if (! check_acl ($config["id_user"], 0, "IR")) {
 }
 		
 //Header
-ui_print_page_header (__('Netflow Reporting'), "images/networkmap/so_cisco_new.png", false, "", false, $buttons);
+ui_print_page_header (__('Netflow Reporting'), "images/networkmap/so_cisco_new.png", false, "", false);
 
 $filter = array ();
 
@@ -40,7 +40,7 @@ $filter['limit'] = (int) $config['block_size'];
 
 $reports = db_get_all_rows_filter ('tnetflow_report', $filter);
 
-$table->width = '80%';
+$table->width = '98%';
 $table->head = array ();
 $table->head[0] = __('Report name');
 $table->head[1] = __('Description');
@@ -57,7 +57,7 @@ $table->data = array ();
 $total_reports = db_get_all_rows_filter ('tnetflow_report', false, 'COUNT(*) AS total');
 $total_reports = $total_reports[0]['total'];
 
-ui_pagination ($total_reports, $url);
+//ui_pagination ($total_reports, $url);
 
 foreach ($reports as $report) {
 	$data = array ();
diff --git a/pandora_console/operation/netflow/nf_view.php b/pandora_console/operation/netflow/nf_view.php
index 0b8b8ecd3c..d91daec348 100644
--- a/pandora_console/operation/netflow/nf_view.php
+++ b/pandora_console/operation/netflow/nf_view.php
@@ -427,6 +427,7 @@ if ($show_bpp)
 
 	// Data iterator
 	$j = 0;
+	$values = array();
 			
 	// Calculate interval date
 	for ($i = 0; $i < $inter; $i++) {
@@ -471,7 +472,9 @@ if ($show_bpp)
 
 			switch ($element){
 				case '0':
-					echo grafico_netflow_aggregate_area($result, $interval, 880, 540, $id_name, '','','',$date);
+					echo grafico_netflow_aggregate_area($result, $interval, 880, 540, '', '','','',$date);
+					echo netflow_show_table_values($result, $date_limit, $date_time);
+					echo netflow_show_total_period($result, $date_limit, $date_time);
 					break;
 				case '1':
 					echo grafico_netflow_aggregate_pie($result);
@@ -486,7 +489,7 @@ if ($show_bpp)
 		}else{
 			switch ($element){
 				case '0':
-					echo grafico_netflow_total_area($values, $interval, 660, 320, $id_name, '','','',$date);
+					echo grafico_netflow_total_area($values, $interval, 660, 320, '', '','','',$date);
 					break;
 			}
 		}
diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql
index d781f199e4..f40147f234 100644
--- a/pandora_console/pandoradb.sql
+++ b/pandora_console/pandoradb.sql
@@ -1278,3 +1278,65 @@ CREATE TABLE IF NOT EXISTS `ttag_event` (
    PRIMARY KEY  (id_tag, id_evento),
    KEY `idx_id_evento` (`id_evento`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
+
+-- -----------------------------------------------------
+-- Table `tnetflow_filter`
+-- -----------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `tnetflow_filter` (
+  `id_sg`  int(10) unsigned NOT NULL auto_increment,
+  `id_name` varchar(60) NOT NULL default '0',
+  `group` varchar(60),
+  `ip_dst` varchar(100),
+  `ip_src` varchar(100),
+  `dst_port` varchar(100),
+  `src_port` varchar(100),
+  `aggregate` varchar(60),
+  `show_packets` int(1) default '0',
+  `show_bytes` int(1) default '0',
+  `show_bps` int(1) default '0',
+  `show_bpp` int(1) default '0',
+PRIMARY KEY  (`id_sg`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- -----------------------------------------------------
+-- Table `tnetflow_report`
+-- -----------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `tnetflow_report` (
+  `id_report` INTEGER UNSIGNED NOT NULL  AUTO_INCREMENT,
+  `id_name` varchar(150) NOT NULL default '',
+  `description` TEXT NOT NULL,
+  `group` varchar(60),
+PRIMARY KEY(`id_report`)  
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- -----------------------------------------------------
+-- Table `tnetflow_options`
+-- -----------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `tnetflow_options` (
+  `id_option`  int(10) unsigned NOT NULL auto_increment,
+  `id_name` varchar(60) NOT NULL default '0',
+  `description` TEXT NOT NULL default '',
+  `path` varchar(200),
+  `port` varchar(100),
+PRIMARY KEY  (`id_option`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- -----------------------------------------------------
+-- Table `tnetflow_report_content`
+-- -----------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `tnetflow_report_content` (
+   	`id_rc` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+	`id_report` INTEGER UNSIGNED NOT NULL default 0,
+        `id_filter`  varchar(60),
+	`date` bigint(20) NOT NULL default '0',
+	`period` int(11) NOT NULL default 0,
+	`max` int (11) NOT NULL default 0,
+	`show_graph` varchar(60),
+	PRIMARY KEY(`id_rc`),
+	FOREIGN KEY (`id_report`) REFERENCES tnetflow_report(`id_report`)
+		ON UPDATE CASCADE ON DELETE CASCADE
+) ENGINE = InnoDB DEFAULT CHARSET=utf8;