From 3484581dd6ca98be6d106bd6779d1d7fe425142f Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 1 Aug 2015 18:14:55 +0000 Subject: [PATCH] Updated network to support old style format, docs provided --- doc/Support/Configuration.md | 5 + html/network-map.php | 254 ++++++++++++++++++++++++++++++++++ html/pages/device/map.inc.php | 11 +- html/pages/map.inc.php | 8 +- includes/defaults.inc.php | 3 + 5 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 html/network-map.php diff --git a/doc/Support/Configuration.md b/doc/Support/Configuration.md index e2288aef21..f70b9f3e76 100644 --- a/doc/Support/Configuration.md +++ b/doc/Support/Configuration.md @@ -169,6 +169,11 @@ $config['enable_footer'] = 1; ``` Disable the footer of the WebUI by setting `enable_footer` to 0. +You can enable the old style network map (only available for individual devices with links discovered via xDP) by setting: +```php +$config['gui']['network-map']['style'] = 'old'; +```` + #### Add host settings The following setting controls how hosts are added. If a host is added as an ip address it is checked to ensure the ip is not already present. If the ip is present the host is not added. If host is added by hostname this check is not performed. If the setting is true hostnames are resovled and the check is also performed. This helps prevents accidental duplicate hosts. diff --git a/html/network-map.php b/html/network-map.php new file mode 100644 index 0000000000..7907eee546 --- /dev/null +++ b/html/network-map.php @@ -0,0 +1,254 @@ + + * @copyright (C) 2006 - 2012 Adam Armstrong + * + */ + +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); +ini_set('log_errors', 1); +ini_set('error_reporting', E_ALL); + +$links = 1; + +include_once '../includes/defaults.inc.php'; +include_once '../config.php'; +include_once '../includes/definitions.inc.php'; +include_once '../includes/functions.php'; +include_once '../includes/dbFacile.php'; +include_once 'includes/functions.inc.php'; +include_once 'includes/authenticate.inc.php'; + +if (strpos($_SERVER['REQUEST_URI'], 'anon')) { + $anon = 1; +} + +if (is_array($config['branding'])) { + if ($config['branding'][$_SERVER['SERVER_NAME']]) { + foreach ($config['branding'][$_SERVER['SERVER_NAME']] as $confitem => $confval) { + eval("\$config['" . $confitem . "'] = \$confval;"); + } + } + else { + foreach ($config['branding']['default'] as $confitem => $confval) { + eval("\$config['" . $confitem . "'] = \$confval;"); + } + } +} + +if (isset($_GET['device'])) { + $where = 'WHERE device_id = '.mres($_GET['device']); +} +else { + $where = ''; +} + +// FIXME this shit probably needs tidied up. + +if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format'])) { + $map = ' + digraph G { bgcolor=transparent; splines=true; overlap=scale; concentrate=0; epsilon=0.001; rankdir=LR; + node [ fontname="helvetica", fontstyle=bold, style=filled, color=white, fillcolor=lightgrey, overlap=false]; + edge [ bgcolor=white, fontname="helvetica", fontstyle=bold, arrowhead=dot, arrowtail=dot]; + graph [bgcolor=transparent]; +'; + + if (!$_SESSION['authenticated']) { + $map .= "\"Not authenticated\" [fontsize=20 fillcolor=\"lightblue\", URL=\"/\" shape=box3d]\n"; + } + else { + $loc_count = 1; + + foreach (dbFetch("SELECT * from devices ".$where) as $device) { + if ($device) { + $links = dbFetch("SELECT * from ports AS I, links AS L WHERE I.device_id = ? AND L.local_port_id = I.port_id ORDER BY L.remote_hostname", array($device['device_id'])); + if (count($links)) { + if ($anon) { + $device['hostname'] = md5($device['hostname']); + } + if (!isset($locations[$device['location']])) { + $locations[$device['location']] = $loc_count; $loc_count++; + } + $loc_id = $locations[$device['location']]; + + $map .= "\"".$device['hostname']."\" [fontsize=20, fillcolor=\"lightblue\", group=".$loc_id." URL=\"{$config['base_url']}/device/device=".$device['device_id']."/tab=map/\" shape=box3d]\n"; + } + + foreach ($links as $link) { + $local_port_id = $link['local_port_id']; + $remote_port_id = $link['remote_port_id']; + + $i = 0; $done = 0; + if ($linkdone[$remote_port_id][$local_port_id]) { + $done = 1; + } + + if (!$done) { + $linkdone[$local_port_id][$remote_port_id] = TRUE; + + $links++; + + if ($link['ifSpeed'] >= "10000000000") { + $info = "color=red3 style=\"setlinewidth(6)\""; + } + elseif ($link['ifSpeed'] >= "1000000000") { + $info = "color=lightblue style=\"setlinewidth(4)\""; + } + elseif ($link['ifSpeed'] >= "100000000") { + $info = "color=lightgrey style=\"setlinewidth(2)\""; + } + elseif ($link['ifSpeed'] >= "10000000") { + $info = "style=\"setlinewidth(1)\""; + } + else { + $info = "style=\"setlinewidth(1)\""; + } + + $src = $device['hostname']; + if ($anon) { + $src = md5($src); + } + if ($remote_port_id) { + $dst = dbFetchCell("SELECT `hostname` FROM `devices` AS D, `ports` AS I WHERE I.port_id = ? AND D.device_id = I.device_id", array($remote_port_id)); + $dst_host = dbFetchCell("SELECT D.device_id FROM `devices` AS D, `ports` AS I WHERE I.port_id = ? AND D.device_id = I.device_id", array($remote_port_id)); + } + else { + unset($dst_host); + $dst = $link['remote_hostname']; + } + + if ($anon) { + $dst = md5($dst); + $src = md5($src); + } + + $sif = ifNameDescr(dbFetchRow("SELECT * FROM ports WHERE `port_id` = ?", array($link['local_port_id'])),$device); + if ($remote_port_id) { + $dif = ifNameDescr(dbFetchRow("SELECT * FROM ports WHERE `port_id` = ?", array($link['remote_port_id']))); + } + else { + $dif['label'] = $link['remote_port']; + $dif['port_id'] = $link['remote_hostname'] . '/' . $link['remote_port']; + } + + if ($where == "") { + if (!$ifdone[$dst][$dif['port_id']] && !$ifdone[$src][$sif['port_id']]) { + $map .= "\"$src\" -> \"" . $dst . "\" [weight=500000, arrowsize=0, len=0];\n"; + } + $ifdone[$src][$sif['port_id']] = 1; + } + else { + $map .= "\"" . $sif['port_id'] . "\" [label=\"" . $sif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"{$config['base_url']}/device/device=".$device['device_id']."/tab=port/port=$local_port_id/\"]\n"; + if (!$ifdone[$src][$sif['port_id']]) { + $map .= "\"$src\" -> \"" . $sif['port_id'] . "\" [weight=500000, arrowsize=0, len=0];\n"; + $ifdone[$src][$sif['port_id']] = 1; + } + + if ($dst_host) { + $map .= "\"$dst\" [URL=\"{$config['base_url']}/device/device=$dst_host/tab=map/\", fontsize=20, shape=box3d]\n"; + } + else { + $map .= "\"$dst\" [ fontsize=20 shape=box3d]\n"; + } + + if ($dst_host == $device['device_id'] || $where == '') { + $map .= "\"" . $dif['port_id'] . "\" [label=\"" . $dif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"{$config['base_url']}/device/device=$dst_host/tab=port/port=$remote_port_id/\"]\n"; + } + else { + $map .= "\"" . $dif['port_id'] . "\" [label=\"" . $dif['label'] . " \", fontsize=12, fillcolor=lightgray"; + if ($dst_host) { + $map .= ", URL=\"{$config['base_url']}/device/device=$dst_host/tab=port/port=$remote_port_id/\""; + } + $map .= "]\n"; + } + + if (!$ifdone[$dst][$dif['port_id']]) { + $map .= "\"" . $dif['port_id'] . "\" -> \"$dst\" [weight=500000, arrowsize=0, len=0];\n"; + $ifdone[$dst][$dif['port_id']] = 1; + } + $map .= "\"" . $sif['port_id'] . "\" -> \"" . $dif['port_id'] . "\" [weight=1, arrowhead=normal, arrowtail=normal, len=2, $info] \n"; + } + } + } + $done = 0; + } + } + } + + $map .= "\n};"; + + if ($_GET['debug'] == 1) { + echo '
$map
'; + exit(); + } + + switch ($_GET['format']) { + case 'svg': + break; + case 'png': + $_GET['format'] = 'png:gd'; + break; + case 'dot': + echo($map); + exit(); + default: + $_GET['format'] = 'png:gd'; + } + + if ($links > 30) { + // Unflatten if there are more than 10 links. beyond that it gets messy + $maptool = $config['dot']; + } + else { + $maptool = $config['dot']; + } + + if ($where == '') { +# $maptool = $config['unflatten'] . ' -f -l 5 | ' . $config['sfdp'] . ' -Gpack -Goverlap=prism -Gcharset=latin1 | dot'; + $maptool = $config['sfdp'] . ' -Gpack -Goverlap=prism -Gcharset=latin1 -Gsize=20,20'; + $maptool = $config['dot']; + } + + $descriptorspec = array(0 => array("pipe", "r"),1 => array("pipe", "w") ); + + $mapfile = $config['temp_dir'] . "/" . strgen() . ".png"; + + $process = proc_open($maptool.' -T'.$_GET['format'],$descriptorspec,$pipes); + + if (is_resource($process)) { + fwrite($pipes[0], "$map"); + fclose($pipes[0]); + while (! feof($pipes[1])) { + $img .= fgets($pipes[1]); + } + fclose($pipes[1]); + $return_value = proc_close($process); + } + + if ($_GET['format'] == "png:gd") { + header("Content-type: image/png"); + } + elseif ($_GET['format'] == "svg") { + header("Content-type: image/svg+xml"); + $img = str_replace(" + + + '; + } +} + diff --git a/html/pages/device/map.inc.php b/html/pages/device/map.inc.php index 918f62e7fc..6d6b56392a 100644 --- a/html/pages/device/map.inc.php +++ b/html/pages/device/map.inc.php @@ -14,4 +14,13 @@ $pagetitle[] = 'Map'; -require_once 'includes/print-map.inc.php'; +if ($config['gui']['network-map']['style'] == 'old') { + echo ' +
+ +
+ '; +} +else { + require_once 'includes/print-map.inc.php'; +} diff --git a/html/pages/map.inc.php b/html/pages/map.inc.php index 115c0600c3..8abc8fecf6 100644 --- a/html/pages/map.inc.php +++ b/html/pages/map.inc.php @@ -11,4 +11,10 @@ * the source code distribution for details. */ $pagetitle[] = 'Map'; -require_once 'includes/print-map.inc.php'; + +if ($config['gui']['network-map']['style'] == 'old') { + print_error('You are using the old style network map, a global map is not available'); +} +else { + require_once 'includes/print-map.inc.php'; +} diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index 11d8d9475a..577b8730dc 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -794,5 +794,8 @@ $config['leaflet']['default_lat'] = '50.898482'; $config['leaflet']['default_lng'] = '-3.401402'; $config['leaflet']['default_zoom'] = 2; +// General GUI options +$config['gui']['network-map']['style'] = 'new';//old is also valid + // Navbar variables $config['navbar']['manage_groups']['hide'] = 0;