mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
fix: lldp find_port_id finds not the most correct port (#7564)
Order statements to give some results precedence over others. Union the results but limit 1 so we can stop when we get the first result. Checking if variables doesn't work very well without the device_id
This commit is contained in:
@@ -1441,36 +1441,48 @@ function find_port_id($description, $identifier = '', $device_id = 0, $mac_addre
|
||||
return 0;
|
||||
}
|
||||
|
||||
$sql = 'SELECT `port_id` FROM `ports` WHERE (0';
|
||||
$statements = array();
|
||||
$params = array();
|
||||
|
||||
if ($description) {
|
||||
$sql .= ' OR `ifDescr`=? OR `ifName`=?';
|
||||
$params[] = $description;
|
||||
$params[] = $description;
|
||||
}
|
||||
if ($device_id) {
|
||||
if ($description) {
|
||||
$statements[] = "SELECT `port_id` FROM `ports` WHERE `device_id`=? AND (`ifDescr`=? OR `ifName`=?)";
|
||||
|
||||
if ($identifier) {
|
||||
if (is_numeric($identifier)) {
|
||||
$sql .= ' OR `ifIndex`=? OR `ifAlias`=?';
|
||||
} else {
|
||||
$sql .= ' OR `ifDescr`=? OR `ifName`=?';
|
||||
$params[] = $device_id;
|
||||
$params[] = $description;
|
||||
$params[] = $description;
|
||||
}
|
||||
|
||||
if ($identifier) {
|
||||
if (is_numeric($identifier)) {
|
||||
$statements[] = 'SELECT `port_id` FROM `ports` WHERE `device_id`=? AND (`ifIndex`=? OR `ifAlias`=?)';
|
||||
} else {
|
||||
$statements[] = 'SELECT `port_id` FROM `ports` WHERE `device_id`=? AND (`ifDescr`=? OR `ifName`=?)';
|
||||
}
|
||||
$params[] = $device_id;
|
||||
$params[] = $identifier;
|
||||
$params[] = $identifier;
|
||||
}
|
||||
$params[] = $identifier;
|
||||
$params[] = $identifier;
|
||||
}
|
||||
|
||||
if ($mac_address) {
|
||||
$sql .= ' OR `ifPhysAddress`=?';
|
||||
$mac_statement = 'SELECT `port_id` FROM `ports` WHERE ';
|
||||
if ($device_id) {
|
||||
$mac_statement .= '`device_id`=? AND ';
|
||||
$params[] = $device_id;
|
||||
}
|
||||
$mac_statement .= '`ifPhysAddress`=?';
|
||||
|
||||
$statements[] = $mac_statement;
|
||||
$params[] = $mac_address;
|
||||
}
|
||||
|
||||
$sql .= ')';
|
||||
|
||||
if ($device_id) {
|
||||
$sql .= ' AND `device_id`=?';
|
||||
$params[] = $device_id;
|
||||
if (empty($statements)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$queries = implode(' UNION ', $statements);
|
||||
$sql = "SELECT * FROM ($queries LIMIT 1) p";
|
||||
|
||||
return (int)dbFetchCell($sql, $params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user