2015-03-28 13:59:23 +00:00
< ? php
/*
* LibreNMS
*
* Copyright ( c ) 2014 Neil Lathwood < https :// github . com / laf / http :// www . lathwood . co . uk / fa >
*
* 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 , either version 3 of the License , or ( at your
* option ) any later version . Please see LICENSE . txt at the top level of
* the source code distribution for details .
*/
$tmp_devices = array ();
if ( ! empty ( $device [ 'hostname' ])) {
$sql = ' WHERE `devices`.`hostname`=?' ;
$sql_array = array ( $device [ 'hostname' ]);
2015-03-28 00:53:35 +00:00
} else {
$sql = ' WHERE 1' ;
2015-03-28 13:59:23 +00:00
}
2015-03-28 00:53:35 +00:00
$sql .= ' AND `local_device_id` != 0 AND `remote_device_id` != 0' ;
$tmp_ids = array ();
foreach ( dbFetchRows ( " SELECT DISTINCT least(`devices`.`device_id`, `remote_device_id`) AS `remote_device_id`, GREATEST(`remote_device_id`,`devices`.`device_id`) AS `local_device_id` FROM `links` LEFT JOIN `ports` ON `local_port_id`=`ports`.`port_id` LEFT JOIN `devices` ON `ports`.`device_id`=`devices`.`device_id` $sql " , $sql_array ) as $link_devices ) {
2015-04-11 10:17:27 +01:00
if ( ! in_array ( $link_devices [ 'local_device_id' ], $tmp_ids ) && device_permitted ( $link_devices [ 'local_device_id' ])) {
2015-04-17 16:24:51 +01:00
$link_dev = dbFetchRow ( " SELECT * FROM `devices` WHERE `device_id`=? " , array ( $link_devices [ 'local_device_id' ]));
$tmp_devices [] = array ( 'id' => $link_devices [ 'local_device_id' ], 'label' => $link_dev [ 'hostname' ], 'title' => generate_device_link ( $link_dev , '' , '' , '' , '' , '' , 1 , 1 ), 'group' => $link_dev [ 'location' ]);
2015-03-28 00:53:35 +00:00
}
2015-04-11 10:17:27 +01:00
if ( ! in_array ( $link_devices [ 'remote_device_id' ], $tmp_ids ) && device_permitted ( $link_devices [ 'remote_device_id' ])) {
2015-04-17 16:24:51 +01:00
$link_dev = dbFetchRow ( " SELECT * FROM `devices` WHERE `device_id`=? " , array ( $link_devices [ 'remote_device_id' ]));
$tmp_devices [] = array ( 'id' => $link_devices [ 'remote_device_id' ], 'label' => $link_dev [ 'hostname' ], 'title' => generate_device_link ( $link_dev , '' , '' , '' , '' , '' , 1 , 1 ), 'group' => $link_dev [ 'location' ]);
2015-03-28 00:53:35 +00:00
}
array_push ( $tmp_ids , $link_devices [ 'local_device_id' ]);
array_push ( $tmp_ids , $link_devices [ 'remote_device_id' ]);
2015-03-28 13:59:23 +00:00
}
2015-03-28 00:53:35 +00:00
$tmp_ids = implode ( ',' , $tmp_ids );
2015-03-28 13:59:23 +00:00
$nodes = json_encode ( $tmp_devices );
if ( is_array ( $tmp_devices [ 0 ])) {
$tmp_links = array ();
2015-04-17 16:24:51 +01:00
foreach ( dbFetchRows ( " SELECT local_device_id, remote_device_id, `remote_hostname`,`ports`.*, `remote_port` FROM `links` LEFT JOIN `ports` ON `local_port_id`=`ports`.`port_id` LEFT JOIN `devices` ON `ports`.`device_id`=`devices`.`device_id` WHERE (`local_device_id` IN ( $tmp_ids ) AND `remote_device_id` IN ( $tmp_ids )) " ) as $link_devices ) {
2015-03-28 13:59:23 +00:00
foreach ( $tmp_devices as $k => $v ) {
2015-03-29 02:11:55 +01:00
if ( $v [ 'id' ] == $link_devices [ 'local_device_id' ]) {
2015-03-28 13:59:23 +00:00
$from = $v [ 'id' ];
2015-04-17 16:24:51 +01:00
$port = shorten_interface_type ( $link_devices [ 'ifName' ]);
$port_data = $link_devices ;
2015-03-28 13:59:23 +00:00
}
2015-03-29 02:11:55 +01:00
if ( $v [ 'id' ] == $link_devices [ 'remote_device_id' ]) {
2015-03-28 13:59:23 +00:00
$to = $v [ 'id' ];
2015-04-08 02:46:03 +01:00
$port .= ' > ' . shorten_interface_type ( $link_devices [ 'remote_port' ]);
2015-03-28 13:59:23 +00:00
}
}
$speed = $link_devices [ 'ifSpeed' ] / 1000 / 1000 ;
if ( $speed == 100 ) {
$width = 3 ;
} elseif ( $speed == 1000 ) {
$width = 5 ;
} elseif ( $speed == 10000 ) {
$width = 10 ;
} elseif ( $speed == 40000 ) {
$width = 15 ;
} elseif ( $speed == 100000 ) {
$width = 20 ;
} else {
$width = 1 ;
}
2015-04-17 16:24:51 +01:00
$tmp_links [] = array ( 'from' => $from , 'to' => $to , 'label' => $port , 'title' => generate_port_link ( $port_data , " <img src='graph.php?type=port_bits&id= " . $port [ 'port_id' ] . " &from= " . $config [ 'time' ][ 'day' ] . " &to= " . $config [ 'time' ][ 'now' ] . " &width=100&height=20&legend=no&bg= " . str_replace ( " # " , " " , $row_colour ) . " '> " , '' , 1 , 1 ), 'width' => $width );
2015-03-28 13:59:23 +00:00
}
$edges = json_encode ( $tmp_links );
?>
< div id = " visualization " ></ div >
2015-03-29 02:11:55 +01:00
< script src = " js/vis.min.js " ></ script >
2015-03-28 13:59:23 +00:00
< script type = " text/javascript " >
// create an array with nodes
var nodes =
< ? php
echo $nodes ;
?>
;
// create an array with edges
var edges =
< ? php
echo $edges ;
?>
;
// create a network
var container = document . getElementById ( 'visualization' );
var data = {
nodes : nodes ,
edges : edges ,
stabilize : true
};
var options = { physics : { barnesHut : { gravitationalConstant : - 11900 , centralGravity : 1.4 , springLength : 203 , springConstant : 0.05 , damping : 0.3 }}, smoothCurves : false };
var network = new vis . Network ( container , data , options );
2015-03-28 00:53:35 +00:00
network . on ( " resize " , function ( params ) { console . log ( params . width , params . height )});
network . on ( 'click' , function ( properties ) {
if ( properties . nodes > 0 ) {
window . location . href = " /device/device= " + properties . nodes + " /tab=map/ "
}
});
2015-03-28 13:59:23 +00:00
</ script >
< ? php
}
$pagetitle [] = " Map " ;
?>