2015-03-27 16:28:09 +00:00
< ? php
2018-07-18 15:12:33 +03:00
use LibreNMS\Config ;
2018-04-07 15:55:28 -05:00
2015-08-06 18:36:26 +00:00
$sql = ' FROM `devices` AS D ' ;
2015-03-27 16:28:09 +00:00
2019-08-05 14:16:05 -05:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
2015-08-06 18:36:26 +00:00
$sql .= " , devices_perms AS P " ;
}
$sql .= " LEFT JOIN `poller_groups` ON `D`.`poller_group`=`poller_groups`.`id` " ;
2019-08-05 14:16:05 -05:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
$sql .= " WHERE D.device_id = P.device_id AND P.user_id = ' " . Auth :: id () . " ' AND D.ignore = '0' " ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$sql .= ' WHERE 1' ;
2015-03-27 16:28:09 +00:00
}
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
2018-03-25 22:50:09 +02:00
$sql .= " AND (hostname LIKE '% $searchPhrase %' OR sysName LIKE '% $searchPhrase %' OR last_polled LIKE '% $searchPhrase %' OR last_polled_timetaken LIKE '% $searchPhrase %') " ;
2015-03-27 16:28:09 +00:00
}
2018-03-25 22:50:09 +02:00
if ( $vars [ 'type' ] == " unpolled " ) {
2018-08-21 16:21:55 -05:00
$overdue = ( int )( Config :: get ( 'rrd.step' , 300 ) * 1.2 );
2018-06-30 02:53:24 -05:00
$sql .= " AND `last_polled` <= DATE_ADD(NOW(), INTERVAL - $overdue SECOND) " ;
2016-06-21 20:12:32 +01:00
}
2015-03-27 16:28:09 +00:00
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = 'last_polled_timetaken DESC' ;
}
2017-01-07 17:32:38 +00:00
$sql .= " AND D.status ='1' AND D.ignore='0' AND D.disabled='0' " ;
2015-05-25 21:54:57 +01:00
2015-03-27 16:47:26 +00:00
$count_sql = " SELECT COUNT(`D`.`device_id`) $sql " ;
2017-01-07 17:32:38 +00:00
$sql .= " ORDER BY $sort " ;
2015-07-13 20:10:26 +02:00
$total = dbFetchCell ( $count_sql );
2015-04-12 11:47:21 +01:00
if ( empty ( $total )) {
$total = 0 ;
}
2015-03-27 16:28:09 +00:00
if ( isset ( $current )) {
2015-07-13 20:10:26 +02:00
$limit_low = (( $current * $rowCount ) - ( $rowCount ));
2015-03-27 16:28:09 +00:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
2017-11-13 14:06:48 +00:00
$sql = " SELECT D.device_id, D.hostname AS `hostname`, D.sysName, D.last_polled AS `last_polled`, `group_name`, D.last_polled_timetaken AS `last_polled_timetaken` $sql " ;
2015-03-27 16:47:26 +00:00
2017-12-10 14:40:45 -06:00
foreach ( dbFetchRows ( $sql , array ()) as $device ) {
2015-08-06 18:36:26 +00:00
if ( empty ( $device [ 'group_name' ])) {
$device [ 'group_name' ] = 'General' ;
}
2015-07-13 20:10:26 +02:00
$response [] = array (
2017-11-13 14:06:48 +00:00
'hostname' => " <a class='list-device' href=' " . generate_device_url ( $device , array ( 'tab' => 'graphs' , 'group' => 'poller' )) . " '> " . format_hostname ( $device ) . '</a>' ,
2015-07-13 20:10:26 +02:00
'last_polled' => $device [ 'last_polled' ],
2015-08-06 18:36:26 +00:00
'poller_group' => $device [ 'group_name' ],
2020-03-10 23:14:43 +01:00
'last_polled_timetaken' => round ( $device [ 'last_polled_timetaken' ], 2 ),
2015-07-13 20:10:26 +02:00
);
2015-03-27 16:28:09 +00:00
}
2015-07-13 20:10:26 +02:00
$output = array (
'current' => $current ,
'rowCount' => $rowCount ,
'rows' => $response ,
'total' => $total ,
);
2018-09-11 07:51:35 -05:00
echo json_encode ( $output );