2009-10-02 13:20:24 +00:00
< ? php
2012-05-25 10:34:01 +00:00
/**
2013-10-28 11:32:21 -07:00
* LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 11:32:21 -07:00
* This file is part of LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 11:32:21 -07:00
* @ package librenms
2012-05-25 10:34:01 +00:00
* @ subpackage functions
2013-10-28 11:32:21 -07:00
* @ author LibreNMS Contributors < librenms - project @ google . groups . com >
* @ copyright ( C ) 2006 - 2012 Adam Armstrong ( as Observium )
* @ copyright ( C ) 2013 LibreNMS Group
2012-05-25 10:34:01 +00:00
*/
2018-09-11 07:51:35 -05:00
use LibreNMS\Authentication\LegacyAuth ;
2018-12-12 03:32:56 +01:00
use LibreNMS\Config ;
2017-11-18 11:33:03 +01:00
2015-09-01 10:14:15 +02:00
/**
* Compare $t with the value of $vars [ $v ], if that exists
* @ param string $v Name of the var to test
* @ param string $t Value to compare $vars [ $v ] to
* @ return boolean true , if values are the same , false if $vars [ $v ] is unset or values differ
*/
2016-08-18 20:28:22 -05:00
function var_eq ( $v , $t )
{
2015-08-27 21:06:07 +02:00
global $vars ;
if ( isset ( $vars [ $v ]) && $vars [ $v ] == $t ) {
return true ;
}
return false ;
}
2015-09-01 10:14:15 +02:00
/**
* Get the value of $vars [ $v ], if it exists
* @ param string $v Name of the var to get
* @ return string | boolean The value of $vars [ $v ] if it exists , false if it does not exist
*/
2016-08-18 20:28:22 -05:00
function var_get ( $v )
{
2015-08-27 21:06:07 +02:00
global $vars ;
if ( isset ( $vars [ $v ])) {
return $vars [ $v ];
}
return false ;
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function data_uri ( $file , $mime )
{
2015-07-13 20:10:26 +02:00
$contents = file_get_contents ( $file );
2017-12-02 22:28:03 +02:00
$base64 = base64_encode ( $contents );
return ( 'data:' . $mime . ';base64,' . $base64 );
2015-07-13 20:10:26 +02:00
} //end data_uri()
2018-12-16 15:18:17 -06:00
/**
* Convert string to nice case , mostly used for applications
*
* @ param $item
* @ return mixed | string
*/
2016-08-04 13:24:59 -05:00
function nicecase ( $item )
{
2018-12-16 15:18:17 -06:00
return \LibreNMS\Util\StringHelpers :: niceCase ( $item );
2019-01-20 19:24:11 +01:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function toner2colour ( $descr , $percent )
{
2015-07-13 20:10:26 +02:00
$colour = get_percentage_colours ( 100 - $percent );
2012-05-25 10:34:01 +00:00
2015-07-13 20:10:26 +02:00
if ( substr ( $descr , - 1 ) == 'C' || stripos ( $descr , 'cyan' ) !== false ) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = '55D6D3' ;
2015-07-13 20:10:26 +02:00
$colour [ 'right' ] = '33B4B1' ;
}
if ( substr ( $descr , - 1 ) == 'M' || stripos ( $descr , 'magenta' ) !== false ) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = 'F24AC8' ;
2015-07-13 20:10:26 +02:00
$colour [ 'right' ] = 'D028A6' ;
}
if ( substr ( $descr , - 1 ) == 'Y' || stripos ( $descr , 'yellow' ) !== false
|| stripos ( $descr , 'giallo' ) !== false
|| stripos ( $descr , 'gul' ) !== false
) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = 'FFF200' ;
2015-07-13 20:10:26 +02:00
$colour [ 'right' ] = 'DDD000' ;
}
if ( substr ( $descr , - 1 ) == 'K' || stripos ( $descr , 'black' ) !== false
|| stripos ( $descr , 'nero' ) !== false
) {
2017-12-02 22:28:03 +02:00
$colour [ 'left' ] = '000000' ;
2015-07-13 20:10:26 +02:00
$colour [ 'right' ] = '222222' ;
}
2012-04-09 23:00:31 +00:00
2015-07-13 20:10:26 +02:00
return $colour ;
} //end toner2colour()
2012-04-09 23:00:31 +00:00
2011-09-17 19:14:44 +00:00
2017-10-26 01:56:09 -05:00
/**
* Find all links in some text and turn them into html links .
*
* @ param string $text
* @ return string
*/
function linkify ( $text )
{
2018-02-07 20:48:18 -06:00
$regex = " #(http|https|ftp|ftps)://[a-z0-9 \ -.]*[a-z0-9 \ -]+(/ \ S*)?#i " ;
2017-10-26 01:56:09 -05:00
return preg_replace ( $regex , '<a href="$0">$0</a>' , $text );
}
2016-08-18 20:28:22 -05:00
function generate_link ( $text , $vars , $new_vars = array ())
{
2017-12-02 22:28:03 +02:00
return '<a href="' . generate_url ( $vars , $new_vars ) . '">' . $text . '</a>' ;
2015-07-13 20:10:26 +02:00
} //end generate_link()
2011-09-14 13:38:01 +00:00
2018-11-29 19:23:40 -06:00
function generate_url ( $vars , $new_vars = [])
2016-08-18 20:28:22 -05:00
{
2018-11-29 19:23:40 -06:00
return \LibreNMS\Util\Url :: generate ( $vars , $new_vars );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function escape_quotes ( $text )
{
2015-07-13 20:10:26 +02:00
return str_replace ( '"' , " \ ' " , str_replace ( " ' " , " \ ' " , $text ));
} //end escape_quotes()
2011-09-14 13:38:01 +00:00
2013-11-19 08:57:43 +10:00
2016-08-18 20:28:22 -05:00
function generate_overlib_content ( $graph_array , $text )
{
2011-09-17 19:14:44 +00:00
global $config ;
2017-12-02 22:28:03 +02:00
$overlib_content = '<div class=overlib><span class=overlib-text>' . $text . '</span><br />' ;
2015-07-13 20:10:26 +02:00
foreach ( array ( 'day' , 'week' , 'month' , 'year' ) as $period ) {
$graph_array [ 'from' ] = $config [ 'time' ][ $period ];
2017-12-02 22:28:03 +02:00
$overlib_content .= escape_quotes ( generate_graph_tag ( $graph_array ));
2011-09-17 19:14:44 +00:00
}
2015-07-13 20:10:26 +02:00
2013-11-19 08:57:43 +10:00
$overlib_content .= '</div>' ;
2011-09-17 19:14:44 +00:00
return $overlib_content ;
2015-07-13 20:10:26 +02:00
} //end generate_overlib_content()
2017-03-03 07:07:12 -08:00
function get_percentage_colours ( $percentage , $component_perc_warn = null )
2016-08-18 20:28:22 -05:00
{
2018-12-16 15:18:17 -06:00
return \LibreNMS\Util\Colors :: percentage ( $percentage , $component_perc_warn );
2015-07-13 20:10:26 +02:00
} //end get_percentage_colours()
2016-08-18 20:28:22 -05:00
function generate_minigraph_image ( $device , $start , $end , $type , $legend = 'no' , $width = 275 , $height = 100 , $sep = '&' , $class = 'minigraph-image' , $absolute_size = 0 )
{
2017-12-02 22:28:03 +02:00
return '<img class="' . $class . '" width="' . $width . '" height="' . $height . '" src="graph.php?' . implode ( $sep , array ( 'device=' . $device [ 'device_id' ], " from= $start " , " to= $end " , " width= $width " , " height= $height " , " type= $type " , " legend= $legend " , " absolute= $absolute_size " )) . '">' ;
2015-07-13 20:10:26 +02:00
} //end generate_minigraph_image()
2016-08-18 20:28:22 -05:00
function generate_device_url ( $device , $vars = array ())
{
2015-07-13 20:10:26 +02:00
return generate_url ( array ( 'page' => 'device' , 'device' => $device [ 'device_id' ]), $vars );
} //end generate_device_url()
2016-08-18 20:28:22 -05:00
function generate_device_link ( $device , $text = null , $vars = array (), $start = 0 , $end = 0 , $escape_text = 1 , $overlib = 1 )
{
2015-07-13 20:10:26 +02:00
global $config ;
if ( ! $start ) {
$start = $config [ 'time' ][ 'day' ];
}
if ( ! $end ) {
$end = $config [ 'time' ][ 'now' ];
}
$class = devclass ( $device );
if ( ! $text ) {
$text = $device [ 'hostname' ];
}
2017-05-05 12:25:58 +01:00
$text = format_hostname ( $device , $text );
2016-01-17 22:01:09 +00:00
2017-10-28 05:59:25 +02:00
if ( $device [ 'snmp_disable' ]) {
$graphs = $config [ 'os' ][ 'ping' ][ 'over' ];
} elseif ( isset ( $config [ 'os' ][ $device [ 'os' ]][ 'over' ])) {
2015-07-13 20:10:26 +02:00
$graphs = $config [ 'os' ][ $device [ 'os' ]][ 'over' ];
2016-08-18 20:28:22 -05:00
} elseif ( isset ( $device [ 'os_group' ]) && isset ( $config [ 'os' ][ $device [ 'os_group' ]][ 'over' ])) {
2015-07-13 20:10:26 +02:00
$graphs = $config [ 'os' ][ $device [ 'os_group' ]][ 'over' ];
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$graphs = $config [ 'os' ][ 'default' ][ 'over' ];
}
$url = generate_device_url ( $device , $vars );
// beginning of overlib box contains large hostname followed by hardware & OS details
2017-12-02 22:28:03 +02:00
$contents = '<div><span class="list-large">' . $device [ 'hostname' ] . '</span>' ;
2015-07-13 20:10:26 +02:00
if ( $device [ 'hardware' ]) {
2017-12-02 22:28:03 +02:00
$contents .= ' - ' . $device [ 'hardware' ];
2015-07-13 20:10:26 +02:00
}
if ( $device [ 'os' ]) {
2017-12-02 22:28:03 +02:00
$contents .= ' - ' . mres ( $config [ 'os' ][ $device [ 'os' ]][ 'text' ]);
2015-07-13 20:10:26 +02:00
}
if ( $device [ 'version' ]) {
2017-12-02 22:28:03 +02:00
$contents .= ' ' . mres ( $device [ 'version' ]);
2015-07-13 20:10:26 +02:00
}
if ( $device [ 'features' ]) {
2017-12-02 22:28:03 +02:00
$contents .= ' (' . mres ( $device [ 'features' ]) . ')' ;
2015-07-13 20:10:26 +02:00
}
if ( isset ( $device [ 'location' ])) {
2017-12-02 22:28:03 +02:00
$contents .= ' - ' . htmlentities ( $device [ 'location' ]);
2015-07-13 20:10:26 +02:00
}
2010-07-23 15:05:01 +00:00
$contents .= '</div>' ;
2015-07-13 20:10:26 +02:00
foreach ( $graphs as $entry ) {
2017-12-02 22:28:03 +02:00
$graph = $entry [ 'graph' ];
2015-07-13 20:10:26 +02:00
$graphhead = $entry [ 'text' ];
2015-10-02 01:43:05 +00:00
$contents .= '<div class="overlib-box">' ;
2017-12-02 22:28:03 +02:00
$contents .= '<span class="overlib-title">' . $graphhead . '</span><br />' ;
2015-07-13 20:10:26 +02:00
$contents .= generate_minigraph_image ( $device , $start , $end , $graph );
$contents .= generate_minigraph_image ( $device , $config [ 'time' ][ 'week' ], $end , $graph );
$contents .= '</div>' ;
}
if ( $escape_text ) {
$text = htmlentities ( $text );
}
if ( $overlib == 0 ) {
$link = $contents ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$link = overlib_link ( $url , $text , escape_quotes ( $contents ), $class );
}
if ( device_permitted ( $device [ 'device_id' ])) {
return $link ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
return $device [ 'hostname' ];
}
} //end generate_device_link()
2017-01-20 18:03:06 +00:00
function overlib_link ( $url , $text , $contents , $class = null )
2016-08-18 20:28:22 -05:00
{
2018-11-29 19:23:40 -06:00
return \LibreNMS\Util\Url :: overlibLink ( $url , $text , $contents , $class );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_graph_popup ( $graph_array )
{
2015-07-13 20:10:26 +02:00
global $config ;
// Take $graph_array and print day,week,month,year graps in overlib, hovered over graph
$original_from = $graph_array [ 'from' ];
2017-12-02 22:28:03 +02:00
$graph = generate_graph_tag ( $graph_array );
$content = '<div class=list-large>' . $graph_array [ 'popup_title' ] . '</div>' ;
$content .= " <div style= \ 'width: 850px \ '> " ;
2015-07-13 20:10:26 +02:00
$graph_array [ 'legend' ] = 'yes' ;
$graph_array [ 'height' ] = '100' ;
2017-12-02 22:28:03 +02:00
$graph_array [ 'width' ] = '340' ;
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
$content .= generate_graph_tag ( $graph_array );
$content .= '</div>' ;
2015-07-13 20:10:26 +02:00
$graph_array [ 'from' ] = $original_from ;
$graph_array [ 'link' ] = generate_url ( $graph_array , array ( 'page' => 'graphs' , 'height' => null , 'width' => null , 'bg' => null ));
// $graph_array['link'] = "graphs/type=" . $graph_array['type'] . "/id=" . $graph_array['id'];
return overlib_link ( $graph_array [ 'link' ], $graph , $content , null );
} //end generate_graph_popup()
2016-08-18 20:28:22 -05:00
function print_graph_popup ( $graph_array )
{
2015-07-13 20:10:26 +02:00
echo generate_graph_popup ( $graph_array );
} //end print_graph_popup()
2016-08-18 20:28:22 -05:00
function bill_permitted ( $bill_id )
{
2018-09-11 07:51:35 -05:00
if ( LegacyAuth :: user () -> hasGlobalRead ()) {
2019-03-19 08:14:01 -05:00
return true ;
2015-07-13 20:10:26 +02:00
}
2019-03-19 08:14:01 -05:00
return \Permissions :: canAccessBill ( $bill_id , LegacyAuth :: id ());
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function port_permitted ( $port_id , $device_id = null )
{
2015-07-13 20:10:26 +02:00
if ( ! is_numeric ( $device_id )) {
$device_id = get_device_id_by_port_id ( $port_id );
}
2019-03-19 08:14:01 -05:00
if ( device_permitted ( $device_id )) {
return true ;
2015-07-13 20:10:26 +02:00
}
2019-03-19 08:14:01 -05:00
return \Permissions :: canAccessPort ( $port_id , LegacyAuth :: id ());
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function application_permitted ( $app_id , $device_id = null )
{
2019-03-19 08:14:01 -05:00
if ( ! is_numeric ( $app_id )) {
return false ;
2015-07-13 20:10:26 +02:00
}
2019-03-19 08:14:01 -05:00
if ( ! $device_id ) {
$device_id = get_device_id_by_app_id ( $app_id );
}
2015-07-13 20:10:26 +02:00
2019-03-19 08:14:01 -05:00
return device_permitted ( $device_id );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function device_permitted ( $device_id )
{
2018-09-11 07:51:35 -05:00
if ( LegacyAuth :: user () -> hasGlobalRead ()) {
2019-03-19 08:14:01 -05:00
return true ;
2015-07-13 20:10:26 +02:00
}
2019-03-19 08:14:01 -05:00
return \Permissions :: canAccessDevice ( $device_id , LegacyAuth :: id ());
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function print_graph_tag ( $args )
{
2015-07-13 20:10:26 +02:00
echo generate_graph_tag ( $args );
} //end print_graph_tag()
2016-08-18 20:28:22 -05:00
function generate_graph_tag ( $args )
{
2018-11-29 19:23:40 -06:00
return \LibreNMS\Util\Url :: graphTag ( $args );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_lazy_graph_tag ( $args )
{
2018-11-29 19:23:40 -06:00
return \LibreNMS\Util\Url :: lazyGraphTag ( $args );
}
2015-07-16 12:42:58 -04:00
2018-08-29 15:25:16 +02:00
function generate_dynamic_graph_tag ( $args )
{
$urlargs = array ();
$width = 0 ;
foreach ( $args as $key => $arg ) {
switch ( strtolower ( $key )) {
case 'width' :
$width = $arg ;
$value = " { { width}} " ;
break ;
case 'from' :
$value = " { { start}} " ;
break ;
case 'to' :
$value = " { { end}} " ;
break ;
default :
$value = $arg ;
break ;
}
$urlargs [] = $key . " = " . $value ;
}
return '<img style="width:' . $width . 'px;height:100%" class="graph img-responsive" data-src-template="graph.php?' . implode ( '&' , $urlargs ) . '" border="0" />' ;
} //end generate_dynamic_graph_tag()
function generate_dynamic_graph_js ( $args )
{
$from = ( is_numeric ( $args [ 'from' ]) ? $args [ 'from' ] : '(new Date()).getTime() / 1000 - 24*3600' );
$range = ( is_numeric ( $args [ 'to' ]) ? $args [ 'to' ] - $args [ 'from' ] : '24*3600' );
$output = ' < script src = " js/RrdGraphJS/q-5.0.2.min.js " ></ script >
< script src = " js/RrdGraphJS/moment-timezone-with-data.js " ></ script >
< script src = " js/RrdGraphJS/rrdGraphPng.js " ></ script >
< script type = " text/javascript " >
q . ready ( function (){
var graphs = [];
q ( \ ' . graph\ ' ) . forEach ( function ( item ){
graphs . push (
q ( item ) . rrdGraphPng ({
canvasPadding : 120 ,
initialStart : ' . $from . ' ,
initialRange : ' . $range . '
})
);
});
});
// needed for dynamic height
window . onload = function (){ window . dispatchEvent ( new Event ( \ ' resize\ ' )); }
</ script > ' ;
return $output ;
} //end generate_dynamic_graph_js()
2010-03-11 21:58:50 +00:00
2016-08-18 20:28:22 -05:00
function generate_graph_js_state ( $args )
{
2015-07-13 20:10:26 +02:00
// we are going to assume we know roughly what the graph url looks like here.
// TODO: Add sensible defaults
2017-12-02 22:28:03 +02:00
$from = ( is_numeric ( $args [ 'from' ]) ? $args [ 'from' ] : 0 );
$to = ( is_numeric ( $args [ 'to' ]) ? $args [ 'to' ] : 0 );
$width = ( is_numeric ( $args [ 'width' ]) ? $args [ 'width' ] : 0 );
2015-07-13 20:10:26 +02:00
$height = ( is_numeric ( $args [ 'height' ]) ? $args [ 'height' ] : 0 );
$legend = str_replace ( " ' " , '' , $args [ 'legend' ]);
$state = <<< STATE
2011-06-14 14:04:56 +00:00
< script type = " text/javascript " language = " JavaScript " >
document . graphFrom = $from ;
document . graphTo = $to ;
document . graphWidth = $width ;
document . graphHeight = $height ;
document . graphLegend = '$legend' ;
</ script >
STATE ;
2015-07-13 20:10:26 +02:00
return $state ;
} //end generate_graph_js_state()
2016-08-18 20:28:22 -05:00
function print_percentage_bar ( $width , $height , $percent , $left_text , $left_colour , $left_background , $right_text , $right_colour , $right_background )
{
2018-12-16 15:18:17 -06:00
return \LibreNMS\Util\Html :: percentageBar ( $width , $height , $percent , $left_text , $left_colour , $left_background , $right_text , $right_colour , $right_background );
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_entity_link ( $type , $entity , $text = null , $graph_type = null )
{
2015-07-13 20:10:26 +02:00
global $config , $entity_cache ;
if ( is_numeric ( $entity )) {
$entity = get_entity_by_id_cache ( $type , $entity );
}
switch ( $type ) {
2016-08-18 20:28:22 -05:00
case 'port' :
$link = generate_port_link ( $entity , $text , $graph_type );
break ;
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
case 'storage' :
if ( empty ( $text )) {
$text = $entity [ 'storage_descr' ];
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
$link = generate_link ( $text , array ( 'page' => 'device' , 'device' => $entity [ 'device_id' ], 'tab' => 'health' , 'metric' => 'storage' ));
break ;
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
default :
2017-12-02 22:28:03 +02:00
$link = $entity [ $type . '_id' ];
2015-07-13 20:10:26 +02:00
}
return ( $link );
} //end generate_entity_link()
2016-08-18 20:28:22 -05:00
function generate_port_link ( $port , $text = null , $type = null , $overlib = 1 , $single_graph = 0 )
{
2015-07-13 20:10:26 +02:00
global $config ;
$graph_array = array ();
2017-04-04 08:08:23 +01:00
2015-07-13 20:10:26 +02:00
if ( ! $text ) {
2016-09-11 10:23:16 -05:00
$text = fixifName ( $port [ 'label' ]);
2015-07-13 20:10:26 +02:00
}
if ( $type ) {
$port [ 'graph_type' ] = $type ;
}
if ( ! isset ( $port [ 'graph_type' ])) {
$port [ 'graph_type' ] = 'port_bits' ;
}
$class = ifclass ( $port [ 'ifOperStatus' ], $port [ 'ifAdminStatus' ]);
if ( ! isset ( $port [ 'hostname' ])) {
$port = array_merge ( $port , device_by_id_cache ( $port [ 'device_id' ]));
}
2017-12-02 22:28:03 +02:00
$content = '<div class=list-large>' . $port [ 'hostname' ] . ' - ' . fixifName ( addslashes ( display ( $port [ 'label' ]))) . '</div>' ;
2015-07-13 20:10:26 +02:00
if ( $port [ 'ifAlias' ]) {
2017-12-02 22:28:03 +02:00
$content .= addslashes ( display ( $port [ 'ifAlias' ])) . '<br />' ;
2015-07-13 20:10:26 +02:00
}
2017-12-02 22:28:03 +02:00
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array [ 'type' ] = $port [ 'graph_type' ];
2015-07-13 20:10:26 +02:00
$graph_array [ 'legend' ] = 'yes' ;
$graph_array [ 'height' ] = '100' ;
2017-12-02 22:28:03 +02:00
$graph_array [ 'width' ] = '340' ;
$graph_array [ 'to' ] = $config [ 'time' ][ 'now' ];
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$graph_array [ 'id' ] = $port [ 'port_id' ];
$content .= generate_graph_tag ( $graph_array );
2015-07-13 20:10:26 +02:00
if ( $single_graph == 0 ) {
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
2017-12-02 22:28:03 +02:00
$content .= generate_graph_tag ( $graph_array );
2015-07-13 20:10:26 +02:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
2017-12-02 22:28:03 +02:00
$content .= generate_graph_tag ( $graph_array );
2015-07-13 20:10:26 +02:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
2017-12-02 22:28:03 +02:00
$content .= generate_graph_tag ( $graph_array );
2015-07-13 20:10:26 +02:00
}
$content .= '</div>' ;
$url = generate_port_url ( $port );
if ( $overlib == 0 ) {
return $content ;
2016-08-18 20:28:22 -05:00
} elseif ( port_permitted ( $port [ 'port_id' ], $port [ 'device_id' ])) {
2015-07-13 20:10:26 +02:00
return overlib_link ( $url , $text , $content , $class );
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
return fixifName ( $text );
}
} //end generate_port_link()
2018-12-12 03:32:56 +01:00
function generate_sensor_link ( $args , $text = null , $type = null )
{
$args = cleanPort ( $args );
if ( ! $text ) {
$text = fixIfName ( $args [ 'sensor_descr' ]);
}
if ( ! $type ) {
$args [ 'graph_type' ] = " sensor_ " . $args [ 'sensor_class' ];
} else {
$args [ 'graph_type' ] = " sensor_ " . $type ;
}
if ( ! isset ( $args [ 'hostname' ])) {
$args = array_merge ( $args , device_by_id_cache ( $args [ 'device_id' ]));
}
$content = '<div class=list-large>' . $text . '</div>' ;
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array = [
'type' => $args [ 'graph_type' ],
'legend' => 'yes' ,
'height' => '100' ,
'width' => '340' ,
'to' => Config :: get ( 'time.now' ),
'from' => Config :: get ( 'time.day' ),
'id' => $args [ 'sensor_id' ],
];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = Config :: get ( 'time.week' );
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = Config :: get ( 'time.month' );
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = Config :: get ( 'time.year' );
$content .= generate_graph_tag ( $graph_array );
$content .= '</div>' ;
$url = generate_sensor_url ( $args );
if ( port_permitted ( $args [ 'interface_id' ], $args [ 'device_id' ])) {
return overlib_link ( $url , $text , $content , null );
} else {
return fixifName ( $text );
}
} //end generate_sensor_link()
2015-07-13 20:10:26 +02:00
2018-12-12 03:32:56 +01:00
function generate_sensor_url ( $sensor , $vars = array ())
{
return generate_url ( array ( 'page' => 'graphs' , 'id' => $sensor [ 'sensor_id' ], 'type' => $sensor [ 'graph_type' ], 'from' => Config :: get ( 'time.day' )), $vars );
} //end generate_sensor_url()
2016-08-18 20:28:22 -05:00
function generate_port_url ( $port , $vars = array ())
{
2015-07-13 20:10:26 +02:00
return generate_url ( array ( 'page' => 'device' , 'device' => $port [ 'device_id' ], 'tab' => 'port' , 'port' => $port [ 'port_id' ]), $vars );
} //end generate_port_url()
2016-08-18 20:28:22 -05:00
function generate_peer_url ( $peer , $vars = array ())
{
2015-07-13 20:10:26 +02:00
return generate_url ( array ( 'page' => 'device' , 'device' => $peer [ 'device_id' ], 'tab' => 'routing' , 'proto' => 'bgp' ), $vars );
} //end generate_peer_url()
2014-03-18 22:36:22 +00:00
2016-08-18 20:28:22 -05:00
function generate_bill_url ( $bill , $vars = array ())
{
2015-07-13 20:10:26 +02:00
return generate_url ( array ( 'page' => 'bill' , 'bill_id' => $bill [ 'bill_id' ]), $vars );
} //end generate_bill_url()
2016-08-18 20:28:22 -05:00
function generate_port_image ( $args )
{
2015-07-13 20:10:26 +02:00
if ( ! $args [ 'bg' ]) {
2017-03-02 12:07:55 -06:00
$args [ 'bg' ] = 'FFFFFF00' ;
2015-07-13 20:10:26 +02:00
}
2017-12-02 22:28:03 +02:00
return " <img src='graph.php?type= " . $args [ 'graph_type' ] . '&id=' . $args [ 'port_id' ] . '&from=' . $args [ 'from' ] . '&to=' . $args [ 'to' ] . '&width=' . $args [ 'width' ] . '&height=' . $args [ 'height' ] . '&bg=' . $args [ 'bg' ] . " '> " ;
2015-07-13 20:10:26 +02:00
} //end generate_port_image()
2016-08-18 20:28:22 -05:00
function generate_port_thumbnail ( $port )
{
2015-07-13 20:10:26 +02:00
global $config ;
$port [ 'graph_type' ] = 'port_bits' ;
2017-12-02 22:28:03 +02:00
$port [ 'from' ] = $config [ 'time' ][ 'day' ];
$port [ 'to' ] = $config [ 'time' ][ 'now' ];
$port [ 'width' ] = 150 ;
$port [ 'height' ] = 21 ;
2015-07-13 20:10:26 +02:00
return generate_port_image ( $port );
} //end generate_port_thumbnail()
2016-08-18 20:28:22 -05:00
function print_port_thumbnail ( $args )
{
2015-07-13 20:10:26 +02:00
echo generate_port_link ( $args , generate_port_image ( $args ));
} //end print_port_thumbnail()
2016-08-18 20:28:22 -05:00
function print_optionbar_start ( $height = 0 , $width = 0 , $marginbottom = 5 )
{
2015-07-13 20:10:26 +02:00
echo '
2018-02-12 17:50:16 +02:00
< div class = " panel panel-default " >
< div class = " panel-heading " >
2015-07-13 20:10:26 +02:00
' ;
} //end print_optionbar_start()
2016-08-18 20:28:22 -05:00
function print_optionbar_end ()
{
2018-02-12 17:50:16 +02:00
echo '
</ div >
</ div >
' ;
2015-07-13 20:10:26 +02:00
} //end print_optionbar_end()
2016-08-18 20:28:22 -05:00
function overlibprint ( $text )
{
2017-12-02 22:28:03 +02:00
return " onmouseover= \" return overlib(' " . $text . " '); \" onmouseout= \" return nd(); \" " ;
2015-07-13 20:10:26 +02:00
} //end overlibprint()
2016-08-18 20:28:22 -05:00
function humanmedia ( $media )
{
2015-07-15 22:04:19 +01:00
global $rewrite_iftype ;
2015-07-13 20:10:26 +02:00
array_preg_replace ( $rewrite_iftype , $media );
return $media ;
} //end humanmedia()
2016-08-18 20:28:22 -05:00
function humanspeed ( $speed )
{
2015-07-13 20:10:26 +02:00
$speed = formatRates ( $speed );
if ( $speed == '' ) {
$speed = '-' ;
}
return $speed ;
} //end humanspeed()
2016-08-18 20:28:22 -05:00
function devclass ( $device )
{
2015-07-13 20:10:26 +02:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '0' ) {
$class = 'list-device-down' ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$class = 'list-device' ;
}
if ( isset ( $device [ 'ignore' ]) && $device [ 'ignore' ] == '1' ) {
$class = 'list-device-ignored' ;
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '1' ) {
$class = 'list-device-ignored-up' ;
}
}
if ( isset ( $device [ 'disabled' ]) && $device [ 'disabled' ] == '1' ) {
$class = 'list-device-disabled' ;
}
return $class ;
} //end devclass()
2016-08-18 20:28:22 -05:00
function getlocations ()
{
2018-09-11 07:51:35 -05:00
if ( LegacyAuth :: user () -> hasGlobalRead ()) {
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
return dbFetchRows ( 'SELECT id, location FROM locations ORDER BY location' );
2015-07-13 20:10:26 +02:00
}
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
return dbFetchRows ( 'SELECT id, L.location FROM devices AS D, locations AS L, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = ? AND D.location_id = L.id ORDER BY location' , [ LegacyAuth :: id ()]);
}
2015-07-13 20:10:26 +02:00
2018-07-23 11:54:02 -05:00
/**
* Get the recursive file size and count for a directory
*
* @ param string $path
* @ return array [ size , file count ]
*/
2016-08-18 20:28:22 -05:00
function foldersize ( $path )
{
2017-12-02 22:28:03 +02:00
$total_size = 0 ;
2015-07-13 20:10:26 +02:00
$total_files = 0 ;
2018-07-23 11:54:02 -05:00
foreach ( glob ( rtrim ( $path , '/' ) . '/*' , GLOB_NOSORT ) as $item ) {
if ( is_dir ( $item )) {
list ( $folder_size , $file_count ) = foldersize ( $item );
$total_size += $folder_size ;
$total_files += $file_count ;
2016-08-18 20:28:22 -05:00
} else {
2018-07-23 11:54:02 -05:00
$total_size += filesize ( $item );
2015-07-13 20:10:26 +02:00
$total_files ++ ;
}
}
2018-07-23 11:54:02 -05:00
return [ $total_size , $total_files ];
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function generate_ap_link ( $args , $text = null , $type = null )
{
2015-07-13 20:10:26 +02:00
global $config ;
2017-04-18 22:42:37 +01:00
$args = cleanPort ( $args );
2015-07-13 20:10:26 +02:00
if ( ! $text ) {
$text = fixIfName ( $args [ 'label' ]);
}
if ( $type ) {
$args [ 'graph_type' ] = $type ;
}
if ( ! isset ( $args [ 'graph_type' ])) {
$args [ 'graph_type' ] = 'port_bits' ;
}
if ( ! isset ( $args [ 'hostname' ])) {
$args = array_merge ( $args , device_by_id_cache ( $args [ 'device_id' ]));
}
2017-12-02 22:28:03 +02:00
$content = '<div class=list-large>' . $args [ 'text' ] . ' - ' . fixifName ( $args [ 'label' ]) . '</div>' ;
2015-07-13 20:10:26 +02:00
if ( $args [ 'ifAlias' ]) {
2017-12-02 22:28:03 +02:00
$content .= display ( $args [ 'ifAlias' ]) . '<br />' ;
2015-07-13 20:10:26 +02:00
}
2017-12-02 22:28:03 +02:00
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array = array ();
$graph_array [ 'type' ] = $args [ 'graph_type' ];
2015-07-13 20:10:26 +02:00
$graph_array [ 'legend' ] = 'yes' ;
$graph_array [ 'height' ] = '100' ;
2017-12-02 22:28:03 +02:00
$graph_array [ 'width' ] = '340' ;
$graph_array [ 'to' ] = $config [ 'time' ][ 'now' ];
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$graph_array [ 'id' ] = $args [ 'accesspoint_id' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
$content .= generate_graph_tag ( $graph_array );
$content .= '</div>' ;
2015-07-13 20:10:26 +02:00
$url = generate_ap_url ( $args );
if ( port_permitted ( $args [ 'interface_id' ], $args [ 'device_id' ])) {
2015-07-15 22:04:19 +01:00
return overlib_link ( $url , $text , $content , null );
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
return fixifName ( $text );
}
} //end generate_ap_link()
2016-08-18 20:28:22 -05:00
function generate_ap_url ( $ap , $vars = array ())
{
2015-07-13 20:10:26 +02:00
return generate_url ( array ( 'page' => 'device' , 'device' => $ap [ 'device_id' ], 'tab' => 'accesspoint' , 'ap' => $ap [ 'accesspoint_id' ]), $vars );
} //end generate_ap_url()
2016-08-18 20:28:22 -05:00
function report_this_text ( $message )
{
2015-07-13 20:10:26 +02:00
global $config ;
2017-12-02 22:28:03 +02:00
return $message . '\nPlease report this to the ' . $config [ 'project_name' ] . ' developers at ' . $config [ 'project_issues' ] . '\n' ;
2015-07-13 20:10:26 +02:00
} //end report_this_text()
// Find all the files in the given directory that match the pattern
2016-08-18 20:28:22 -05:00
function get_matching_files ( $dir , $match = '/\.php$/' )
{
2015-07-13 20:10:26 +02:00
global $config ;
$list = array ();
if ( $handle = opendir ( $dir )) {
while ( false !== ( $file = readdir ( $handle ))) {
if ( $file != '.' && $file != '..' && preg_match ( $match , $file ) === 1 ) {
$list [] = $file ;
}
}
closedir ( $handle );
}
return $list ;
} //end get_matching_files()
// Include all the files in the given directory that match the pattern
2016-08-18 20:28:22 -05:00
function include_matching_files ( $dir , $match = '/\.php$/' )
{
2015-07-13 20:10:26 +02:00
foreach ( get_matching_files ( $dir , $match ) as $file ) {
include_once $file ;
}
} //end include_matching_files()
2016-08-18 20:28:22 -05:00
function generate_pagination ( $count , $limit , $page , $links = 2 )
{
2017-12-02 22:28:03 +02:00
$end_page = ceil ( $count / $limit );
$start = (( $page - $links ) > 0 ) ? ( $page - $links ) : 1 ;
$end = (( $page + $links ) < $end_page ) ? ( $page + $links ) : $end_page ;
$return = '<ul class="pagination">' ;
2015-07-13 20:10:26 +02:00
$link_class = ( $page == 1 ) ? 'disabled' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li><a href='' onClick='changePage(1,event);'>«</a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page - 1,event);'><</a></li> " ;
2015-07-13 20:10:26 +02:00
if ( $start > 1 ) {
2014-10-29 18:30:42 +00:00
$return .= " <li><a href='' onClick='changePage(1,event);'>1</a></li> " ;
$return .= " <li class='disabled'><span>...</span></li> " ;
}
2015-07-13 20:10:26 +02:00
for ( $x = $start ; $x <= $end ; $x ++ ) {
$link_class = ( $page == $x ) ? 'active' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $x ,event);'> $x </a></li> " ;
2014-10-29 18:30:42 +00:00
}
2015-07-13 20:10:26 +02:00
if ( $end < $end_page ) {
2014-10-29 18:30:42 +00:00
$return .= " <li class='disabled'><span>...</span></li> " ;
$return .= " <li><a href='' onClick='changePage( $end_page ,event);'> $end_page </a></li> " ;
}
2015-07-13 20:10:26 +02:00
$link_class = ( $page == $end_page ) ? 'disabled' : '' ;
2017-12-02 22:28:03 +02:00
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page + 1,event);'>></a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $end_page ,event);'>»</a></li> " ;
$return .= '</ul>' ;
2015-07-13 20:10:26 +02:00
return ( $return );
} //end generate_pagination()
2016-08-18 20:28:22 -05:00
function demo_account ()
{
2015-02-16 23:45:28 +00:00
print_error ( " You are logged in as a demo account, this page isn't accessible to you " );
2015-07-13 20:10:26 +02:00
} //end demo_account()
2015-02-16 23:45:28 +00:00
2016-08-18 20:28:22 -05:00
function get_client_ip ()
{
2015-07-13 20:10:26 +02:00
if ( isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ])) {
2015-03-18 19:14:51 +00:00
$client_ip = $_SERVER [ 'HTTP_X_FORWARDED_FOR' ];
2016-08-18 20:28:22 -05:00
} else {
2015-03-18 19:14:51 +00:00
$client_ip = $_SERVER [ 'REMOTE_ADDR' ];
}
2015-07-13 20:10:26 +02:00
2015-03-18 19:14:51 +00:00
return $client_ip ;
2015-07-13 20:10:26 +02:00
} //end get_client_ip()
2015-04-08 02:46:03 +01:00
2017-02-01 20:15:50 +00:00
/**
* @ param $string
* @ param int $max
* @ return string
*/
function shorten_text ( $string , $max = 30 )
{
2018-12-16 15:18:17 -06:00
return \LibreNMS\Util\StringHelpers :: shortenText ( $string , $max );
2017-02-01 20:15:50 +00:00
}
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function shorten_interface_type ( $string )
{
2015-04-08 02:46:03 +01:00
return str_ireplace (
2015-07-13 20:10:26 +02:00
array (
'FastEthernet' ,
'TenGigabitEthernet' ,
'GigabitEthernet' ,
'Port-Channel' ,
'Ethernet' ,
2017-09-01 17:14:03 +01:00
'Bundle-Ether' ,
2015-07-13 20:10:26 +02:00
),
array (
'Fa' ,
'Te' ,
'Gi' ,
'Po' ,
'Eth' ,
2017-09-01 17:14:03 +01:00
'BE' ,
2015-07-13 20:10:26 +02:00
),
$string
);
} //end shorten_interface_type()
2015-04-08 02:46:03 +01:00
2015-04-13 15:32:39 +01:00
2016-08-18 20:28:22 -05:00
function clean_bootgrid ( $string )
{
2015-07-13 20:10:26 +02:00
$output = str_replace ( array ( " \r " , " \n " ), '' , $string );
2015-04-13 15:32:39 +01:00
$output = addslashes ( $output );
return $output ;
2015-07-13 20:10:26 +02:00
} //end clean_bootgrid()
2016-08-18 20:28:22 -05:00
function get_config_by_group ( $group )
{
2015-05-29 14:26:29 +01:00
$items = array ();
2018-08-19 22:44:04 +01:00
foreach ( dbFetchRows ( " SELECT * FROM `config` WHERE `config_group` = ? " , array ( $group )) as $config_item ) {
2015-05-25 18:05:11 +01:00
$val = $config_item [ 'config_value' ];
2015-07-13 20:10:26 +02:00
if ( filter_var ( $val , FILTER_VALIDATE_INT )) {
2017-12-02 22:28:03 +02:00
$val = ( int ) $val ;
2016-08-18 20:28:22 -05:00
} elseif ( filter_var ( $val , FILTER_VALIDATE_FLOAT )) {
2017-12-02 22:28:03 +02:00
$val = ( float ) $val ;
2016-08-18 20:28:22 -05:00
} elseif ( filter_var ( $val , FILTER_VALIDATE_BOOLEAN )) {
2017-12-02 22:28:03 +02:00
$val = ( boolean ) $val ;
2015-07-13 20:10:26 +02:00
}
if ( $val === true ) {
$config_item += array ( 'config_checked' => 'checked' );
2015-05-25 18:05:11 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-25 18:05:11 +01:00
$items [ $config_item [ 'config_name' ]] = $config_item ;
2015-05-17 03:20:28 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-17 03:20:28 +01:00
return $items ;
2015-07-13 20:10:26 +02:00
} //end get_config_by_group()
2015-05-17 03:20:28 +01:00
2016-08-18 20:28:22 -05:00
function get_config_like_name ( $name )
{
2015-05-29 14:26:29 +01:00
$items = array ();
2018-08-27 12:21:25 +01:00
foreach ( dbFetchRows ( " SELECT * FROM `config` WHERE `config_name` LIKE ? " , array ( " % $name % " )) as $config_item ) {
2015-11-16 16:40:23 -08:00
$items [ $config_item [ 'config_id' ]] = $config_item ;
2015-05-17 03:20:28 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-17 03:20:28 +01:00
return $items ;
2015-07-13 20:10:26 +02:00
} //end get_config_like_name()
2015-05-17 03:20:28 +01:00
2016-08-18 20:28:22 -05:00
function get_config_by_name ( $name )
{
2015-07-13 20:10:26 +02:00
$config_item = dbFetchRow ( 'SELECT * FROM `config` WHERE `config_name` = ?' , array ( $name ));
return $config_item ;
} //end get_config_by_name()
2016-08-18 20:28:22 -05:00
function set_config_name ( $name , $config_value )
{
2015-05-17 03:20:28 +01:00
return dbUpdate ( array ( 'config_value' => $config_value ), 'config' , '`config_name`=?' , array ( $name ));
2015-07-13 20:10:26 +02:00
} //end set_config_name()
2015-05-17 03:20:28 +01:00
2016-08-18 20:28:22 -05:00
function get_url ()
{
2015-05-17 03:20:28 +01:00
// http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php
// http://stackoverflow.com/users/184600/ma%C4%8Dek
return sprintf (
2015-07-13 20:10:26 +02:00
'%s://%s%s' ,
2015-05-17 03:20:28 +01:00
isset ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] != 'off' ? 'https' : 'http' ,
$_SERVER [ 'SERVER_NAME' ],
$_SERVER [ 'REQUEST_URI' ]
);
2015-07-13 20:10:26 +02:00
} //end get_url()
2015-05-17 03:20:28 +01:00
2016-08-18 20:28:22 -05:00
function alert_details ( $details )
{
2015-07-13 20:10:26 +02:00
if ( ! is_array ( $details )) {
$details = json_decode ( gzuncompress ( $details ), true );
2015-05-17 11:44:33 +00:00
}
2015-07-13 20:10:26 +02:00
2015-05-17 11:44:33 +00:00
$fault_detail = '' ;
2015-07-13 20:10:26 +02:00
foreach ( $details [ 'rule' ] as $o => $tmp_alerts ) {
2017-12-02 22:28:03 +02:00
$fallback = true ;
$fault_detail .= '#' . ( $o + 1 ) . ': ' ;
2015-07-13 20:10:26 +02:00
if ( $tmp_alerts [ 'bill_id' ]) {
2017-12-02 22:28:03 +02:00
$fault_detail .= '<a href="' . generate_bill_url ( $tmp_alerts ) . '">' . $tmp_alerts [ 'bill_name' ] . '</a>; ' ;
$fallback = false ;
2015-05-17 11:44:33 +00:00
}
2015-07-13 20:10:26 +02:00
if ( $tmp_alerts [ 'port_id' ]) {
2017-04-04 08:08:23 +01:00
$tmp_alerts = cleanPort ( $tmp_alerts );
2017-12-02 22:28:03 +02:00
$fault_detail .= generate_port_link ( $tmp_alerts ) . '; ' ;
$fallback = false ;
2015-07-13 20:10:26 +02:00
}
2017-02-15 07:55:03 +00:00
if ( $tmp_alerts [ 'accesspoint_id' ]) {
$fault_detail .= generate_ap_link ( $tmp_alerts , $tmp_alerts [ 'name' ]) . '; ' ;
2017-12-02 22:28:03 +02:00
$fallback = false ;
2017-02-15 07:55:03 +00:00
}
2018-12-12 03:32:56 +01:00
if ( $tmp_alerts [ 'sensor_id' ]) {
$details = " Current Value: " . $tmp_alerts [ 'sensor_current' ] . " ( " . $tmp_alerts [ 'sensor_class' ] . " )<br> " ;
$details_a = [];
if ( $tmp_alerts [ 'sensor_limit_low' ]) {
$details_a [] = " low: " . $tmp_alerts [ 'sensor_limit_low' ];
}
if ( $tmp_alerts [ 'sensor_limit_low_warn' ]) {
$details_a [] = " low_warn: " . $tmp_alerts [ 'sensor_limit_low_warn' ];
}
if ( $tmp_alerts [ 'sensor_limit_warn' ]) {
$details_a [] = " high_warn: " . $tmp_alerts [ 'sensor_limit_warn' ];
}
if ( $tmp_alerts [ 'sensor_limit' ]) {
$details_a [] = " high: " . $tmp_alerts [ 'sensor_limit' ];
}
$details .= implode ( ', ' , $details_a );
$fault_detail .= generate_sensor_link ( $tmp_alerts , $tmp_alerts [ 'name' ]) . '; <br>' . $details ;
$fallback = false ;
}
if ( $tmp_alerts [ 'bgpPeer_id' ]) {
// If we have a bgpPeer_id, we format the data accordingly
$fault_detail .= " BGP peer <a href=' " .
generate_url ( array ( 'page' => 'device' ,
'device' => $tmp_alerts [ 'device_id' ],
'tab' => 'routing' ,
'proto' => 'bgp' )) .
" '> " . $tmp_alerts [ 'bgpPeerIdentifier' ] . " </a> " ;
$fault_detail .= " , AS " . $tmp_alerts [ 'bgpPeerRemoteAs' ];
$fault_detail .= " , State " . $tmp_alerts [ 'bgpPeerState' ];
$fallback = false ;
}
2015-08-15 16:01:43 +10:00
if ( $tmp_alerts [ 'type' ] && $tmp_alerts [ 'label' ]) {
if ( $tmp_alerts [ 'error' ] == " " ) {
2017-12-02 22:28:03 +02:00
$fault_detail .= ' ' . $tmp_alerts [ 'type' ] . ' - ' . $tmp_alerts [ 'label' ] . '; ' ;
2016-08-18 20:28:22 -05:00
} else {
2017-12-02 22:28:03 +02:00
$fault_detail .= ' ' . $tmp_alerts [ 'type' ] . ' - ' . $tmp_alerts [ 'label' ] . ' - ' . $tmp_alerts [ 'error' ] . '; ' ;
2015-08-15 16:01:43 +10:00
}
2017-12-02 22:28:03 +02:00
$fallback = false ;
2015-08-15 16:01:43 +10:00
}
2015-07-13 20:10:26 +02:00
if ( $fallback === true ) {
foreach ( $tmp_alerts as $k => $v ) {
if ( ! empty ( $v ) && $k != 'device_id' && ( stristr ( $k , 'id' ) || stristr ( $k , 'desc' ) || stristr ( $k , 'msg' )) && substr_count ( $k , '_' ) <= 1 ) {
$fault_detail .= " $k => ' $v ', " ;
}
}
$fault_detail = rtrim ( $fault_detail , ', ' );
}
$fault_detail .= '<br>' ;
} //end foreach
2015-05-17 11:44:33 +00:00
return $fault_detail ;
2015-07-13 20:10:26 +02:00
} //end alert_details()
2015-07-23 17:48:22 +01:00
2016-08-18 20:28:22 -05:00
function dynamic_override_config ( $type , $name , $device )
{
$attrib_val = get_dev_attrib ( $device , $name );
2015-10-10 13:40:27 +00:00
if ( $attrib_val == 'true' ) {
$checked = 'checked' ;
2016-08-18 20:28:22 -05:00
} else {
2015-10-10 13:40:27 +00:00
$checked = '' ;
}
if ( $type == 'checkbox' ) {
2017-12-02 22:28:03 +02:00
return '<input type="checkbox" id="override_config" name="override_config" data-attrib="' . $name . '" data-device_id="' . $device [ 'device_id' ] . '" data-size="small" ' . $checked . '>' ;
2016-08-18 20:28:22 -05:00
} elseif ( $type == 'text' ) {
2017-12-02 22:28:03 +02:00
return '<input type="text" id="override_config_text" name="override_config_text" data-attrib="' . $name . '" data-device_id="' . $device [ 'device_id' ] . '" value="' . $attrib_val . '">' ;
2015-11-17 00:20:06 +00:00
}
2015-10-10 13:40:27 +00:00
} //end dynamic_override_config()
2015-10-12 21:28:31 +00:00
2016-08-18 20:28:22 -05:00
function generate_dynamic_config_panel ( $title , $config_groups , $items = array (), $transport = '' , $end_panel = true )
{
2015-10-12 21:28:31 +00:00
$anchor = md5 ( $title );
$output = '
< div class = " panel panel-default " >
< div class = " panel-heading " >
< h4 class = " panel-title " >
2017-12-02 22:28:03 +02:00
< a data - toggle = " collapse " data - parent = " #accordion " href = " #' . $anchor . ' " >< i class = " fa fa-caret-down " ></ i > ' . $title . ' </ a >
2015-10-12 21:28:31 +00:00
' ;
if ( ! empty ( $transport )) {
2017-12-02 22:28:03 +02:00
$output .= '<button name="test-alert" id="test-alert" type="button" data-transport="' . $transport . '" class="btn btn-primary btn-xs pull-right">Test transport</button>' ;
2015-10-12 21:28:31 +00:00
}
$output .= '
</ h4 >
</ div >
2017-12-02 22:28:03 +02:00
< div id = " ' . $anchor . ' " class = " panel-collapse collapse " >
2015-10-12 21:28:31 +00:00
< div class = " panel-body " >
' ;
if ( ! empty ( $items )) {
foreach ( $items as $item ) {
$output .= '
2018-11-19 15:04:33 -06:00
< div class = " form-group has-feedback ' . (isset( $item['class'] ) ? $item['class'] : '') . ' " >
2017-12-02 22:28:03 +02:00
< label for = " ' . $item['name'] . ' " " class= " col - sm - 4 control - label " >' . $item['descr'] . ' </label>
< div data - toggle = " tooltip " title = " ' . $config_groups[$item['name'] ]['config_descr'] . ' " class = " toolTip fa fa-fw fa-lg fa-question-circle " ></ div >
2015-10-12 21:28:31 +00:00
< div class = " col-sm-4 " >
' ;
if ( $item [ 'type' ] == 'checkbox' ) {
2017-12-02 22:28:03 +02:00
$output .= '<input id="' . $item [ 'name' ] . '" type="checkbox" name="global-config-check" ' . $config_groups [ $item [ 'name' ]][ 'config_checked' ] . ' data-on-text="Yes" data-off-text="No" data-size="small" data-config_id="' . $config_groups [ $item [ 'name' ]][ 'config_id' ] . '">' ;
2016-08-18 20:28:22 -05:00
} elseif ( $item [ 'type' ] == 'text' ) {
2018-01-17 05:21:21 +01:00
$pattern = isset ( $item [ 'pattern' ]) ? ' pattern="' . $item [ 'pattern' ] . '"' : " " ;
$pattern .= isset ( $item [ 'required' ]) && $item [ 'required' ] ? " required " : " " ;
2015-10-12 21:28:31 +00:00
$output .= '
2018-01-05 09:58:42 +01:00
< input id = " ' . $item['name'] . ' " class = " form-control validation " type = " text " name = " global-config-input " value = " ' . $config_groups[$item['name'] ]['config_value'] . ' " data - config_id = " ' . $config_groups[$item['name'] ]['config_id'] . ' " ' . $pattern . ' >
2016-09-22 10:44:17 -04:00
< span class = " form-control-feedback " >< i class = " fa " aria - hidden = " true " ></ i ></ span >
2015-10-12 21:28:31 +00:00
' ;
2016-11-24 02:55:34 -06:00
} elseif ( $item [ 'type' ] == 'password' ) {
$output .= '
2017-12-02 22:28:03 +02:00
< input id = " ' . $item['name'] . ' " class = " form-control " type = " password " name = " global-config-input " value = " ' . $config_groups[$item['name'] ]['config_value'] . ' " data - config_id = " ' . $config_groups[$item['name'] ]['config_id'] . ' " >
2016-11-24 02:55:34 -06:00
< span class = " form-control-feedback " >< i class = " fa " aria - hidden = " true " ></ i ></ span >
' ;
2016-09-06 02:01:15 +03:00
} elseif ( $item [ 'type' ] == 'numeric' ) {
$output .= '
2017-12-02 22:28:03 +02:00
< input id = " ' . $item['name'] . ' " class = " form-control " onkeypress = " return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57 " type = " text " name = " global-config-input " value = " ' . $config_groups[$item['name'] ]['config_value'] . ' " data - config_id = " ' . $config_groups[$item['name'] ]['config_id'] . ' " >
2016-09-06 02:01:15 +03:00
< span class = " glyphicon form-control-feedback " aria - hidden = " true " ></ span >
' ;
2016-08-18 20:28:22 -05:00
} elseif ( $item [ 'type' ] == 'select' ) {
2015-10-12 21:28:31 +00:00
$output .= '
2018-11-19 15:04:33 -06:00
< select id = " ' . ( $config_groups[$item['name'] ]['name'] ?: $item['name'] ) . ' " class = " form-control " name = " global-config-select " data - config_id = " ' . $config_groups[$item['name'] ]['config_id'] . ' " >
2015-10-12 21:28:31 +00:00
' ;
if ( ! empty ( $item [ 'options' ])) {
foreach ( $item [ 'options' ] as $option ) {
2016-08-23 12:37:12 -06:00
if ( gettype ( $option ) == 'string' ) {
/* for backwards-compatibility */
$tmp_opt = $option ;
$option = array (
'value' => $tmp_opt ,
'description' => $tmp_opt ,
);
}
2017-12-02 22:28:03 +02:00
$output .= '<option value="' . $option [ 'value' ] . '"' ;
2016-08-23 12:37:12 -06:00
if ( $option [ 'value' ] == $config_groups [ $item [ 'name' ]][ 'config_value' ]) {
2015-10-12 21:28:31 +00:00
$output .= ' selected' ;
}
2017-12-02 22:28:03 +02:00
$output .= '>' . $option [ 'description' ] . '</option>' ;
2015-10-12 21:28:31 +00:00
}
}
2017-12-02 22:28:03 +02:00
$output .= '
2015-10-12 21:28:31 +00:00
</ select >
2016-09-22 10:44:17 -04:00
< span class = " form-control-feedback " >< i class = " fa " aria - hidden = " true " ></ i ></ span >
2015-10-12 21:28:31 +00:00
' ;
}
$output .= '
</ div >
</ div >
' ;
}
}
if ( $end_panel === true ) {
$output .= '
</ div >
</ div >
</ div >
' ;
}
return $output ;
} //end generate_dynamic_config_panel()
2015-11-18 19:15:23 +01:00
2016-06-24 14:56:45 -07:00
/**
* Return the rows from 'ports' for all ports of a certain type as parsed by port_descr_parser .
* One or an array of strings can be provided as an argument ; if an array is passed , all ports matching
* any of the types in the array are returned .
* @ param $types mixed String or strings matching 'port_descr_type' s .
* @ return array Rows from the ports table for matching ports .
*/
2016-08-18 20:28:22 -05:00
function get_ports_from_type ( $given_types )
{
2016-06-24 14:56:45 -07:00
global $config ;
# Make the arg an array if it isn't, so subsequent steps only have to handle arrays.
2016-08-18 20:28:22 -05:00
if ( ! is_array ( $given_types )) {
2016-06-24 14:56:45 -07:00
$given_types = array ( $given_types );
}
# Check the config for a '_descr' entry for each argument. This is how a 'custom_descr' entry can
# be key/valued to some other string that's actually searched for in the DB. Merge or append the
# configured value if it's an array or a string. Or append the argument itself if there's no matching
# entry in config.
$search_types = array ();
2016-08-18 20:28:22 -05:00
foreach ( $given_types as $type ) {
2017-12-02 22:28:03 +02:00
if ( isset ( $config [ $type . '_descr' ]) === true ) {
if ( is_array ( $config [ $type . '_descr' ]) === true ) {
$search_types = array_merge ( $search_types , $config [ $type . '_descr' ]);
2016-08-18 20:28:22 -05:00
} else {
2017-12-02 22:28:03 +02:00
$search_types [] = $config [ $type . '_descr' ];
2016-06-24 14:56:45 -07:00
}
2016-08-18 20:28:22 -05:00
} else {
2016-06-24 14:56:45 -07:00
$search_types [] = $type ;
}
}
# Using the full list of strings to search the DB for, build the 'where' portion of a query that
# compares 'port_descr_type' with entry in the list. Also, since '@' is the convential wildcard,
# replace it with '%' so it functions as a wildcard in the SQL query.
$type_where = ' (' ;
$or = '' ;
$type_param = array ();
2016-08-18 20:28:22 -05:00
foreach ( $search_types as $type ) {
2016-06-24 14:56:45 -07:00
if ( ! empty ( $type )) {
2017-12-02 22:28:03 +02:00
$type = strtr ( $type , '@' , '%' );
$type_where .= " $or `port_descr_type` LIKE ? " ;
$or = 'OR' ;
$type_param [] = $type ;
2016-06-24 14:56:45 -07:00
}
}
2017-12-02 22:28:03 +02:00
$type_where .= ') ' ;
2016-06-24 14:56:45 -07:00
# Run the query with the generated 'where' and necessary parameters, and send it back.
$ports = dbFetchRows ( " SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias " , $type_param );
return $ports ;
}
2016-08-12 00:53:25 +03:00
2016-09-26 22:27:27 +01:00
/**
* @ param $filename
* @ param $content
*/
function file_download ( $filename , $content )
{
$length = strlen ( $content );
header ( 'Content-Description: File Transfer' );
header ( 'Content-Type: text/plain' );
header ( " Content-Disposition: attachment; filename= $filename " );
header ( 'Content-Transfer-Encoding: binary' );
header ( 'Content-Length: ' . $length );
header ( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header ( 'Expires: 0' );
header ( 'Pragma: public' );
echo $content ;
}
2016-10-26 06:54:48 +00:00
function get_rules_from_json ()
{
global $config ;
return json_decode ( file_get_contents ( $config [ 'install_dir' ] . '/misc/alert_rules.json' ), true );
}
2017-01-09 21:09:02 +02:00
function search_oxidized_config ( $search_in_conf_textbox )
{
global $config ;
$oxidized_search_url = $config [ 'oxidized' ][ 'url' ] . '/nodes/conf_search?format=json' ;
$postdata = http_build_query (
array (
'search_in_conf_textbox' => $search_in_conf_textbox ,
)
);
$opts = array ( 'http' =>
array (
2017-12-02 22:28:03 +02:00
'method' => 'POST' ,
'header' => 'Content-type: application/x-www-form-urlencoded' ,
2017-01-09 21:09:02 +02:00
'content' => $postdata
)
);
2017-12-02 22:28:03 +02:00
$context = stream_context_create ( $opts );
2017-01-09 21:09:02 +02:00
return json_decode ( file_get_contents ( $oxidized_search_url , false , $context ), true );
}
2017-02-03 21:20:25 +00:00
/**
* @ param $data
* @ return bool | mixed
*/
function array_to_htmljson ( $data )
{
if ( is_array ( $data )) {
$data = htmlentities ( json_encode ( $data ));
return str_replace ( ',' , ',<br />' , $data );
} else {
return false ;
}
}
2017-02-13 00:41:05 +02:00
/**
2017-03-12 08:05:31 -05:00
* @ param int $eventlog_severity
* @ return string $eventlog_severity_icon
2017-02-13 00:41:05 +02:00
*/
function eventlog_severity ( $eventlog_severity )
{
switch ( $eventlog_severity ) {
case 1 :
2017-12-24 21:56:24 +02:00
return " label-success " ; //OK
2017-02-13 00:41:05 +02:00
case 2 :
2017-12-24 21:56:24 +02:00
return " label-info " ; //Informational
2017-02-13 00:41:05 +02:00
case 3 :
2017-12-24 21:56:24 +02:00
return " label-primary " ; //Notice
2017-02-13 00:41:05 +02:00
case 4 :
2017-12-24 21:56:24 +02:00
return " label-warning " ; //Warning
2017-02-13 00:41:05 +02:00
case 5 :
2017-12-24 21:56:24 +02:00
return " label-danger " ; //Critical
2017-02-13 00:41:05 +02:00
default :
2017-12-24 21:56:24 +02:00
return " label-default " ; //Unknown
2017-02-13 00:41:05 +02:00
}
} // end eventlog_severity
2017-02-22 12:08:18 +00:00
/**
*
*/
function set_image_type ()
2017-03-14 05:39:43 +13:00
{
return header ( 'Content-type: ' . get_image_type ());
}
function get_image_type ()
2017-02-22 12:08:18 +00:00
{
global $config ;
if ( $config [ 'webui' ][ 'graph_type' ] === 'svg' ) {
2017-03-14 05:39:43 +13:00
return 'image/svg+xml' ;
2017-02-22 12:08:18 +00:00
} else {
2017-03-14 05:39:43 +13:00
return 'image/png' ;
2017-02-22 12:08:18 +00:00
}
}
2017-02-27 23:32:13 +01:00
function get_oxidized_nodes_list ()
{
global $config ;
$context = stream_context_create ( array (
'http' => array (
'header' => " Accept: application/json " ,
)
));
$data = json_decode ( file_get_contents ( $config [ 'oxidized' ][ 'url' ] . '/nodes?format=json' , false , $context ), true );
foreach ( $data as $object ) {
$device = device_by_name ( $object [ 'name' ]);
2018-10-17 18:12:25 +02:00
if ( ! device_permitted ( $device [ 'device_id' ])) {
//user cannot see this device, so let's skip it.
continue ;
}
2017-02-27 23:32:13 +01:00
$fa_color = $object [ 'status' ] == 'success' ? 'success' : 'danger' ;
echo "
< tr >
< td >
2018-10-17 18:12:25 +02:00
" . generate_device_link( $device );
if ( $device [ 'device_id' ] == 0 ) {
echo " (device ' " . $object [ 'name' ] . " ' not in LibreNMS) " ;
}
echo "
2017-02-27 23:32:13 +01:00
</ td >
< td >
2019-04-01 22:19:31 -05:00
" . $device['sysName'] . "
</ td >
< td >
2017-02-27 23:32:13 +01:00
< i class = 'fa fa-square text-" . $fa_color . "' ></ i >
</ td >
< td >
" . $object['time'] . "
</ td >
< td >
" . $object['model'] . "
</ td >
< td >
" . $object['group'] . "
</ td >
2018-09-05 02:09:27 +02:00
< td >
2018-10-17 18:12:25 +02:00
" ;
if ( ! $device [ 'device_id' ] == 0 ) {
echo "
2018-09-14 04:10:28 +02:00
< button class = 'btn btn-default btn-sm' name = 'btn-refresh-node-devId" . $device[' device_id '] . "' id = 'btn-refresh-node-devId" . $device[' device_id '] . "' onclick = 'refresh_oxidized_node(\"" . $device[' hostname '] . "\")' >
2018-09-05 02:09:27 +02:00
< i class = 'fa fa-refresh' ></ i >
</ button >
2018-09-14 04:10:28 +02:00
< a href = '" . generate_url(array(' page ' => ' device ', ' device ' => $device[' device_id '], ' tab ' => ' showconfig ')) . "' >
2018-09-05 02:09:27 +02:00
< i class = 'fa fa-align-justify fa-lg icon-theme' ></ i >
</ a >
2018-10-17 18:12:25 +02:00
" ;
} else {
echo "
< button class = 'btn btn-default btn-sm' disabled name = 'btn-refresh-node-devId" . $device[' device_id '] . "' id = 'btn-refresh-node-devId" . $device[' device_id '] . "' >
< i class = 'fa fa-refresh' ></ i >
</ button > " ;
}
echo "
2018-09-05 02:09:27 +02:00
</ td >
2017-02-27 23:32:13 +01:00
</ tr > " ;
}
}
2017-03-03 08:27:33 -06:00
// fetches disks for a system
function get_disks ( $device )
{
return dbFetchRows ( 'SELECT * FROM `ucd_diskio` WHERE device_id = ? ORDER BY diskio_descr' , array ( $device ));
}
2017-03-22 09:28:50 -05:00
2017-03-29 22:54:02 -05:00
/**
* Get the fail2ban jails for a device ... just requires the device ID
* an empty return means either no jails or fail2ban is not in use
* @ param $device_id
* @ return array
*/
function get_fail2ban_jails ( $device_id )
{
2017-12-02 22:28:03 +02:00
$options = array (
2017-03-29 22:54:02 -05:00
'filter' => array (
'type' => array ( '=' , 'fail2ban' ),
),
);
2017-12-02 22:28:03 +02:00
$component = new LibreNMS\Component ();
$f2bc = $component -> getComponents ( $device_id , $options );
2017-03-29 22:54:02 -05:00
if ( isset ( $f2bc [ $device_id ])) {
$id = $component -> getFirstComponentID ( $f2bc , $device_id );
return json_decode ( $f2bc [ $device_id ][ $id ][ 'jails' ]);
}
return array ();
}
2017-04-12 04:45:12 -05:00
/**
* Get the Postgres databases for a device ... just requires the device ID
* an empty return means Postres is not in use
* @ param $device_id
* @ return array
*/
function get_postgres_databases ( $device_id )
{
2017-12-02 22:28:03 +02:00
$options = array (
2017-04-12 04:45:12 -05:00
'filter' => array (
2017-12-02 22:28:03 +02:00
'type' => array ( '=' , 'postgres' ),
2017-04-12 04:45:12 -05:00
),
);
2017-12-02 22:28:03 +02:00
$component = new LibreNMS\Component ();
$pgc = $component -> getComponents ( $device_id , $options );
2017-04-12 04:45:12 -05:00
if ( isset ( $pgc [ $device_id ])) {
$id = $component -> getFirstComponentID ( $pgc , $device_id );
return json_decode ( $pgc [ $device_id ][ $id ][ 'databases' ]);
}
return array ();
}
2018-03-13 15:26:23 +02:00
/**
* Get all disks ( disk serial numbers ) from the collected
* rrd files .
*
* @ param array $device device for which we get the rrd ' s
* @ param int $app_id application id on the device
* @ return array list of disks
*/
2017-03-22 09:28:50 -05:00
function get_disks_with_smart ( $device , $app_id )
{
2017-12-02 22:28:03 +02:00
$disks = array ();
2018-03-13 15:26:23 +02:00
$pattern = sprintf ( '%s/%s-%s-%s-*.rrd' , get_rrd_dir ( $device [ 'hostname' ]), 'app' , 'smart' , $app_id );
foreach ( glob ( $pattern ) as $rrd ) {
$filename = basename ( $rrd , '.rrd' );
2018-03-27 02:36:38 +13:00
list (,,, $disk ) = explode ( " - " , $filename , 4 );
2018-03-13 15:26:23 +02:00
if ( $disk ) {
array_push ( $disks , $disk );
2017-03-22 09:28:50 -05:00
}
}
2018-03-13 15:26:23 +02:00
2017-03-22 09:28:50 -05:00
return $disks ;
}
2017-04-01 16:18:00 -05:00
/**
* Gets all dashboards the user can access
* adds in the keys :
* username - the username of the owner of each dashboard
* default - the default dashboard for the logged in user
*
* @ param int $user_id optionally get list for another user
* @ return array list of dashboards
*/
function get_dashboards ( $user_id = null )
{
2018-10-06 16:56:22 -05:00
$user = is_null ( $user_id ) ? Auth :: user () : \App\Models\User :: find ( $user_id );
2017-04-01 16:18:00 -05:00
$default = get_user_pref ( 'dashboard' );
2018-10-06 16:56:22 -05:00
return \App\Models\Dashboard :: allAvailable ( $user ) -> with ( 'user' ) -> get () -> map ( function ( $dashboard ) use ( $default ) {
$dash = $dashboard -> toArray ();
$dash [ 'username' ] = $dashboard -> user ? $dashboard -> user -> username : '' ;
$dash [ 'default' ] = $default == $dashboard -> dashboard_id ;
2017-04-01 16:18:00 -05:00
2018-10-06 16:56:22 -05:00
return $dash ;
}) -> keyBy ( 'dashboard_id' ) -> all ();
2017-04-01 16:18:00 -05:00
}
2017-05-03 16:51:01 -05:00
2017-12-02 22:28:03 +02:00
/**
* Return stacked graphs information
*
* @ param string $transparency value of desired transparency applied to rrdtool options ( values 01 - 99 )
* @ return array containing transparency and stacked setup
*/
function generate_stacked_graphs ( $transparency = '88' )
{
if ( Config :: get ( 'webui.graph_stacked' ) == true ) {
return array ( 'transparency' => $transparency , 'stacked' => '1' );
} else {
return array ( 'transparency' => '' , 'stacked' => '-1' );
}
}
2017-12-30 05:42:51 -06:00
/**
* Get the ZFS pools for a device ... just requires the device ID
* an empty return means ZFS is not in use or there are currently no pools
* @ param $device_id
* @ return array
*/
function get_zfs_pools ( $device_id )
{
$options = array (
'filter' => array (
'type' => array ( '=' , 'zfs' ),
),
);
$component = new LibreNMS\Component ();
$zfsc = $component -> getComponents ( $device_id , $options );
if ( isset ( $zfsc [ $device_id ])) {
$id = $component -> getFirstComponentID ( $zfsc , $device_id );
return json_decode ( $zfsc [ $device_id ][ $id ][ 'pools' ]);
}
return array ();
}
2018-02-10 16:18:53 +03:00
2018-11-22 09:05:38 -06:00
/**
* Get the ports for a device ... just requires the device ID
* an empty return means portsactivity is not in use or there are currently no ports
* @ param $device_id
* @ return array
*/
function get_portactivity_ports ( $device_id )
{
$options = array (
'filter' => array (
'type' => array ( '=' , 'portsactivity' ),
),
);
$component = new LibreNMS\Component ();
$portsc = $component -> getComponents ( $device_id , $options );
if ( isset ( $portsc [ $device_id ])) {
$id = $component -> getFirstComponentID ( $portsc , $device_id );
return json_decode ( $portsc [ $device_id ][ $id ][ 'ports' ]);
}
return array ();
}
2018-02-10 16:18:53 +03:00
/**
* Returns the sysname of a device with a html line break prepended .
* if the device has an empty sysname it will return device ' s hostname instead
* And finally if the device has no hostname it will return an empty string
2018-04-24 21:20:21 -05:00
* @ param array device
2018-02-10 16:18:53 +03:00
* @ return string
*/
function get_device_name ( $device )
{
$ret_str = '' ;
if ( format_hostname ( $device ) !== $device [ 'sysName' ]) {
$ret_str = $device [ 'sysName' ];
} elseif ( $device [ 'hostname' ] !== $device [ 'ip' ]) {
$ret_str = $device [ 'hostname' ];
}
return $ret_str ;
}
2018-12-20 22:56:54 +01:00
/**
* Returns state generic label from value with optional text
*/
function get_state_label ( $state_value , $state_text_param = null )
{
switch ( $state_value ) {
case 0 : // OK
$state_text = ( is_null ( $state_text_param ) ? " OK " : $state_text_param );
$state_label = " label-success " ;
break ;
case 1 : // Warning
$state_text = ( is_null ( $state_text_param ) ? " Warning " : $state_text_param );
$state_label = " label-warning " ;
break ;
case 2 : // Critical
$state_text = ( is_null ( $state_text_param ) ? " Critical " : $state_text_param );
$state_label = " label-danger " ;
break ;
case 3 :// Unknown
default :
$state_text = ( is_null ( $state_text_param ) ? " Unknown " : $state_text_param );
$state_label = " label-default " ;
}
$state = " <span class='label $state_label '> $state_text </span> " ;
return $state ;
}
/**
* Get state label color
*/
function get_sensor_label_color ( $sensor )
{
$current_label_color = " label-success " ;
if ( is_null ( $sensor )) {
return " label-unknown " ;
}
if ( ! is_null ( $sensor [ 'sensor_limit_warn' ]) && $sensor [ 'sensor_current' ] > $sensor [ 'sensor_limit_warn' ]) {
$current_label_color = " label-warning " ;
}
if ( ! is_null ( $sensor [ 'sensor_limit_low_warn' ]) && $sensor [ 'sensor_current' ] < $sensor [ 'sensor_limit_low_warn' ]) {
$current_label_color = " label-warning " ;
}
if ( ! is_null ( $sensor [ 'sensor_limit' ]) && $sensor [ 'sensor_current' ] > $sensor [ 'sensor_limit' ]) {
$current_label_color = " label-danger " ;
}
if ( ! is_null ( $sensor [ 'sensor_limit_low' ]) && $sensor [ 'sensor_current' ] < $sensor [ 'sensor_limit_low' ]) {
$current_label_color = " label-danger " ;
}
return $current_label_color ;
}
2019-03-20 16:17:49 +01:00
/**
* Get the unit for the sensor class given as parameter
* @ param $class
* @ return string The unit
*/
function get_unit_for_sensor_class ( $class )
{
$units_by_classes = array (
'ber' => '' ,
'charge' => '%' ,
'chromatic_dispersion' => 'ps/nm' ,
'cooling' => 'W' ,
'count' => '' ,
'current' => 'A' ,
'dbm' => 'dBm' ,
'delay' => 's' ,
'eer' => '' ,
'fanspeed' => 'rpm' ,
'frequency' => 'Hz' ,
'humidity' => '%' ,
'load' => '%' ,
'power' => 'W' ,
'power_consumed' => 'kWh' ,
'power_factor' => '' ,
'pressure' => 'kPa' ,
'quality_factor' => 'dB' ,
'signal' => 'dBm' ,
'snr' => 'dB' ,
'state' => '' ,
'temperature' => '°C' ,
'voltage' => 'V' ,
'waterflow' => 'l/m' ,
);
if ( ! array_key_exists ( $class , $units_by_classes )) {
return '' ;
}
return $units_by_classes [ $class ];
}