2019-03-04 12:43:31 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Network explorer
|
|
|
|
*
|
|
|
|
* @package Include.
|
|
|
|
* @subpackage Network functions.
|
|
|
|
*
|
|
|
|
* Pandora FMS - http://pandorafms.com
|
|
|
|
* ==================================================
|
|
|
|
* Copyright (c) 2005-2019 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 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Write here requires and definitions.
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the tnetwok_matrix summatory data.
|
|
|
|
*
|
2019-03-08 12:11:18 +01:00
|
|
|
* @param integer $top Number of hosts to show.
|
|
|
|
* @param boolean $talker Talker (true) or listetener (false).
|
|
|
|
* @param integer $start Utimestamp of start time.
|
|
|
|
* @param integer $end Utimestamp of end time.
|
|
|
|
* @param string $ip_filter Ip to filter.
|
|
|
|
* @param boolean $order_by_bytes True by top by bytes. False by packets.
|
2019-03-25 12:40:49 +01:00
|
|
|
* @param array $host_filter Host filter array.
|
2019-03-04 12:43:31 +01:00
|
|
|
*
|
|
|
|
* @return array With requested data.
|
|
|
|
*/
|
2019-03-08 12:11:18 +01:00
|
|
|
function network_matrix_get_top(
|
|
|
|
$top,
|
|
|
|
$talker,
|
|
|
|
$start,
|
|
|
|
$end,
|
|
|
|
$ip_filter='',
|
2019-03-25 12:30:51 +01:00
|
|
|
$order_by_bytes=true,
|
|
|
|
$host_filter=[]
|
2019-03-08 12:11:18 +01:00
|
|
|
) {
|
2019-03-04 12:43:31 +01:00
|
|
|
$field_to_group = ($talker === true) ? 'source' : 'destination';
|
2019-03-08 12:11:18 +01:00
|
|
|
$field_to_order = ($order_by_bytes === true) ? 'sum_bytes' : 'sum_pkts';
|
2019-03-04 20:26:11 +01:00
|
|
|
$filter_sql = '';
|
|
|
|
if (!empty($ip_filter)) {
|
|
|
|
$filter_field = ($talker === true) ? 'destination' : 'source';
|
|
|
|
$filter_sql = sprintf('AND %s="%s"', $filter_field, $ip_filter);
|
|
|
|
}
|
|
|
|
|
2019-03-25 12:30:51 +01:00
|
|
|
$host_filter_sql = '';
|
|
|
|
if (!empty($host_filter)) {
|
|
|
|
$host_filter_sql = sprintf(
|
|
|
|
' AND %s IN ("%s")',
|
|
|
|
$field_to_group,
|
|
|
|
implode('","', $host_filter)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-04 12:43:31 +01:00
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT SUM(bytes) sum_bytes, SUM(pkts) sum_pkts, %s host
|
|
|
|
FROM tnetwork_matrix
|
|
|
|
WHERE utimestamp > %d AND utimestamp < %d
|
2019-03-04 20:26:11 +01:00
|
|
|
%s
|
2019-03-25 12:30:51 +01:00
|
|
|
%s
|
2019-03-04 12:43:31 +01:00
|
|
|
GROUP BY %s
|
2019-03-08 12:11:18 +01:00
|
|
|
ORDER BY %s DESC
|
2019-03-04 12:43:31 +01:00
|
|
|
LIMIT %d',
|
|
|
|
$field_to_group,
|
|
|
|
$start,
|
|
|
|
$end,
|
2019-03-04 20:26:11 +01:00
|
|
|
$filter_sql,
|
2019-03-25 12:30:51 +01:00
|
|
|
$host_filter_sql,
|
2019-03-04 12:43:31 +01:00
|
|
|
$field_to_group,
|
2019-03-08 12:11:18 +01:00
|
|
|
$field_to_order,
|
2019-03-04 12:43:31 +01:00
|
|
|
$top
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = db_get_all_rows_sql($sql);
|
|
|
|
|
|
|
|
return ($data !== false) ? $data : [];
|
|
|
|
}
|
2019-03-04 16:29:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the possible actions on networking.
|
|
|
|
*
|
|
|
|
* @param boolean $network True if network. False if netflow.
|
|
|
|
*
|
|
|
|
* @return array With the actions to print in a select.
|
|
|
|
*/
|
2019-03-25 13:10:47 +01:00
|
|
|
function network_get_report_actions($network=true)
|
2019-03-04 16:29:08 +01:00
|
|
|
{
|
|
|
|
$common_actions = [
|
|
|
|
'listeners' => __('Top listeners'),
|
|
|
|
'talkers' => __('Top talkers'),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($network) {
|
|
|
|
return $common_actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
$common_actions,
|
|
|
|
[
|
|
|
|
'tcp' => __('Top TCP protocols'),
|
|
|
|
'udp' => __('Top UDP protocols'),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-03-08 11:46:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print the header of the network
|
|
|
|
*
|
|
|
|
* @param string $title Title of header.
|
|
|
|
* @param string $order Current ordering.
|
|
|
|
* @param string $selected Selected order.
|
|
|
|
* @param array $hidden_data All the data to hide into the button.
|
|
|
|
*
|
|
|
|
* @return string With HTML data.
|
|
|
|
*/
|
|
|
|
function network_print_explorer_header(
|
|
|
|
$title,
|
|
|
|
$order,
|
|
|
|
$selected,
|
|
|
|
$hidden_data
|
|
|
|
) {
|
|
|
|
$cell = '<div style="display: flex; align-items: center;">';
|
|
|
|
$cell .= $title;
|
|
|
|
$cell .= html_print_link_with_params(
|
|
|
|
'images/arrow-down-white.png',
|
|
|
|
array_merge($hidden_data, ['order_by' => $order]),
|
|
|
|
'image',
|
|
|
|
($selected === $order) ? 'opacity: 0.5' : ''
|
|
|
|
);
|
|
|
|
$cell .= '</div>';
|
|
|
|
|
|
|
|
return $cell;
|
|
|
|
}
|
2019-03-14 09:25:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for format_for_graph to print bytes.
|
|
|
|
*
|
|
|
|
* @param integer $value Value to parse like bytes.
|
|
|
|
*
|
|
|
|
* @return string Number parsed.
|
|
|
|
*/
|
|
|
|
function network_format_bytes($value)
|
|
|
|
{
|
|
|
|
if (!isset($value)) {
|
|
|
|
$value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = (int) $value;
|
|
|
|
|
|
|
|
return format_for_graph(
|
|
|
|
$value,
|
|
|
|
2,
|
|
|
|
'.',
|
|
|
|
',',
|
|
|
|
1024,
|
|
|
|
'B'
|
|
|
|
);
|
|
|
|
}
|
2019-03-25 12:30:51 +01:00
|
|
|
|
|
|
|
|
2019-03-25 12:40:49 +01:00
|
|
|
/**
|
|
|
|
* Build netflow data structure to network map.
|
|
|
|
*
|
2019-03-25 13:10:47 +01:00
|
|
|
* @param integer $start Time in timestamp format.
|
|
|
|
* @param integer $end Time in timestamp format.
|
|
|
|
* @param integer $top Max data to show.
|
|
|
|
* @param boolean $talker True to get top tolkers. False for listeners.
|
2019-03-25 12:40:49 +01:00
|
|
|
*
|
|
|
|
* @return array With map structure.
|
|
|
|
*/
|
2019-03-25 13:10:47 +01:00
|
|
|
function network_build_map_data($start, $end, $top, $talker)
|
2019-03-25 12:30:51 +01:00
|
|
|
{
|
2019-03-25 13:10:47 +01:00
|
|
|
$data = network_matrix_get_top($top, $talker, $start, $end);
|
2019-03-25 12:30:51 +01:00
|
|
|
|
|
|
|
$hosts = array_map(
|
|
|
|
function ($elem) {
|
|
|
|
return $elem['host'];
|
|
|
|
},
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
$inverse_hosts = array_flip($hosts);
|
|
|
|
|
|
|
|
$nodes = array_map(
|
|
|
|
function ($elem) {
|
|
|
|
return [
|
|
|
|
'name' => $elem,
|
|
|
|
'type' => NODE_GENERIC,
|
|
|
|
'width' => 20,
|
|
|
|
'height' => 20,
|
|
|
|
'status' => '#82B92E',
|
|
|
|
];
|
|
|
|
},
|
|
|
|
$hosts
|
|
|
|
);
|
|
|
|
|
|
|
|
$relations = [];
|
2019-03-25 15:57:04 +01:00
|
|
|
$orphan_relations = [];
|
2019-03-25 12:30:51 +01:00
|
|
|
foreach ($hosts as $host) {
|
|
|
|
$host_top = network_matrix_get_top(
|
|
|
|
$top,
|
2019-03-25 13:10:47 +01:00
|
|
|
!$talker,
|
2019-03-25 12:30:51 +01:00
|
|
|
$start,
|
|
|
|
$end,
|
|
|
|
$host,
|
|
|
|
true,
|
|
|
|
$hosts
|
|
|
|
);
|
|
|
|
foreach ($host_top as $sd) {
|
|
|
|
$src_index = $inverse_hosts[$host];
|
|
|
|
$dst_index = $inverse_hosts[$sd['host']];
|
|
|
|
if (isset($src_index) === false || isset($dst_index) === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-25 12:40:49 +01:00
|
|
|
$relations[$src_index.'-'.$dst_index] = [
|
2019-03-25 12:30:51 +01:00
|
|
|
'id_parent' => $inverse_hosts[$sd['host']],
|
|
|
|
'parent_type' => NODE_GENERIC,
|
|
|
|
'child_type' => NODE_GENERIC,
|
|
|
|
'id_child' => $inverse_hosts[$host],
|
|
|
|
'link_color' => '#82B92E',
|
2019-03-25 15:57:04 +01:00
|
|
|
'text_start' => network_format_bytes($sd['sum_bytes']),
|
2019-03-25 12:30:51 +01:00
|
|
|
];
|
|
|
|
}
|
2019-03-25 15:57:04 +01:00
|
|
|
|
|
|
|
// Put the orphans on Other node.
|
|
|
|
if (empty($host_top)) {
|
|
|
|
$other_id = (end($inverse_hosts) + 1);
|
|
|
|
// TODOS: Add the data.
|
|
|
|
$orphan_relations[$inverse_hosts[$host].'-'.$other_id] = [
|
|
|
|
'id_parent' => $other_id,
|
|
|
|
'parent_type' => NODE_GENERIC,
|
|
|
|
'child_type' => NODE_GENERIC,
|
|
|
|
'id_child' => $inverse_hosts[$host],
|
|
|
|
'link_color' => '#82B92E',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the Others node and their relations.
|
|
|
|
if (empty($orphan_relations) === false) {
|
|
|
|
$nodes[] = [
|
|
|
|
'name' => __('Others'),
|
|
|
|
'type' => NODE_GENERIC,
|
|
|
|
'width' => 20,
|
|
|
|
'height' => 20,
|
|
|
|
'status' => '#82B92E',
|
|
|
|
];
|
|
|
|
$relations = array_merge($relations, $orphan_relations);
|
2019-03-25 12:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'nodes' => $nodes,
|
|
|
|
'relations' => $relations,
|
|
|
|
'pure' => 1,
|
|
|
|
'no_pandora_node' => 1,
|
|
|
|
'map_options' => [
|
|
|
|
'generation_method' => LAYOUT_SPRING1,
|
|
|
|
'map_filter' => ['node_radius' => 40],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|