From e3bdc2ff667e9a2cd8b91d3a6b7d2a55564ba767 Mon Sep 17 00:00:00 2001 From: crcro Date: Thu, 29 Sep 2016 09:40:41 +0300 Subject: [PATCH] webui: top devices widget enhancement #4447 webui: top devices enhancement #4447 * code cleaning/optimization as per @laf instructions * removed blank lines, some vars rename for easier understanding * removed redundant var definition for graphs * travis recomandation for string to array --- html/includes/common/top-devices.inc.php | 261 +++++++++++++++++------ 1 file changed, 190 insertions(+), 71 deletions(-) diff --git a/html/includes/common/top-devices.inc.php b/html/includes/common/top-devices.inc.php index 63fd66b1b5..b7fd3b9552 100644 --- a/html/includes/common/top-devices.inc.php +++ b/html/includes/common/top-devices.inc.php @@ -1,6 +1,7 @@ - * + * Copyright (C) 2016 Cercel Valentin + * * This widget is based on legacy frontpage module created by Paul Gear. * * This program is free software: you can redistribute it and/or modify @@ -17,51 +18,118 @@ * along with this program. If not, see . */ /** - * Top devices by traffic + * Top devices * @author Sergiusz Paprzycki * @copyright 2015 Sergiusz Paprzycki + * @copyright 2016 Cercel Valentin * @license GPL * @package LibreNMS * @subpackage Widgets */ +$sql = dbFetchRow('SELECT `settings` FROM `users_widgets` WHERE `user_id` = ? AND `widget_id` = ?', array($_SESSION["user_id"], '11')); +$widget_mode = json_decode($sql['settings'], true); + +$top_query = $widget_mode['top_query']; + +$selected_traffic = ''; +$selected_uptime = ''; +$selected_ping = ''; +$selected_cpu = ''; +$selected_ram = ''; + +switch ($top_query) { + case "traffic": + $table_header = 'Traffic'; + $selected_traffic = 'selected'; + $graph_type = 'device_bits'; + $graph_params = array(); + break; + case "uptime": + $table_header = 'Uptime'; + $selected_uptime = 'selected'; + $graph_type = 'device_uptime'; + $graph_params = array('tab' => 'graphs', 'group' => 'system'); + break; + case "ping": + $table_header = 'Response time'; + $selected_ping = 'selected'; + $graph_type = 'device_ping_perf'; + $graph_params = array('tab' => 'graphs', 'group' => 'poller'); + break; + case "cpu": + $table_header = 'CPU Load'; + $selected_cpu = 'selected'; + $graph_type = 'device_processor'; + $graph_params = array('tab' => 'health', 'metric' => 'processor'); + break; + case "ram": + $table_header = 'Memory usage'; + $selected_ram = 'selected'; + $graph_type = 'device_mempool'; + $graph_params = array('tab' => 'health', 'metric' => 'mempool'); + break; +} + if (defined('SHOW_SETTINGS') || empty($widget_settings)) { $common_output[] = ' -
-
-
- -
-
- -
-
-
-
-
- -
-
- -
-
-
-
- -
-
-
- '; +
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+
'; } else { $interval = $widget_settings['time_interval']; - (integer) $interval_seconds = ($interval * 60); - (integer) $device_count = $widget_settings['device_count']; - $common_output[] = ' -

Top '.$device_count.' devices (last '.$interval.' minutes)

- '; + (integer)$interval_seconds = ($interval * 60); + (integer)$device_count = $widget_settings['device_count']; + + $common_output[] = '

Top ' . $device_count . ' devices (last ' . $interval . ' minutes)

'; + $params = array('user' => $_SESSION['user_id'], 'interval' => array($interval_seconds), 'count' => array($device_count)); - if (is_admin() || is_read()) { - $query = ' + + if ($top_query === 'traffic') { + if (is_admin() || is_read()) { + $query = ' SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total FROM ports as p, devices as d WHERE d.device_id = p.device_id @@ -72,8 +140,8 @@ if (defined('SHOW_SETTINGS') || empty($widget_settings)) { ORDER BY total desc LIMIT :count '; - } else { - $query = ' + } else { + $query = ' SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total FROM ports as p, devices as d, `devices_perms` AS `P` WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND @@ -85,46 +153,97 @@ if (defined('SHOW_SETTINGS') || empty($widget_settings)) { ORDER BY total desc LIMIT :count '; + } + } elseif ($top_query === 'uptime') { + if (is_admin() || is_read()) { + $query = 'SELECT `uptime`, `hostname`, `last_polled`, `device_id` + FROM `devices` + WHERE unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `uptime` DESC + LIMIT :count'; + } else { + $query = 'SELECT `uptime`, `hostname`, `last_polled`, `d`.`device_id` + FROM `devices` as `d`, `devices_perms` AS `P` + WHERE `P`.`user_id` = :user + AND `P`.`device_id` = `d`.`device_id` + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `uptime` DESC + LIMIT :count'; + } + } elseif ($top_query === 'ping') { + if (is_admin() || is_read()) { + $query = 'SELECT `last_ping_timetaken`, `hostname`, `last_polled`, `device_id` + FROM `devices` + WHERE unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `last_ping_timetaken` DESC + LIMIT :count'; + } else { + $query = 'SELECT `last_ping_timetaken`, `hostname`, `last_polled`, `d`.`device_id` + FROM `devices` as `d`, `devices_perms` AS `P` + WHERE `P`.`user_id` = :user + AND `P`.`device_id` = `d`.`device_id` + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `last_ping_timetaken` DESC + LIMIT :count'; + } + } elseif ($top_query === 'cpu') { + if (is_admin() || is_read()) { + $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, avg(`processor_usage`) as `cpuload` + FROM `processors` AS `procs`, `devices` AS `d` + WHERE `d`.`device_id` = `procs`.`device_id` + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + GROUP BY `d`.`device_id` + ORDER BY `cpuload` DESC + LIMIT :count'; + } else { + $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, avg(`processor_usage`) as `cpuload` + FROM `processors` AS procs, `devices` AS `d`, `devices_perms` AS `P` + WHERE `P`.`user_id` = :user AND `P`.`device_id` = `procs`.`device_id` + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + GROUP BY `procs`.`device_id` + ORDER BY `cpuload` DESC + LIMIT :count'; + } + } elseif ($top_query === 'ram') { + if (is_admin() || is_read()) { + $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, `mempool_perc` + FROM `mempools` as `mem`, `devices` as `d` + WHERE `d`.`device_id` = `mem`.`device_id` + AND `mempool_descr` IN (\'Physical memory\',\'Memory\') + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `mempool_perc` DESC + LIMIT :count'; + } else { + $query = 'SELECT `hostname`, `last_polled`, `d`.`device_id`, `mempool_perc` + FROM `mempools` as `mem`, `devices` as `d`, `devices_perms` AS `P` + WHERE `P`.`user_id` = :user AND `P`.`device_id` = `mem`.`device_id` + AND `mempool_descr` IN (\'Physical memory\',\'Memory\') + AND unix_timestamp() - UNIX_TIMESTAMP(`last_polled`) < :interval + ORDER BY `mempool_perc` DESC + LIMIT :count'; + } } + $common_output[] = ' -
- - - - - - - - - '; +
+
DeviceTotal traffic
+ + + + + + + '; foreach (dbFetchRows($query, $params) as $result) { $common_output[] = ' - - - - - '; + + + + '; } $common_output[] = ' - -
Device' . $table_header . '
'.generate_device_link($result, shorthost($result['hostname'])).''.generate_device_link( - $result, - generate_minigraph_image( - $result, - $config['time']['day'], - $config['time']['now'], - 'device_bits', - 'no', - 150, - 21 - ), - array(), - 0, - 0, - 0 - ).' -
' . generate_device_link($result, shorthost($result['hostname'])) . '' . generate_device_link($result, generate_minigraph_image($result, $config['time']['day'], $config['time']['now'], $graph_type, 'no', 150, 21), $graph_params, 0, 0, 0) . '
-
- '; + + + '; }