2017-05-01 23:49:11 -05:00
<? php
2017-12-24 22:18:48 +02:00
/*
* LibreNMS
*
* 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.
*
* @package LibreNMS
* @subpackage webui
* @link http://librenms.org
2018-03-25 22:50:09 +02:00
* @copyright 2018 LibreNMS
2017-12-24 22:18:48 +02:00
* @author LibreNMS Contributors
*/
2017-05-01 23:49:11 -05:00
2018-09-11 07:51:35 -05:00
use LibreNMS\Authentication\LegacyAuth ;
2018-04-07 15:55:28 -05:00
2018-03-25 22:50:09 +02:00
$graph_type = mres ( $vars [ 'graph_type' ]);
$unit = mres ( $vars [ 'unit' ]);
$class = mres ( $vars [ 'class' ]);
2017-05-01 23:49:11 -05:00
$sql = " FROM ` $table ` AS S, `devices` AS D" ;
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2017-05-01 23:49:11 -05:00
$sql .= ', devices_perms as P' ;
}
$sql .= " WHERE S.sensor_class=? AND S.device_id = D.device_id " ;
2018-03-25 22:50:09 +02:00
$param [] = mres ( $vars [ 'class' ]);
2017-05-01 23:49:11 -05:00
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalRead ()) {
2017-05-01 23:49:11 -05:00
$sql .= " AND D.device_id = P.device_id AND P.user_id = ?" ;
2018-09-11 07:51:35 -05:00
$param [] = LegacyAuth :: id ();
2017-05-01 23:49:11 -05:00
}
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
$sql .= " AND (`D`.`hostname` LIKE '% $searchPhrase %' OR `sensor_descr` LIKE '% $searchPhrase %' OR `sensor_current` LIKE '%searchPhrase')" ;
}
$count_sql = "SELECT COUNT(`sensor_id`) $sql " ;
$count = dbFetchCell ( $count_sql , $param );
if ( empty ( $count )) {
$count = 0 ;
}
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = '`D`.`hostname`, `S`.`sensor_descr`' ;
}
$sql .= " ORDER BY $sort " ;
if ( isset ( $current )) {
$limit_low = (( $current * $rowCount ) - ( $rowCount ));
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
$sql = "SELECT * $sql " ;
foreach ( dbFetchRows ( $sql , $param ) as $sensor ) {
2017-05-03 16:46:05 -05:00
$alert = '' ;
2017-05-01 23:49:11 -05:00
if ( ! isset ( $sensor [ 'sensor_current' ])) {
$sensor [ 'sensor_current' ] = 'NaN' ;
2017-05-03 16:46:05 -05:00
} elseif (( ! is_null ( $sensor [ 'sensor_limit' ]) && $sensor [ 'sensor_current' ] >= $sensor [ 'sensor_limit' ]) ||
( ! is_null ( $sensor [ 'sensor_limit_low' ]) && $sensor [ 'sensor_current' ] <= $sensor [ 'sensor_limit_low' ])
) {
$alert = '<i class="fa fa-flag fa-lg" style="color:red" aria-hidden="true"></i>' ;
2017-05-01 23:49:11 -05:00
}
// FIXME - make this "four graphs in popup" a function/include and "small graph" a function.
// FIXME - So now we need to clean this up and move it into a function. Isn't it just "print-graphrow"?
// FIXME - DUPLICATED IN device/overview/sensors
$graph_colour = str_replace ( '#' , '' , $row_colour );
$graph_array = array ();
$graph_array [ 'height' ] = '100' ;
$graph_array [ 'width' ] = '210' ;
$graph_array [ 'to' ] = $config [ 'time' ][ 'now' ];
$graph_array [ 'id' ] = $sensor [ 'sensor_id' ];
$graph_array [ 'type' ] = $graph_type ;
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$graph_array [ 'legend' ] = 'no' ;
$link_array = $graph_array ;
$link_array [ 'page' ] = 'graphs' ;
unset ( $link_array [ 'height' ], $link_array [ 'width' ], $link_array [ 'legend' ]);
$link_graph = generate_url ( $link_array );
$link = generate_url ( array ( 'page' => 'device' , 'device' => $sensor [ 'device_id' ], 'tab' => $tab , 'metric' => $sensor [ 'sensor_class' ]));
$overlib_content = '<div style="width: 580px;"><h2>' . $sensor [ 'hostname' ] . ' - ' . $sensor [ 'sensor_descr' ] . '</h1>' ;
foreach ( array ( 'day' , 'week' , 'month' , 'year' ) as $period ) {
$graph_array [ 'from' ] = $config [ 'time' ][ $period ];
2017-05-03 16:46:05 -05:00
$overlib_content .= str_replace ( '"' , " \\ '" , generate_graph_tag ( $graph_array ));
2017-05-01 23:49:11 -05:00
}
$overlib_content .= '</div>' ;
$graph_array [ 'width' ] = 80 ;
$graph_array [ 'height' ] = 20 ;
$graph_array [ 'bg' ] = 'ffffff00' ;
// the 00 at the end makes the area transparent.
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$sensor_minigraph = generate_lazy_graph_tag ( $graph_array );
$sensor [ 'sensor_descr' ] = substr ( $sensor [ 'sensor_descr' ], 0 , 48 );
2018-12-20 22:56:54 +01:00
if ( $graph_type == 'sensor_state' ) {
// If we have a state, let's display a label with textual state translation
$state_translation = array ();
$state_translation = dbFetchRows ( 'SELECT * FROM state_translations as ST, sensors_to_state_indexes as SSI WHERE ST.state_index_id=SSI.state_index_id AND SSI.sensor_id = ? AND ST.state_value = ? ' , array ( $sensor [ 'sensor_id' ], $sensor [ 'sensor_current' ]));
//$current_label = get_state_label_color($sensor);
$sensor_current = get_state_label ( $sensor [ 'state_generic_value' ], ( ! empty ( $state_translation [ '0' ][ 'state_descr' ])) ? $state_translation [ 0 ][ 'state_descr' ] . " (" . $sensor [ 'sensor_current' ] . ")" : $sensor [ 'sensor_current' ]);
} else {
// we have another sensor
$current_label = get_sensor_label_color ( $sensor );
2018-12-21 02:50:36 +01:00
$sensor_current = "<span class='label $current_label '>" . format_si ( $sensor [ 'sensor_current' ]) . $unit . "</span>" ;
2018-12-20 22:56:54 +01:00
}
2017-05-01 23:49:11 -05:00
$response [] = array (
2017-05-05 12:18:05 -05:00
'hostname' => generate_device_link ( $sensor ),
'sensor_descr' => overlib_link ( $link , $sensor [ 'sensor_descr' ], $overlib_content , null ),
'graph' => overlib_link ( $link_graph , $sensor_minigraph , $overlib_content , null ),
'alert' => $alert ,
2018-12-20 22:56:54 +01:00
'sensor_current' => $sensor_current ,
2017-05-05 12:18:05 -05:00
'sensor_limit_low' => is_null ( $sensor [ 'sensor_limit_low' ]) ? '-' : round ( $sensor [ 'sensor_limit_low' ], 2 ) . $unit ,
'sensor_limit' => is_null ( $sensor [ 'sensor_limit' ]) ? '-' : round ( $sensor [ 'sensor_limit' ], 2 ) . $unit ,
2017-05-01 23:49:11 -05:00
);
2018-03-25 22:50:09 +02:00
if ( $vars [ 'view' ] == 'graphs' ) {
2017-05-01 23:49:11 -05:00
$daily_graph = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'day' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=211&height=100' ;
$daily_url = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'day' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=400&height=150' ;
$weekly_graph = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'week' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=211&height=100' ;
$weekly_url = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'week' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=400&height=150' ;
$monthly_graph = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'month' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=211&height=100' ;
$monthly_url = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'month' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=400&height=150' ;
$yearly_graph = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'year' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=211&height=100' ;
$yearly_url = 'graph.php?id=' . $sensor [ 'sensor_id' ] . '&type=' . $graph_type . '&from=' . $config [ 'time' ][ 'year' ] . '&to=' . $config [ 'time' ][ 'now' ] . '&width=400&height=150' ;
$response [] = array (
'hostname' => "<a onmouseover= \" return overlib('<img src=\' $daily_url \'>', LEFT); \" onmouseout= \" return nd(); \" >
<img src=' $daily_graph ' border=0></a> " ,
'sensor_descr' => "<a onmouseover= \" return overlib('<img src=\' $weekly_url \'>', LEFT); \" onmouseout= \" return nd(); \" >
<img src=' $weekly_graph ' border=0></a> " ,
'graph' => "<a onmouseover= \" return overlib('<img src=\' $monthly_url \'>', LEFT); \" onmouseout= \" return nd(); \" >
<img src=' $monthly_graph ' border=0></a>" ,
'alert' => '' ,
'sensor_current' => "<a onmouseover= \" return overlib('<img src=\' $yearly_url \'>', LEFT); \" onmouseout= \" return nd(); \" >
<img src=' $yearly_graph ' border=0></a>" ,
'sensor_range' => '' ,
);
} //end if
} //end foreach
$output = array (
'current' => $current ,
'rowCount' => $rowCount ,
'rows' => $response ,
'total' => $count ,
);
echo _json_encode ( $output );