diff --git a/pandora_console/include/functions_netflow.php b/pandora_console/include/functions_netflow.php
index 2cef9b3263..4686e5588f 100644
--- a/pandora_console/include/functions_netflow.php
+++ b/pandora_console/include/functions_netflow.php
@@ -1,22 +1,34 @@
id_name)) or filters filtered
*
- * @param mixed Array with filter conditions to retrieve filters or false.
+ * @param mixed $filter Array with filter conditions to retrieve filters or
+ * false.
*
- * @return array List of all filters
+ * @return array List of all filters.
*/
function netflow_get_filters($filter=false)
{
@@ -64,9 +77,10 @@ function netflow_get_filters($filter=false)
/**
* Selects all netflow reports (array (id_name => id_name)) or filters filtered
*
- * @param mixed Array with filter conditions to retrieve filters or false.
+ * @param mixed $filter Array with filter conditions to retrieve filters or
+ * false.
*
- * @return array List of all filters
+ * @return array List of all filters.
*/
function netflow_get_reports($filter=false)
{
@@ -89,14 +103,20 @@ function netflow_get_reports($filter=false)
}
-// permite validar si un filtro pertenece a un grupo permitido para el usuario
+/**
+ * Check if a filter owns to a certain group.
+ *
+ * @param integer $id_sg Id group to check.
+ *
+ * @return boolean True if user manages that group.
+ */
function netflow_check_filter_group($id_sg)
{
global $config;
$id_group = db_get_value('id_group', 'tnetflow_filter', 'id_sg', $id_sg);
$own_info = get_user_info($config['id_user']);
- // Get group list that user has access
+ // Get group list that user has access.
$groups_user = users_get_groups($config['id_user'], 'IW', $own_info['is_admin'], true);
$groups_id = [];
$has_permission = false;
@@ -111,44 +131,12 @@ function netflow_check_filter_group($id_sg)
}
-/*
- Permite validar si un informe pertenece a un grupo permitido para el usuario.
- * Si mode = false entonces es modo godmode y solo puede ver el grupo All el admin
- * Si es modo operation (mode = true) entonces todos pueden ver el grupo All
- */
-
-function netflow_check_report_group($id_report, $mode=false)
-{
- global $config;
-
- if (!$mode) {
- $own_info = get_user_info($config['id_user']);
- $mode = $own_info['is_admin'];
- }
-
- $id_group = db_get_value('id_group', 'tnetflow_report', 'id_report', $id_report);
-
- // Get group list that user has access
- $groups_user = users_get_groups($config['id_user'], 'IW', $mode, true);
- $groups_id = [];
- $has_permission = false;
-
- foreach ($groups_user as $key => $groups) {
- if ($groups['id_grupo'] == $id_group) {
- return true;
- }
- }
-
- return false;
-}
-
-
/**
* Get a filter.
*
- * @param int filter id to be fetched.
- * @param array Extra filter.
- * @param array Fields to be fetched.
+ * @param integer $id_sg Filter id to be fetched.
+ * @param mixed $filter Extra filter.
+ * @param mixed $fields Fields to be fetched.
*
* @return array A netflow filter matching id and filter.
*/
@@ -164,52 +152,11 @@ function netflow_filter_get_filter($id_sg, $filter=false, $fields=false)
}
-/**
- * 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 = [];
- }
-
- $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 = [];
- }
-
- $filter['id_rc'] = (int) $id_rc;
-
- return db_get_row_filter('tnetflow_report_content', $filter, $fields);
-}
-
-
/**
* Compare two flows according to the 'data' column.
*
- * @param array a First flow.
- * @param array b Second flow.
+ * @param array $a First flow.
+ * @param array $b Second flow.
*
* @return Result of the comparison.
*/
@@ -222,7 +169,9 @@ function compare_flows($a, $b)
/**
* Sort netflow data according to the 'data' column.
*
- * @param array netflow_data Netflow data array.
+ * @param array $netflow_data Netflow data array.
+ *
+ * @return void (Array passed by reference)
*/
function sort_netflow_data(&$netflow_data)
{
@@ -233,12 +182,12 @@ function sort_netflow_data(&$netflow_data)
/**
* Show a table with netflow statistics.
*
- * @param array data Statistic data.
- * @param string start_date Start date.
- * @param string end_date End date.
- * @param string aggregate Aggregate field.
+ * @param array $data Statistic data.
+ * @param string $start_date Start date.
+ * @param string $end_date End date.
+ * @param string $aggregate Aggregate field.
*
- * @return The statistics table.
+ * @return string HTML statistics table.
*/
function netflow_stat_table($data, $start_date, $end_date, $aggregate)
{
@@ -282,12 +231,12 @@ function netflow_stat_table($data, $start_date, $end_date, $aggregate)
/**
* Show a table with netflow data.
*
- * @param array data Netflow data.
- * @param string start_date Start date.
- * @param string end_date End date.
- * @param string aggregate Aggregate field.
+ * @param array $data Netflow data.
+ * @param string $start_date Start date.
+ * @param string $end_date End date.
+ * @param string $aggregate Aggregate field.
*
- * @return The statistics table.
+ * @return string HTML data table.
*/
function netflow_data_table($data, $start_date, $end_date, $aggregate)
{
@@ -297,7 +246,7 @@ function netflow_data_table($data, $start_date, $end_date, $aggregate)
$start_date = date($nfdump_date_format, $start_date);
$end_date = date($nfdump_date_format, $end_date);
- // Set the format
+ // Set the format.
if ($period <= SECONDS_6HOURS) {
$time_format = 'H:i:s';
} else if ($period < SECONDS_1DAY) {
@@ -338,7 +287,7 @@ function netflow_data_table($data, $start_date, $end_date, $aggregate)
$table->style[1] = 'padding: 4px;';
}
- // No aggregates
+ // No aggregates.
if ($source_count == 0) {
$table->head[1] = __('Data');
$table->align[1] = 'right';
@@ -350,7 +299,8 @@ function netflow_data_table($data, $start_date, $end_date, $aggregate)
$i++;
}
}
- // Aggregates
+
+ // Aggregates.
else {
$i = 0;
foreach ($data['data'] as $timestamp => $values) {
@@ -372,9 +322,9 @@ function netflow_data_table($data, $start_date, $end_date, $aggregate)
/**
* Show a table with a traffic summary.
*
- * @param array data Summary data.
+ * @param array $data Summary data.
*
- * @return The statistics table.
+ * @return string HTML summary table.
*/
function netflow_summary_table($data)
{
@@ -428,7 +378,7 @@ function netflow_summary_table($data)
/**
* Returns 1 if the given address is a network address.
*
- * @param string address Host or network address.
+ * @param string $address Host or network address.
*
* @return 1 if the address is a network address, 0 otherwise.
*/
@@ -445,25 +395,40 @@ function netflow_is_net($address)
/**
* Returns netflow data for the given period in an array.
*
- * @param string start_date Period start date.
- * @param string end_date Period end date.
- * @param string filter Netflow filter.
- * @param string aggregate Aggregate field.
- * @param int max Maximum number of aggregates.
- * @param int max Maximum number of aggregates.
- * @param boolean absolute True to give the absolute data and false to get
- * troughput.
+ * @param string $start_date Period start date.
+ * @param string $end_date Period end date.
+ * @param mixed $interval_length Resolution points or hourly or daily.
+ * @param string $filter Netflow filter.
+ * @param string $aggregate Aggregate field.
+ * @param integer $max Maximum number of aggregates.
+ * @param boolean $absolute True to give the absolute data and false
+ * to get troughput.
+ * @param string $connection_name Node name when data is get in meta.
+ * @param boolean $address_resolution True to resolve ips to hostnames.
*
- * @return An array with netflow stats.
+ * @return array An array with netflow stats.
*/
-function netflow_get_data($start_date, $end_date, $interval_length, $filter, $aggregate, $max, $absolute, $connection_name='', $address_resolution=false)
-{
+function netflow_get_data(
+ $start_date,
+ $end_date,
+ $interval_length,
+ $filter,
+ $aggregate,
+ $max,
+ $absolute,
+ $connection_name='',
+ $address_resolution=false
+) {
global $nfdump_date_format;
global $config;
- // Requesting remote data
+ // Requesting remote data.
if (defined('METACONSOLE') && $connection_name != '') {
- $data = metaconsole_call_remote_api($connection_name, 'netflow_get_data', "$start_date|$end_date|$interval_length|".base64_encode(json_encode($filter))."|$aggregate|$max|1".(int) $address_resolution);
+ $data = metaconsole_call_remote_api(
+ $connection_name,
+ 'netflow_get_data',
+ "$start_date|$end_date|$interval_length|".base64_encode(json_encode($filter))."|$aggregate|$max|1".(int) $address_resolution
+ );
return json_decode($data, true);
}
@@ -513,24 +478,25 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
$intervals[] = $end_date;
}
- // If there is aggregation calculate the top n
+ // If there is aggregation calculate the top n.
$values['data'] = [];
$values['sources'] = [];
- // Get the command to call nfdump
+ // Get the command to call nfdump.
$command = netflow_get_command($filter);
- // Suppress the header line and the statistics at the bottom and configure piped output
+ // Suppress the header line and the statistics at the bottom and configure
+ // piped output.
$command .= ' -q -o csv';
- // Call nfdump
+ // Call nfdump.
$agg_command = $command." -n $max -s $aggregate/bytes -t ".date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date);
exec($agg_command, $string);
- // Remove the first line
+ // Remove the first line.
$string[0] = '';
- // Parse aggregates
+ // Parse aggregates.
foreach ($string as $line) {
if ($line == '') {
continue;
@@ -544,7 +510,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
}
}
- // Update the filter
+ // Update the filter.
switch ($aggregate) {
case 'proto':
$extra_filter = 'proto';
@@ -576,7 +542,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
array_keys($values['sources'])
);
- // Address resolution start
+ // Address resolution start.
$get_hostnames = false;
if ($address_resolution && ($aggregate == 'srcip' || $aggregate == 'dstip')) {
$get_hostnames = true;
@@ -609,7 +575,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
$interval_end = $intervals[($k + 1)];
- // Set default values
+ // Set default values.
foreach ($values['sources'] as $source => $discard) {
$values['data'][$interval_end][$source] = 0;
}
@@ -625,11 +591,11 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
);
foreach ($data as $line) {
- // Address resolution start
+ // Address resolution start.
if ($get_hostnames) {
if (!isset($hostnames[$line['agg']])) {
$hostname = false;
- // Trying to get something like an IP from the description
+ // Trying to get something like an IP from the description.
if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $line['agg'], $matches)
|| preg_match(
"/(((?=(?>.*?(::))(?!.+\3)))\3?|([\dA-F]{1,4}(\3|:?)|\2))(?4){5}((?4){2}|(25[0-5]|
@@ -652,7 +618,7 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
}
}
- // Address resolution end
+ // Address resolution end.
if (! isset($values['sources'][$line['agg']])) {
continue;
}
@@ -672,30 +638,40 @@ function netflow_get_data($start_date, $end_date, $interval_length, $filter, $ag
/**
* Returns netflow stats for the given period in an array.
*
- * @param string start_date Period start date.
- * @param string end_date Period end date.
- * @param string filter Netflow filter.
- * @param string aggregate Aggregate field.
- * @param int max Maximum number of aggregates.
- * @param boolean absolute True to give the absolute data and false to get
- * troughput.
+ * @param string $start_date Period start date.
+ * @param string $end_date Period end date.
+ * @param string $filter Netflow filter.
+ * @param string $aggregate Aggregate field.
+ * @param integer $max Maximum number of aggregates.
+ * @param boolean $absolute True to give the absolute data and false to get
+ * troughput.
+ * @param string $connection_name Node name when data is get in meta.
+ * @param boolean $address_resolution True to resolve ips to hostnames.
*
- * @return An array with netflow stats.
+ * @return array With netflow stats.
*/
-function netflow_get_stats($start_date, $end_date, $filter, $aggregate, $max, $absolute=true, $connection_name='', $address_resolution=false)
-{
+function netflow_get_stats(
+ $start_date,
+ $end_date,
+ $filter,
+ $aggregate,
+ $max,
+ $absolute=true,
+ $connection_name='',
+ $address_resolution=false
+) {
global $config, $nfdump_date_format;
- // Requesting remote data
+ // Requesting remote data.
if (defined('METACONSOLE') && $connection_name != '') {
$data = metaconsole_call_remote_api($connection_name, 'netflow_get_stats', "$start_date|$end_date|".base64_encode(json_encode($filter))."|$aggregate|$max|$absolute|".(int) $address_resolution);
return json_decode($data, true);
}
- // Get the command to call nfdump
+ // Get the command to call nfdump.
$command = netflow_get_command($filter);
- // Execute nfdump
+ // Execute nfdump.
$command .= " -o csv -q -n $max -s $aggregate/bytes -t ".date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date);
exec($command, $string);
@@ -703,7 +679,7 @@ function netflow_get_stats($start_date, $end_date, $filter, $aggregate, $max, $a
return [];
}
- // Remove the first line
+ // Remove the first line.
$string[0] = '';
$i = 0;
@@ -719,14 +695,14 @@ function netflow_get_stats($start_date, $end_date, $filter, $aggregate, $max, $a
$values[$i]['date'] = $val[0];
$values[$i]['time'] = $val[1];
- // create field to sort array
+ // Create field to sort array.
$datetime = $val[0];
$end_date = strtotime($datetime);
$values[$i]['datetime'] = $end_date;
if ($aggregate == 'proto') {
$values[$i]['agg'] = $val[3];
} else {
- // Address resolution start
+ // Address resolution start.
if ($address_resolution && ($aggregate == 'srcip' || $aggregate == 'dstip')) {
global $hostnames;
@@ -741,7 +717,7 @@ function netflow_get_stats($start_date, $end_date, $filter, $aggregate, $max, $a
}
}
- // Address resolution end
+ // Address resolution end.
$values[$i]['agg'] = $val[4];
}
@@ -766,27 +742,28 @@ function netflow_get_stats($start_date, $end_date, $filter, $aggregate, $max, $a
/**
* 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 filter Netflow filter.
+ * @param string $start_date Period start date.
+ * @param string $end_date Period end date.
+ * @param string $filter Netflow filter.
+ * @param string $connection_name Node name when data is get in meta.
*
- * @return An array with netflow stats.
+ * @return array With netflow summary data.
*/
function netflow_get_summary($start_date, $end_date, $filter, $connection_name='')
{
global $nfdump_date_format;
global $config;
- // Requesting remote data
+ // Requesting remote data.
if (defined('METACONSOLE') && $connection_name != '') {
$data = metaconsole_call_remote_api($connection_name, 'netflow_get_summary', "$start_date|$end_date|".base64_encode(json_encode($filter)));
return json_decode($data, true);
}
- // Get the command to call nfdump
+ // Get the command to call nfdump.
$command = netflow_get_command($filter);
- // Execute nfdump
+ // Execute nfdump.
$command .= ' -o csv -n 1 -s srcip/bytes -t '.date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date);
exec($command, $string);
@@ -794,7 +771,7 @@ function netflow_get_summary($start_date, $end_date, $filter, $connection_name='
return [];
}
- // Read the summary
+ // Read the summary.
$summary = explode(',', $string[5]);
if (! isset($summary[5])) {
return [];
@@ -814,31 +791,28 @@ function netflow_get_summary($start_date, $end_date, $filter, $connection_name='
/**
* Returns a traffic record for the given period in an array.
*
- * @param string start_date Period start date.
- * @param string end_date Period end date.
- * @param string filter Netflow filter.
- * @param int max Maximum number of elements.
+ * @param string $start_date Period start date.
+ * @param string $end_date Period end date.
+ * @param string $filter Netflow filter.
+ * @param integer $max Maximum number of elements.
+ * @param boolean $address_resolution True to resolve ips to hostnames.
*
- * @return An array with netflow stats.
+ * @return array With netflow record data.
*/
-function netflow_get_record($start_date, $end_date, $filter, $max, $address_resolution=false)
-{
+function netflow_get_record(
+ $start_date,
+ $end_date,
+ $filter,
+ $max,
+ $address_resolution=false
+) {
global $nfdump_date_format;
global $config;
- // TIME_START = 0;
- // TIME_END = 1;
- // DURATION = 2;
- // SOURCE_ADDRESS = 3;
- // DESTINATION_ADDRESS = 4;
- // SOURCE_PORT = 5;
- // DESTINATION_PORT = 6;
- // PROTOCOL = 7;
- // INPUT_BYTES = 12;
- // Get the command to call nfdump
+ // Get the command to call nfdump.
$command = netflow_get_command($filter);
- // Execute nfdump
+ // Execute nfdump.
$command .= " -q -o csv -n $max -s record/bytes -t ".date($nfdump_date_format, $start_date).'-'.date($nfdump_date_format, $end_date);
exec($command, $result);
@@ -865,7 +839,7 @@ function netflow_get_record($start_date, $end_date, $filter, $max, $address_reso
$values[] = $data;
}
- // Address resolution start
+ // Address resolution start.
if ($address_resolution) {
global $hostnames;
@@ -892,7 +866,7 @@ function netflow_get_record($start_date, $end_date, $filter, $max, $address_reso
}
}
- // Address resolution end
+ // Address resolution end.
return $values;
}
@@ -900,23 +874,23 @@ function netflow_get_record($start_date, $end_date, $filter, $max, $address_reso
/**
* Returns the command needed to run nfdump for the given filter.
*
- * @param array filter Netflow filter.
+ * @param array $filter Netflow filter.
*
- * @return Command to run.
+ * @return string Command to run.
*/
function netflow_get_command($filter)
{
global $config;
- // Build command
+ // Build command.
$command = io_safe_output($config['netflow_nfdump']).' -N';
- // Netflow data path
+ // Netflow data path.
if (isset($config['netflow_path']) && $config['netflow_path'] != '') {
$command .= ' -R. -M '.$config['netflow_path'];
}
- // Filter options
+ // Filter options.
$command .= netflow_get_filter_arguments($filter);
return $command;
@@ -926,13 +900,13 @@ function netflow_get_command($filter)
/**
* Returns the nfdump command line arguments that match the given filter.
*
- * @param array filter Netflow filter.
+ * @param array $filter Netflow filter.
*
- * @return Command line argument string.
+ * @return string Command line argument string.
*/
function netflow_get_filter_arguments($filter)
{
- // Advanced filter
+ // Advanced filter.
$filter_args = '';
if ($filter['advanced_filter'] != '') {
$filter_args = preg_replace('/["\r\n]/', '', io_safe_output($filter['advanced_filter']));
@@ -943,7 +917,7 @@ function netflow_get_filter_arguments($filter)
$filter_args .= ' "(router ip '.$filter['router_ip'].')';
}
- // Normal filter
+ // Normal filter.
if ($filter['ip_dst'] != '') {
$filter_args .= ' "(';
$val_ipdst = explode(',', io_safe_output($filter['ip_dst']));
@@ -1071,18 +1045,30 @@ function netflow_get_chart_types()
/**
* Draw a netflow report item.
*
- * @param string start_date Period start date.
- * @param string end_date Period end date.
- * @param string interval_length Interval length in seconds (num_intervals * interval_length = start_date - end_date).
- * @param string type Chart type.
- * @param array filter Netflow filter.
- * @param int max_aggregates Maximum number of aggregates.
- * @param string output Output format. Only HTML and XML are supported.
+ * @param string $start_date Period start date.
+ * @param string $end_date Period end date.
+ * @param mixed $interval_length Resolution points or hourly or daily.
+ * @param string $type Chart type.
+ * @param array $filter Netflow filter.
+ * @param integer $max_aggregates Maximum number of aggregates.
+ * @param string $connection_name Node name when data is get in meta.
+ * @param string $output Output format. Only HTML, PDF and XML
+ * are supported.
+ * @param boolean $address_resolution True to resolve ips to hostnames.
*
- * @return The netflow report in the appropriate format.
+ * @return string The netflow report in the appropriate format.
*/
-function netflow_draw_item($start_date, $end_date, $interval_length, $type, $filter, $max_aggregates, $connection_name='', $output='HTML', $address_resolution=false)
-{
+function netflow_draw_item(
+ $start_date,
+ $end_date,
+ $interval_length,
+ $type,
+ $filter,
+ $max_aggregates,
+ $connection_name='',
+ $output='HTML',
+ $address_resolution=false
+) {
$aggregate = $filter['aggregate'];
$interval = ($end_date - $start_date);
if (defined('METACONSOLE')) {
@@ -1093,11 +1079,21 @@ function netflow_draw_item($start_date, $end_date, $interval_length, $type, $fil
$height = 320;
- // Process item
+ // Process item.
switch ($type) {
case '0':
case 'netflow_area':
- $data = netflow_get_data($start_date, $end_date, $interval_length, $filter, $aggregate, $max_aggregates, false, $connection_name, $address_resolution);
+ $data = netflow_get_data(
+ $start_date,
+ $end_date,
+ $interval_length,
+ $filter,
+ $aggregate,
+ $max_aggregates,
+ false,
+ $connection_name,
+ $address_resolution
+ );
if (empty($data)) {
break;
}
@@ -1119,8 +1115,8 @@ function netflow_draw_item($start_date, $end_date, $interval_length, $type, $fil
);
return $html;
} else if ($output == 'XML') {
- $xml .= "