From 14f1bd36b8f1bfd9ff96953437a68f8f15fed967 Mon Sep 17 00:00:00 2001 From: f0o Date: Sun, 20 Sep 2015 10:13:56 +0100 Subject: [PATCH] Added ability to filter top interfaces by type --- html/ajax_search.php | 22 ++++ html/includes/common/top-interfaces.inc.php | 124 +++++++++++++------- 2 files changed, 102 insertions(+), 44 deletions(-) diff --git a/html/ajax_search.php b/html/ajax_search.php index e42655ee9..7d82a1ac9 100644 --- a/html/ajax_search.php +++ b/html/ajax_search.php @@ -294,6 +294,28 @@ if (isset($_REQUEST['search'])) { }//end foreach }//end if + $json = json_encode($device); + die($json); + } + else if ($_REQUEST['type'] == 'iftype') { + // Device search + if (is_admin() === true || is_read() === true) { + $results = dbFetchRows("SELECT `ports`.ifType FROM `ports` WHERE `ifType` LIKE '%".$search."%' GROUP BY ifType ORDER BY ifType LIMIT 8"); + } + else { + $results = dbFetchRows("SELECT `I`.ifType FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifType` LIKE '%".$search."%') GROUP BY ifType ORDER BY ifType LIMIT 8", array($_SESSION['user_id'], $_SESSION['user_id'])); + } + if (count($results)) { + $found = 1; + $devices = count($results); + + foreach ($results as $result) { + $device[] = array( + 'filter' => $result['ifType'], + ); + }//end foreach + }//end if + $json = json_encode($device); die($json); }//end if diff --git a/html/includes/common/top-interfaces.inc.php b/html/includes/common/top-interfaces.inc.php index 3299be1ae..07ebabf55 100644 --- a/html/includes/common/top-interfaces.inc.php +++ b/html/includes/common/top-interfaces.inc.php @@ -27,74 +27,110 @@ if( defined('show_settings') || empty($widget_settings) ) { $common_output[] = ' -
+
-
- -
-
- -
-
-
-
-
- -
-
- + +
+
-
+ +
+ +
+
+
+ +
+ +
+
+
+
+ + '; } else { $interval = $widget_settings['time_interval']; - (integer) $interval_seconds = ($interval * 60); + (integer) $lastpoll_seconds = ($interval * 60); (integer) $interface_count = $widget_settings['interface_count']; - $common_output[] = ' -

Top '.$interface_count.' interfaces (last '.$interval.' minutes)

- '; - $params = array('user' => $_SESSION['user_id'], 'interval' => array($interval_seconds), 'count' => array($interface_count)); + $params = array('user' => $_SESSION['user_id'], 'lastpoll' => array($lastpoll_seconds), 'count' => array($interface_count), 'filter' => ($widget_settings['interface_filter']?:(int)1)); if (is_admin() || is_read()) { $query = ' SELECT *, p.ifInOctets_rate + p.ifOutOctets_rate as total - FROM ports as p, devices as d - WHERE d.device_id = p.device_id - AND unix_timestamp() - p.poll_time < :interval - AND ( p.ifInOctets_rate > 0 - OR p.ifOutOctets_rate > 0 ) - ORDER BY total desc + FROM ports as p + INNER JOIN devices ON p.device_id = devices.device_id + AND unix_timestamp() - p.poll_time <= :lastpoll + AND ( p.ifType = :filter || 1 = :filter ) + AND ( p.ifInOctets_rate > 0 || p.ifOutOctets_rate > 0 ) + ORDER BY total DESC LIMIT :count '; } else { $query = ' - SELECT ports.*, devices.hostname, ports.ifInOctets_rate + ports.ifOutOctets_rate as total - FROM devices, ports - LEFT JOIN ports_perms ON ports.port_id = ports_perms.port_id - WHERE ports_perms.user_id = :user - AND unix_timestamp() - ports.poll_time < :interval - AND (ports.ifInOctets_rate > 0 OR ports.ifOutOctets_rate > 0) - GROUP BY ports.port_id - UNION ALL - SELECT ports.*, devices.hostname, ports.ifInOctets_rate + ports.ifOutOctets_rate as total - FROM ports, devices LEFT JOIN devices_perms ON devices.device_id = devices_perms.device_id - WHERE devices_perms.user_id = :user - AND ports.device_id = devices.device_id - AND unix_timestamp() - ports.poll_time < :interval - AND (ports.ifInOctets_rate > 0 OR ports.ifOutOctets_rate > 0) - GROUP BY ports.port_id - ORDER BY total DESC LIMIT :count + SELECT ports.*, devices.hostname, ports.ifInOctets_rate + ports.ifOutOctets_rate as total + FROM ports + INNER JOIN devices ON ports.device_id = devices.device_id + LEFT JOIN ports_perms ON ports.port_id = ports_perms.port_id + LEFT JOIN devices_perms ON devices.device_id = devices_perms.device_id + WHERE ( ports_perms.user_id = :user || devices_perms.user_id = :user ) + AND unix_timestamp() - ports.poll_time <= :lastpoll + AND ( ports.ifType = :filter || 1 = :filter ) + AND ( ports.ifInOctets_rate > 0 || ports.ifOutOctets_rate > 0 ) + GROUP BY ports.port_id + ORDER BY total DESC + LIMIT :count '; } - $common_output[] = ' +

Top '.$interface_count.' interfaces polled within '.$interval.' minutes