2015-04-08 21:17:48 +01:00
<? php
2017-12-24 21:58:32 +02:00
/*
* 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
* @copyright 2017 LibreNMS
* @author LibreNMS Contributors
*/
2015-04-08 21:17:48 +01:00
$where = 1 ;
$param = array ();
2015-07-13 20:10:26 +02:00
$sql = ' FROM `devices`' ;
2015-05-05 11:29:59 +01:00
2015-07-13 20:10:26 +02:00
if ( is_admin () === false && is_read () === false ) {
2016-10-16 17:58:48 +03:00
$sql .= ' LEFT JOIN `devices_perms` AS `DP` ON `devices`.`device_id` = `DP`.`device_id`' ;
$where .= ' AND `DP`.`user_id`=?' ;
2015-05-13 15:49:50 +01:00
$param [] = $_SESSION [ 'user_id' ];
}
2015-05-05 11:29:59 +01:00
2015-05-13 15:49:50 +01:00
if ( ! empty ( $_POST [ 'location' ])) {
2015-05-05 11:29:59 +01:00
$sql .= " LEFT JOIN `devices_attribs` AS `DB` ON `DB`.`device_id`=`devices`.`device_id` AND `DB`.`attrib_type`='override_sysLocation_bool' AND `DB`.`attrib_value`='1' LEFT JOIN `devices_attribs` AS `DA` ON `devices`.`device_id`=`DA`.`device_id`" ;
}
2017-11-19 15:54:09 -06:00
if ( ! empty ( $_POST [ 'group' ]) && is_numeric ( $_POST [ 'group' ])) {
$sql .= " LEFT JOIN `device_group_device` AS `DG` ON `DG`.`device_id`=`devices`.`device_id`" ;
$where .= " AND `DG`.`device_group_id`=?" ;
$param [] = $_POST [ 'group' ];
}
2015-05-05 11:29:59 +01:00
$sql .= " WHERE $where " ;
2015-04-08 21:17:48 +01:00
2015-07-13 20:10:26 +02:00
if ( ! empty ( $_POST [ 'hostname' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND hostname LIKE ?' ;
$param [] = '%' . $_POST [ 'hostname' ] . '%' ;
2015-04-08 21:17:48 +01:00
}
2015-07-13 20:10:26 +02:00
if ( ! empty ( $_POST [ 'os' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND os = ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'os' ];
}
if ( ! empty ( $_POST [ 'version' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND version = ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'version' ];
}
if ( ! empty ( $_POST [ 'hardware' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND hardware = ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'hardware' ];
}
if ( ! empty ( $_POST [ 'features' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND features = ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'features' ];
}
if ( ! empty ( $_POST [ 'type' ])) {
if ( $_POST [ 'type' ] == 'generic' ) {
2016-10-16 17:58:48 +03:00
$sql .= " AND ( type = ? OR type = '')" ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'type' ];
2016-08-18 20:28:22 -05:00
} else {
2016-10-16 17:58:48 +03:00
$sql .= ' AND type = ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'type' ];
}
}
2015-04-08 21:17:48 +01:00
if ( ! empty ( $_POST [ 'state' ])) {
2015-07-13 20:10:26 +02:00
$sql .= ' AND status= ?' ;
if ( is_numeric ( $_POST [ 'state' ])) {
2015-05-27 16:06:47 +00:00
$param [] = $_POST [ 'state' ];
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$param [] = str_replace ( array ( 'up' , 'down' ), array ( 1 , 0 ), $_POST [ 'state' ]);
}
}
if ( ! empty ( $_POST [ 'disabled' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND disabled= ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'disabled' ];
}
if ( ! empty ( $_POST [ 'ignore' ])) {
2016-10-16 17:58:48 +03:00
$sql .= ' AND `ignore`= ?' ;
2015-07-13 20:10:26 +02:00
$param [] = $_POST [ 'ignore' ];
}
if ( ! empty ( $_POST [ 'location' ]) && $_POST [ 'location' ] == 'Unset' ) {
$location_filter = '' ;
2015-04-08 21:17:48 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-05 11:29:59 +01:00
if ( ! empty ( $_POST [ 'location' ])) {
2016-10-16 17:58:48 +03:00
$sql .= " AND `location` = ?" ;
2015-08-09 21:17:54 +00:00
$param [] = $_POST [ 'location' ];
2015-05-05 11:29:59 +01:00
}
2015-07-13 20:10:26 +02:00
2015-05-05 11:29:59 +01:00
$count_sql = "SELECT COUNT(`devices`.`device_id`) $sql " ;
2015-04-08 21:17:48 +01:00
2017-11-19 15:54:09 -06:00
$total = ( int ) dbFetchCell ( $count_sql , $param );
2015-04-08 21:17:48 +01:00
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = '`hostname` DESC' ;
}
$sql .= " ORDER BY $sort " ;
if ( isset ( $current )) {
2016-10-16 17:58:48 +03:00
$limit_low = (( $current * $rowCount ) - ( $rowCount ));
2015-04-08 21:17:48 +01:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
2015-05-05 23:54:36 +01:00
$sql = "SELECT DISTINCT(`devices`.`device_id`),`devices`.* $sql " ;
2015-04-08 21:17:48 +01:00
if ( ! isset ( $_POST [ 'format' ])) {
2015-07-13 20:10:26 +02:00
$_POST [ 'format' ] = 'list_detail' ;
2015-04-08 21:17:48 +01:00
}
2015-07-13 20:10:26 +02:00
list ( $format , $subformat ) = explode ( '_' , $_POST [ 'format' ]);
2015-04-08 21:17:48 +01:00
foreach ( dbFetchRows ( $sql , $param ) as $device ) {
2015-07-13 20:10:26 +02:00
if ( isset ( $bg ) && $bg == $list_colour_b ) {
$bg = $list_colour_a ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$bg = $list_colour_b ;
}
if ( $device [ 'status' ] == '0' ) {
2017-12-24 21:58:32 +02:00
$extra = 'label-danger' ;
2016-08-18 20:28:22 -05:00
} else {
2017-12-24 21:58:32 +02:00
$extra = 'label-success' ;
2015-07-13 20:10:26 +02:00
}
if ( $device [ 'ignore' ] == '1' ) {
2017-12-24 21:58:32 +02:00
$extra = 'label-default' ;
2016-10-16 17:58:48 +03:00
$msg = 'ignored' ;
2015-07-13 20:10:26 +02:00
if ( $device [ 'status' ] == '1' ) {
2017-12-24 21:58:32 +02:00
$extra = 'label-warning' ;
2015-07-13 20:10:26 +02:00
}
}
if ( $device [ 'disabled' ] == '1' ) {
2017-12-24 21:58:32 +02:00
$extra = 'label-default' ;
2015-07-13 20:10:26 +02:00
}
2016-10-16 17:58:48 +03:00
$type = strtolower ( $device [ 'os' ]);
2017-01-24 16:16:01 -06:00
$image = getIconTag ( $device );
2015-07-13 20:10:26 +02:00
if ( $device [ 'os' ] == 'ios' ) {
formatCiscoHardware ( $device , true );
}
$device [ 'os_text' ] = $config [ 'os' ][ $device [ 'os' ]][ 'text' ];
2016-10-16 17:58:48 +03:00
$port_count = dbFetchCell ( 'SELECT COUNT(*) FROM `ports` WHERE `device_id` = ?' , array ( $device [ 'device_id' ]));
$sensor_count = dbFetchCell ( 'SELECT COUNT(*) FROM `sensors` WHERE `device_id` = ?' , array ( $device [ 'device_id' ]));
2017-05-01 23:49:11 -05:00
$wireless_count = dbFetchCell ( 'SELECT COUNT(*) FROM `wireless_sensors` WHERE `device_id` = ?' , array ( $device [ 'device_id' ]));
2016-10-16 17:58:48 +03:00
$actions = '
<div class="container-fluid">
<div class="row">
2017-01-21 13:24:05 +02:00
<div class="col-xs-1"><a href="' . generate_device_url ( $device ) . '"> <i class="fa fa-id-card fa-lg icon-theme" title="View device"></i></a></div>
<div class="col-xs-1"><a href="' . generate_device_url ( $device , array ( 'tab' => 'alerts' )) . '"> <i class="fa fa-exclamation-circle fa-lg icon-theme" title="View alerts"></i></a></div>
2016-10-16 17:58:48 +03:00
' ;
2015-07-13 20:10:26 +02:00
if ( $_SESSION [ 'userlevel' ] >= '7' ) {
2017-01-21 13:24:05 +02:00
$actions .= '<div class="col-xs-1"><a href="' . generate_device_url ( $device , array ( 'tab' => 'edit' )) . '"> <i class="fa fa-pencil fa-lg icon-theme" title="Edit device"></i></a></div>' ;
2015-07-13 20:10:26 +02:00
}
2016-10-16 17:58:48 +03:00
if ( $subformat == 'detail' ) {
$actions .= '</div><div class="row">' ;
}
2015-07-13 20:10:26 +02:00
2016-10-16 17:58:48 +03:00
$actions .= '
2017-01-21 13:24:05 +02:00
<div class="col-xs-1"><a href="telnet://' . $device [ 'hostname' ] . '"><i class="fa fa-terminal fa-lg icon-theme" title="Telnet to ' . $device [ 'hostname' ] . '"></i></a></div>
<div class="col-xs-1"><a href="ssh://' . $device [ 'hostname' ] . '"><i class="fa fa-lock fa-lg icon-theme" title="SSH to ' . $device [ 'hostname' ] . '"></i></a></div>
<div class="col-xs-1"><a href="https://' . $device [ 'hostname' ] . '" target="_blank" rel="noopener"><i class="fa fa-globe fa-lg icon-theme" title="Launch browser https://' . $device [ 'hostname' ] . '"></i></a></div>
2016-10-16 17:58:48 +03:00
</div>
</div>
' ;
2015-07-13 20:10:26 +02:00
$hostname = generate_device_link ( $device );
2016-10-16 17:58:48 +03:00
2015-07-13 20:10:26 +02:00
if ( extension_loaded ( 'mbstring' )) {
$location = mb_substr ( $device [ 'location' ], 0 , 32 , 'utf8' );
2016-08-18 20:28:22 -05:00
} else {
2016-11-20 05:12:25 -06:00
$location = substr ( $device [ 'location' ], 0 , 32 );
2015-07-13 20:10:26 +02:00
}
if ( $subformat == 'detail' ) {
2016-10-16 17:58:48 +03:00
$platform = $device [ 'hardware' ] . '<br>' . $device [ 'features' ];
$os = $device [ 'os_text' ] . '<br>' . $device [ 'version' ];
2017-02-01 20:15:28 +00:00
$device [ 'ip' ] = inet6_ntop ( $device [ 'ip' ]);
2016-10-16 17:58:48 +03:00
$uptime = formatUptime ( $device [ 'uptime' ], 'short' );
2017-05-05 12:25:58 +01:00
if ( format_hostname ( $device ) !== $device [ 'sysName' ]) {
2017-02-01 20:15:28 +00:00
$hostname .= '<br />' . $device [ 'sysName' ];
} elseif ( $device [ 'hostname' ] !== $device [ 'ip' ]) {
$hostname .= '<br />' . $device [ 'hostname' ];
}
2017-05-01 23:49:11 -05:00
$metrics = array ();
2015-07-13 20:10:26 +02:00
if ( $port_count ) {
2017-05-01 23:49:11 -05:00
$port_widget = '<a href="' . generate_device_url ( $device , array ( 'tab' => 'ports' )) . '">' ;
$port_widget .= '<span><i class="fa fa-link fa-lg icon-theme"></i> ' . $port_count ;
$port_widget .= '</span></a> ' ;
$metrics [] = $port_widget ;
2015-07-13 20:10:26 +02:00
}
if ( $sensor_count ) {
2017-05-01 23:49:11 -05:00
$sensor_widget = '<a href="' . generate_device_url ( $device , array ( 'tab' => 'health' )) . '">' ;
$sensor_widget .= '<span><i class="fa fa-dashboard fa-lg icon-theme"></i> ' . $sensor_count ;
$sensor_widget .= '</span></a> ' ;
$metrics [] = $sensor_widget ;
2015-07-13 20:10:26 +02:00
}
2017-05-01 23:49:11 -05:00
if ( $wireless_count ) {
$wireless_widget = '<a href="' . generate_device_url ( $device , array ( 'tab' => 'wireless' )) . '">' ;
$wireless_widget .= '<span><i class="fa fa-wifi fa-lg icon-theme"></i> ' . $wireless_count ;
$wireless_widget .= '</span></a> ' ;
$metrics [] = $wireless_widget ;
}
$col_port = '<div class="device-table-metrics">' ;
$col_port .= implode ( count ( $metrics ) == 2 ? '<br />' : '' , $metrics );
$col_port .= '</div>' ;
2016-08-18 20:28:22 -05:00
} else {
2016-10-16 17:58:48 +03:00
$platform = $device [ 'hardware' ];
$os = $device [ 'os_text' ] . ' ' . $device [ 'version' ];
$uptime = formatUptime ( $device [ 'uptime' ], 'short' );
2017-05-01 23:49:11 -05:00
$col_port = '' ;
2015-07-13 20:10:26 +02:00
}
$response [] = array (
2016-10-16 17:58:48 +03:00
'extra' => $extra ,
'list_type' => $subformat ,
'icon' => $image ,
2015-07-13 20:10:26 +02:00
'hostname' => $hostname ,
2016-10-16 17:58:48 +03:00
'ports' => $col_port ,
2015-07-13 20:10:26 +02:00
'hardware' => $platform ,
2016-10-16 17:58:48 +03:00
'os' => $os ,
'uptime' => $uptime ,
'location' => $location ,
'actions' => $actions ,
2015-07-13 20:10:26 +02:00
);
} //end foreach
$output = array (
2016-10-16 17:58:48 +03:00
'current' => $current ,
2015-07-13 20:10:26 +02:00
'rowCount' => $rowCount ,
2016-10-16 17:58:48 +03:00
'rows' => $response ,
'total' => $total ,
2015-07-13 20:10:26 +02:00
);
2015-04-08 21:17:48 +01:00
echo _json_encode ( $output );