From c273ab28af2669e74ea132a3c40a8b87fcf15e85 Mon Sep 17 00:00:00 2001 From: Joseph Tingiris Date: Wed, 19 Feb 2020 06:51:39 -0500 Subject: [PATCH] Global search by IP address (#11165) * device search by (valid) ip address * add device_ip & not empty conditions to search results * updated comments * spaces after commas * search ip and overwrite_ip for name based searches, too --- html/ajax_search.php | 26 ++++++++++++++++++-------- resources/views/layouts/menu.blade.php | 7 ++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/html/ajax_search.php b/html/ajax_search.php index 46738aa9ff..0bbaadb489 100644 --- a/html/ajax_search.php +++ b/html/ajax_search.php @@ -29,7 +29,7 @@ if (isset($_REQUEST['search'])) { } if ($_REQUEST['type'] == 'group') { - foreach (dbFetchRows("SELECT id,name FROM device_groups WHERE name LIKE ?", ["%$search%"]) as $group) { + foreach (dbFetchRows("SELECT id, name FROM device_groups WHERE name LIKE ?", ["%$search%"]) as $group) { if ($_REQUEST['map']) { $results[] = array( 'name' => 'g:'.$group['name'], @@ -50,13 +50,23 @@ if (isset($_REQUEST['search'])) { } elseif ($_REQUEST['type'] == 'device') { // Device search if (Auth::user()->hasGlobalRead()) { - $results = dbFetchRows( - "SELECT * FROM `devices` LEFT JOIN `locations` ON `locations`.`id` = `devices`.`location_id` WHERE `devices`.`hostname` LIKE ? OR `locations`.`location` LIKE ? OR `devices`.`sysName` LIKE ? OR `devices`.`purpose` LIKE ? OR `devices`.`notes` LIKE ? ORDER BY `devices`.hostname LIMIT ?", - ["%$search%", "%$search%", "%$search%", "%$search%", "%$search%", $limit] - ); + if (\LibreNMS\Util\IPv4::isValid($search, false) || \LibreNMS\Util\IPv6::isValid($search, false)) { + // Device search ip, overwrite_ip, or hostname by address; sort by ip + $results = dbFetchRows( + "SELECT *, inet6_ntoa(`ip`) as `ntoa_ip` FROM `devices` LEFT JOIN `locations` on `locations`.`id` = `devices`.`location_id` WHERE inet6_ntoa(`devices`.`ip`) LIKE ? OR `devices`.`overwrite_ip` LIKE ? OR `devices`.`hostname` LIKE ? ORDER BY `ip`, `hostname` LIMIT ?", + ["$search%", "$search%", "$search%", $limit] + ); + } else { + // Device search ip, overwrite_ip, hostname, location, sysname, purpose, or notes; sort by hostname + $results = dbFetchRows( + "SELECT *, inet6_ntoa(ip) as `ntoa_ip` FROM `devices` LEFT JOIN `locations` ON `locations`.`id` = `devices`.`location_id` WHERE inet6_ntoa(`devices`.`ip`) LIKE ? OR `devices`.`overwrite_ip` LIKE ? OR `devices`.`hostname` LIKE ? OR `locations`.`location` LIKE ? OR `devices`.`sysName` LIKE ? OR `devices`.`purpose` LIKE ? OR `devices`.`notes` LIKE ? ORDER BY `devices`.`hostname` LIMIT ?", + ["%$search%", "%$search%", "%$search%", "%$search%", "%$search%", "%$search%", "%$search%", $limit] + ); + } } else { + // Device search hostname, sysname, or location by non-address for users without global read access $results = dbFetchRows( - "SELECT * FROM `devices` AS `D` LEFT JOIN `locations` ON `locations`.`id` = `D`.`location_id` WHERE $perms_sql AND (D.`hostname` LIKE ? OR D.`sysName` LIKE ? OR `locations`.`location` LIKE ?) ORDER BY hostname LIMIT ?", + "SELECT *, inet6_ntoa(ip) as `ntoa_ip` FROM `devices` AS `D` LEFT JOIN `locations` ON `locations`.`id` = `D`.`location_id` WHERE $perms_sql AND (D.`hostname` LIKE ? OR D.`sysName` LIKE ? OR `locations`.`location` LIKE ?) ORDER BY hostname LIMIT ?", array_merge($device_ids, ["%$search%", "%$search%", "%$search%", $limit]) ); } @@ -82,7 +92,6 @@ if (isset($_REQUEST['search'])) { $num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` AS `I`, `devices` AS `D` WHERE ' . $perms_sql . ' AND `I`.`device_id` = `D`.`device_id` AND `I`.`ignore` = 0 AND `I`.`deleted` = 0 AND `D`.`device_id` = ?', array_merge($device_ids, [$result['device_id']])); - $device[] = array( 'name' => $name, 'device_id' => $result['device_id'], @@ -91,9 +100,10 @@ if (isset($_REQUEST['search'])) { 'device_ports' => $num_ports, 'device_image' => getIcon($result), 'device_hardware' => $result['hardware'], - 'device_os' => \LibreNMS\Config::getOsSetting($result['os'], 'text'), + 'device_os' => \LibreNMS\Config::getOsSetting($result['os'], 'text'), 'version' => $result['version'], 'location' => $result['location'], + 'device_ip' => $result['ntoa_ip'] ); }//end foreach }//end if diff --git a/resources/views/layouts/menu.blade.php b/resources/views/layouts/menu.blade.php index de888bd5d0..c5a8d35654 100644 --- a/resources/views/layouts/menu.blade.php +++ b/resources/views/layouts/menu.blade.php @@ -582,6 +582,7 @@ device_image: device.device_image, url: device.url, name: device.name, + device_ip: device.device_ip, device_os: device.device_os, version: device.version, device_hardware: device.device_hardware, @@ -662,7 +663,7 @@ valueKey: 'name', templates: { header: '
 Devices
', - suggestion: Handlebars.compile('

@{{name}} | @{{device_os}} | @{{version}} | @{{device_hardware}} with @{{device_ports}} port(s) | @{{location}}

') + suggestion: Handlebars.compile('

@{{name}}@{{#if device_ip}} | @{{device_ip}}@{{/if}}@{{#if device_os}} | @{{device_os}}@{{/if}}@{{#if version}} @{{version}}@{{/if}}@{{#if device_hardware}} | @{{device_hardware}}@{{/if}}@{{#if device_ports}} with @{{device_ports}} port(s)@{{/if}}@{{#if location}} | @{{location}}@{{/if}}

') } }, { @@ -673,7 +674,7 @@ valueKey: 'name', templates: { header: '
 Ports
', - suggestion: Handlebars.compile('

@{{name}} – @{{hostname}}
@{{description}}

') + suggestion: Handlebars.compile('

@{{name}} - @{{hostname}}@{{#if description}} @{{description}}@{{/if}}

') } }, { @@ -684,7 +685,7 @@ valueKey: 'name', templates: { header: '
 BGP Sessions
', - suggestion: Handlebars.compile('

@{{name}} - @{{hostname}}
AS@{{localas}} -> AS@{{remoteas}}

') + suggestion: Handlebars.compile('

@{{name}} - @{{hostname}} - AS@{{localas}} -> AS@{{remoteas}}

') } }).on('typeahead:select', function (ev, suggestion) { window.location.href = suggestion.url;