From ddab4153bc0c391cacac5766df1cd862d7f88d95 Mon Sep 17 00:00:00 2001 From: seros1521 <55374617+seros1521@users.noreply.github.com> Date: Sun, 5 Jul 2020 10:23:11 +0700 Subject: [PATCH] Modifies find_device_id (#11804) * modifies find_device_id * Removed host name validation check for RFC. Some devices and OS allow you to set a name that contains characters that are prohibited by the RFC. Such devices can be added using their IP address, their sysName will be written to the database. If the device is already in the database, then it should be possible to find it, even if the name is unacceptable according to RFC requirements. * Additional search options for sysName have been added using the default domain. This was needed to improve LLDP detection - a short name was used in the PDU, and the full name with the domain was stored in the sysName field in the database. * modifies find_device_id * reordering checks * handling situations with duplicate sysName * modifies find_device_id (fix style errors) --- includes/discovery/functions.inc.php | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 435d91de6a..a386f1cc68 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1364,9 +1364,6 @@ function find_device_id($name = '', $ip = '', $mac_address = '') $params = array(); if ($name && is_valid_hostname($name)) { - $where[] = '`sysName`=?'; - $params[] = $name; - $where[] = '`hostname`=?'; $params[] = $name; @@ -1405,6 +1402,32 @@ function find_device_id($name = '', $ip = '', $mac_address = '') } } + if ($name) { + $where = array(); + $params = array(); + + $where[] = '`sysName`=?'; + $params[] = $name; + + if ($mydomain = Config::get('mydomain')) { + $where[] = '`sysName`=?'; + $params[] = "$name.$mydomain"; + + $where[] = 'concat(`sysName`, \'.\', ?) =?'; + $params[] = "$mydomain"; + $params[] = "$name"; + } + + $sql = 'SELECT `device_id` FROM `devices` WHERE ' . implode(' OR ', $where) . ' LIMIT 2'; + $ids = dbFetchColumn($sql, $params); + if (count($ids) == 1) { + return (int)$ids[0]; + } elseif (count($ids) > 1) { + d_echo("find_device_id: more than one device found with sysName '$name'.\n"); + // don't do anything, try other methods, if any + } + } + return 0; }