From 6a1bf2fbce5261804344112f100ed6e6ad6deb90 Mon Sep 17 00:00:00 2001
From: fermin831 <fermin.hernandez@artica.es>
Date: Mon, 25 Mar 2019 11:34:03 +0100
Subject: [PATCH] Added first version of live map

Former-commit-id: c440a0d0785d83c17560438986b7b394223e4779
---
 pandora_console/include/functions_netflow.php |  99 +++++++++
 .../operation/network/network_usage_map.php   | 208 ++++++++++++++++++
 2 files changed, 307 insertions(+)
 create mode 100644 pandora_console/operation/network/network_usage_map.php

diff --git a/pandora_console/include/functions_netflow.php b/pandora_console/include/functions_netflow.php
index 20176ac074..65b062068e 100644
--- a/pandora_console/include/functions_netflow.php
+++ b/pandora_console/include/functions_netflow.php
@@ -23,6 +23,7 @@ require_once $config['homedir'].'/include/functions_users.php';
 require_once $config['homedir'].'/include/functions_io.php';
 require_once $config['homedir'].'/include/functions_io.php';
 require_once $config['homedir'].'/include/functions_network.php';
+require_once $config['homedir'].'/include/class/NetworkMap.class.php';
 enterprise_include_once(
     $config['homedir'].'/enterprise/include/pdf_translator.php'
 );
@@ -1778,3 +1779,101 @@ function netflow_aggregate_is_ip($aggregate)
 {
     return in_array($aggregate, ['dstip', 'srcip']);
 }
+
+
+/**
+ * Build netflow data structure to network map.
+ *
+ * @param integer $start_date Time in timestamp format.
+ * @param integer $end_date   Time in timestamp format.
+ * @param integer $top        Max data to show.
+ *
+ * @return array With map structure.
+ */
+function netflow_build_map_data($start_date, $end_date, $top)
+{
+    $data = netflow_get_relationships_raw_data(
+        $start_date,
+        $end_date,
+        [
+            'id_name'         => '',
+            'id_group'        => 0,
+            'aggregate'       => 'srcip',
+            'id_dst'          => '',
+            'ip_src'          => '',
+            'dst_port'        => '',
+            'src_port'        => '',
+            'advanced_filter' => '',
+            'router_ip'       => '',
+        ],
+        $top,
+        'srcip'
+    );
+
+    $nodes = array_map(
+        function ($elem) {
+            return [
+                'name'   => $elem,
+                'type'   => NODE_GENERIC,
+                'width'  => 20,
+                'height' => 20,
+                'status' => '#82B92E',
+            ];
+        },
+        $data['sources']
+    );
+
+    $relations = [];
+
+    $inverse_nodes = array_flip($data['sources']);
+
+    // Port are situated in a different places from addreses.
+    $is_ip = true;
+    $src_key = ($is_ip === true) ? 3 : 5;
+    $dst_key = ($is_ip === true) ? 4 : 6;
+
+    foreach ($data['lines'] as $line) {
+        if (empty($line) === true) {
+            continue;
+        }
+
+        // Parse the line.
+        $items = explode(',', $line);
+
+        // Get the required data.
+        $src_item = $inverse_nodes[$items[$src_key]];
+        $dst_item = $inverse_nodes[$items[$dst_key]];
+        $value = $items[12];
+        $index_rel = $src_item.'-'.$dst_item;
+
+        // Check if valid data.
+        if (!isset($value) || !isset($src_item) || !isset($dst_item)) {
+            continue;
+        }
+
+        if (isset($relations[$index_rel])) {
+            $relations[$index_rel]['text_start'] += $value;
+        } else {
+            // Update the value.
+            $relations[$index_rel] = [
+                'id_parent'   => $src_item,
+                'parent_type' => NODE_GENERIC,
+                'child_type'  => NODE_GENERIC,
+                'id_child'    => $dst_item,
+                'link_color'  => '#82B92E',
+                'text_start'  => $value,
+            ];
+        }
+    }
+
+    return [
+        'nodes'           => $nodes,
+        'relations'       => $relations,
+        'pure'            => 1,
+        'no_pandora_node' => 1,
+        'map_options'     => [
+            'generation_method' => LAYOUT_SPRING1,
+            'map_filter'        => ['node_radius' => 40],
+        ],
+    ];
+}
diff --git a/pandora_console/operation/network/network_usage_map.php b/pandora_console/operation/network/network_usage_map.php
new file mode 100644
index 0000000000..e3cc5f7fca
--- /dev/null
+++ b/pandora_console/operation/network/network_usage_map.php
@@ -0,0 +1,208 @@
+<?php
+
+/**
+ * Netflow functions
+ *
+ * @package    Netflow usage map.
+ * @subpackage UI.
+ *
+ * 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.
+ */
+
+require_once $config['homedir'].'/include/functions_network.php';
+require_once $config['homedir'].'/include/class/NetworkMap.class.php';
+
+global $config;
+
+check_login();
+
+// ACL Check.
+if (! check_acl($config['id_user'], 0, 'AR')) {
+    db_pandora_audit(
+        'ACL Violation',
+        'Trying to access Network usage map.'
+    );
+    include 'general/noaccess.php';
+    exit;
+}
+
+// Include JS timepicker.
+ui_include_time_picker();
+
+?>
+<script>
+// Configure jQuery timepickers.
+$("#text-time_lower, #text-time_greater").timepicker({
+    showSecond: true,
+    timeFormat: '<?php echo TIME_FORMAT_JS; ?>',
+    timeOnlyTitle: '<?php echo __('Choose time'); ?>',
+    timeText: '<?php echo __('Time'); ?>',
+    hourText: '<?php echo __('Hour'); ?>',
+    minuteText: '<?php echo __('Minute'); ?>',
+    secondText: '<?php echo __('Second'); ?>',
+    currentText: '<?php echo __('Now'); ?>',
+    closeText: '<?php echo __('Close'); ?>'
+});
+
+$("#text-date_lower, #text-date_greater").datepicker({dateFormat: "<?php echo DATE_FORMAT_JS; ?>"});
+$.datepicker.setDefaults($.datepicker.regional[ "<?php echo get_user_language(); ?>"]);
+
+function network_report_click_period(event) {
+    var is_period = document.getElementById(event.target.id).checked;
+
+    document.getElementById('period_container').style.display = !is_period
+        ? 'none'
+        : 'block';
+    document.getElementById('end_date_container').style.display = is_period
+        ? 'none'
+        : 'block';
+}
+</script>
+
+<?php
+// Query params and other initializations.
+$time_greater = get_parameter('time_greater', date(TIME_FORMAT));
+$date_greater = get_parameter('date_greater', date(DATE_FORMAT));
+$utimestamp_greater = strtotime($date_greater.' '.$time_greater);
+$is_period = (bool) get_parameter('is_period', false);
+$period = (int) get_parameter('period', SECONDS_1HOUR);
+$time_lower = get_parameter('time_lower', date(TIME_FORMAT, ($utimestamp_greater - $period)));
+$date_lower = get_parameter('date_lower', date(DATE_FORMAT, ($utimestamp_greater - $period)));
+$utimestamp_lower = ($is_period) ? ($utimestamp_greater - $period) : strtotime($date_lower.' '.$time_lower);
+if (!$is_period) {
+    $period = ($utimestamp_greater - $utimestamp_lower);
+}
+
+$top = (int) get_parameter('top', 10);
+
+$order_by = get_parameter('order_by', 'bytes');
+if (!in_array($order_by, ['bytes', 'pkts', 'flows'])) {
+    $order_by = 'bytes';
+}
+
+$style_end = ($is_period) ? 'display: none;' : '';
+$style_period = ($is_period) ? '' : 'display: none;';
+
+// Build the table.
+$table = new stdClass();
+$table->class = 'databox';
+$table->styleTable = 'width: 100%';
+
+$table->data['0']['0'] = '<div style="display: flex;">';
+$table->data['0']['0'] .= '<div id="end_date_container" style="'.$style_end.'">';
+$table->data['0']['0'] .= __('Start date').'&nbsp;&nbsp;';
+$table->data['0']['0'] .= html_print_input_text('date_lower', $date_lower, '', 10, 7, true);
+$table->data['0']['0'] .= '&nbsp;&nbsp;';
+$table->data['0']['0'] .= html_print_input_text('time_lower', $time_lower, '', 7, 8, true);
+$table->data['0']['0'] .= '</div>';
+
+$table->data['0']['0'] .= '<div id="period_container" style="'.$style_period.'">';
+$table->data['0']['0'] .= __('Time Period').'&nbsp;&nbsp;';
+$table->data['0']['0'] .= html_print_extended_select_for_time('period', $period, '', '', 0, false, true);
+$table->data['0']['0'] .= '</div>';
+$table->data['0']['0'] .= html_print_checkbox(
+    'is_period',
+    1,
+    ($is_period === true) ? 1 : 0,
+    true,
+    false,
+    'network_report_click_period(event)'
+);
+$table->data['0']['0'] .= '</div>';
+
+$table->data['0']['1'] = __('End date').'&nbsp;&nbsp;';
+$table->data['0']['1'] .= html_print_input_text('date_greater', $date_greater, '', 10, 7, true);
+$table->data['0']['1'] .= '&nbsp;&nbsp;';
+$table->data['0']['1'] .= html_print_input_text('time_greater', $time_greater, '', 7, 8, true);
+
+$table->data['0']['2'] = __('Number of result to show').'&nbsp;&nbsp;';
+$table->data['0']['2'] .= html_print_select(
+    [
+        '5'   => 5,
+        '10'  => 10,
+        '15'  => 15,
+        '20'  => 20,
+        '25'  => 25,
+        '50'  => 50,
+        '100' => 100,
+        '250' => 250,
+    ],
+    'top',
+    $top,
+    '',
+    '',
+    0,
+    true
+);
+
+$table->data['1']['0'] = '';
+$table->data['1']['1'] = '';
+
+$netflow_button = '';
+if ((bool) $config['activate_netflow'] === true) {
+    $netflow_button = html_print_submit_button(
+        __('Show netflow map'),
+        'update_netflow',
+        false,
+        'class="sub upd"',
+        true
+    );
+}
+
+$nta_button = '';
+if ((bool) $config['activate_nta'] === true) {
+    $nta_button = html_print_submit_button(
+        __('Show NTA map'),
+        'update_nta',
+        false,
+        'class="sub upd"',
+        true
+    );
+}
+
+$table->data['1']['2'] .= implode(
+    '&nbsp;&nbsp;',
+    [
+        $netflow_button,
+        $nta_button,
+    ]
+);
+
+echo '<form method="post">';
+html_print_input_hidden('order_by', $order_by);
+
+html_print_table($table);
+echo '</form>';
+
+$has_data = true;
+if ((bool) get_parameter('update_netflow') === true) {
+    $map_data = netflow_build_map_data(
+        $utimestamp_lower,
+        $utimestamp_greater,
+        $top
+    );
+    $has_data = !empty($map_data['nodes']);
+} else if ((bool) get_parameter('update_nta') === true) {
+    // TODOS.
+    $has_data = false;
+} else {
+    return;
+}
+
+if ($has_data === true) {
+    $map_manager = new NetworkMap($map_data);
+    $map_manager->printMap();
+} else {
+    ui_print_info_message(__('No data retrieved'));
+}
+