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; }