webui: Improved fdb search (#8251)

* sort fdb search by number of mac addresses/port to show most likely endpoint switchport first in list. Also add IP search to fdb search

* fix style check error
This commit is contained in:
Adam Winberg
2018-04-07 21:14:28 +02:00
committed by Neil Lathwood
parent e534608668
commit 2ece84495d
2 changed files with 38 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
<?php
$param = array();
$select = "SELECT `F`.`port_id` AS `port_id`, `device_id`, `ifInErrors`, `ifOutErrors`, `ifOperStatus`,";
$select = "SELECT `F`.`port_id` AS `port_id`, `F`.`device_id`, `ifInErrors`, `ifOutErrors`, `ifOperStatus`,";
$select .= " `ifAdminStatus`, `ifAlias` AS `interface`, `ifDescr`, `mac_address`, `V`.`vlan_vlan` AS `vlan`,";
$select .= " `hostname`, `hostname` AS `device` , group_concat(`M`.`ipv4_address` SEPARATOR ', ') AS `ipv4_address`";
@@ -9,6 +9,8 @@ $sql = " FROM `ports_fdb` AS `F`";
$sql .= " LEFT JOIN `devices` AS `D` USING(`device_id`)";
$sql .= " LEFT JOIN `ports` AS `P` USING(`port_id`, `device_id`)";
$sql .= " LEFT JOIN `vlans` AS `V` USING(`vlan_id`, `device_id`)";
// Add counter so we can ORDER BY the port_id with least amount of macs attached
$sql .= " LEFT JOIN ( SELECT `port_id`, COUNT(*) `portCount` FROM `ports_fdb` GROUP BY `port_id` ) AS `C` ON `C`.`port_id` = `F`.`port_id`";
$where = " WHERE 1";
@@ -30,11 +32,16 @@ if (is_numeric($vars['port_id'])) {
if (isset($vars['searchPhrase']) && !empty($vars['searchPhrase'])) {
$search = mres(trim($vars['searchPhrase']));
$ip_search = '%'.mres(trim($vars['searchPhrase'])).'%';
$mac_search = '%'.str_replace(array(':', ' ', '-', '.', '0x'), '', $search).'%';
if (isset($vars['searchby']) && $vars['searchby'] == 'vlan') {
$where .= ' AND `V`.`vlan_vlan` = ?';
$param[] = (int)$search;
} elseif (isset($vars['searchby']) && $vars['searchby'] == 'ip') {
$sql .= " LEFT JOIN `ipv4_mac` AS `M` USING (`mac_address`)";
$where .= ' AND `M`.`ipv4_address` LIKE ?';
$param[] = $ip_search;
} elseif ((isset($vars['searchby']) && $vars['searchby'] == 'mac') ||
(!is_numeric($search) || $search > 4096)
) {
@@ -49,16 +56,27 @@ if (isset($vars['searchPhrase']) && !empty($vars['searchPhrase'])) {
$total = (int)dbFetchCell("SELECT COUNT(*) $sql $where", $param);
// Don't use ipv4_mac in count it will inflate the rows unless we aggregate it and it isn't used for search
$sql .= " LEFT JOIN `ipv4_mac` AS `M` USING (`mac_address`, `device_id`)";
// Don't use ipv4_mac in count it will inflate the rows unless we aggregate it
// Except for 'ip' search.
if ($vars['searchby'] != 'ip') {
$sql .= " LEFT JOIN `ipv4_mac` AS `M` USING (`mac_address`)";
}
$sql .= $where;
$sql .= " GROUP BY `device_id`, `port_id`, `mac_address`, `vlan`, `hostname`, `ifAlias`,";
$sql .= " `ifAdminStatus`, `ifDescr`, `ifOperStatus`, `ifInErrors`, `ifOutErrors`";
if (!isset($sort) || empty($sort)) {
$sort = '`F`.`port_id` ASC';
// Get most likely endpoint port_id, used to add a visual marker for this element
// in the list
if (isset($vars['searchby']) && !empty($vars['searchPhrase']) && $vars['searchby'] != 'vlan') {
$countsql .= " ORDER BY `C`.`portCount` ASC LIMIT 1";
foreach (dbFetchRows($select . $sql . $countsql, $param) as $entry) {
$endpoint_portid = $entry['port_id'];
}
}
if (!isset($sort) || empty($sort)) {
$sort = '`C`.`portCount` ASC';
}
$sql .= " ORDER BY $sort";
if (isset($current)) {
@@ -83,12 +101,17 @@ foreach (dbFetchRows($select . $sql, $param) as $entry) {
} else {
$error_img = '';
}
if ($entry['port_id'] == $endpoint_portid) {
$endpoint_img = "<i class='fa fa-star fa-lg' style='color:green' aria-hidden='true' title='This indicates the most likely endpoint switchport'></i>";
} else {
$endpoint_img = '';
}
$response[] = array(
'device' => generate_device_link(device_by_id_cache($entry['device_id'])),
'mac_address' => formatMac($entry['mac_address']),
'ipv4_address' => $entry['ipv4_address'],
'interface' => generate_port_link($entry, makeshortif(fixifname($entry['label']))).' '.$error_img,
'interface' => generate_port_link($entry, makeshortif(fixifname($entry['label']))).' '.$error_img.' '.$endpoint_img,
'vlan' => $entry['vlan'],
);
}//end if

View File

@@ -5,7 +5,7 @@
<table id="fdb-search" class="table table-hover table-condensed table-striped">
<thead>
<tr>
<th data-column-id="device" data-order="asc">Device</th>
<th data-column-id="device">Device</th>
<th data-column-id="mac_address">MAC Address</th>
<th data-column-id="ipv4_address">IPv4 Address</th>
<th data-column-id="interface">Port</th>
@@ -61,6 +61,14 @@ if ($_POST['searchby'] == 'mac') {
?>
">MAC Address</option>"+
"<option value=\"ip\" "+
<?php
if ($_POST['searchby'] == 'ip') {
echo '" selected "+';
}
?>
">IP Address</option>"+
"<option value=\"vlan\" "+
<?php
if ($_POST['searchby'] == 'vlan') {