mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/* Copyright (C) 2015 Sergiusz Paprzycki <serek@walcz.net>
|
/* Copyright (C) 2015 Sergiusz Paprzycki <serek@walcz.net>
|
||||||
*
|
* Copyright (C) 2016 Cercel Valentin <crc@nuamchefazi.ro>
|
||||||
|
*
|
||||||
* This widget is based on legacy frontpage module created by Paul Gear.
|
* This widget is based on legacy frontpage module created by Paul Gear.
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@@ -17,51 +18,118 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Top devices by traffic
|
* Top devices
|
||||||
* @author Sergiusz Paprzycki
|
* @author Sergiusz Paprzycki
|
||||||
* @copyright 2015 Sergiusz Paprzycki <serek@walcz.net>
|
* @copyright 2015 Sergiusz Paprzycki <serek@walcz.net>
|
||||||
|
* @copyright 2016 Cercel Valentin <crc@nuamchefazi.ro>
|
||||||
* @license GPL
|
* @license GPL
|
||||||
* @package LibreNMS
|
* @package LibreNMS
|
||||||
* @subpackage Widgets
|
* @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)) {
|
if (defined('SHOW_SETTINGS') || empty($widget_settings)) {
|
||||||
$common_output[] = '
|
$common_output[] = '
|
||||||
<form class="form" onsubmit="widget_settings(this); return false;">
|
<form class="form" onsubmit="widget_settings(this); return false;">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-4">
|
||||||
<label for="graph_type" class="control-label">Number of Devices: </label>
|
<label for="title" class="control-label availability-map-widget-header">Widget title</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-4">
|
<div class="col-sm-6">
|
||||||
<input class="form-control" name="device_count" id="input_count_'.$unique_id.'" placeholder="ie. 5" value="'.$widget_settings['device_count'].'">
|
<input type="text" class="form-control" name="title" placeholder="Custom title for widget" value="' . htmlspecialchars($widget_settings['title']) . '">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="clearfix"></div>
|
<div class="form-group">
|
||||||
<div class="form-group">
|
<div class="col-sm-4">
|
||||||
<div class="col-sm-6">
|
<label for="top_query" class="control-label availability-map-widget-header">Top query</label>
|
||||||
<label for="graph_type" class="control-label">Time interval (minutes): </label>
|
</div>
|
||||||
</div>
|
<div class="col-sm-6">
|
||||||
<div class="col-sm-4">
|
<select class="form-control" name="top_query">
|
||||||
<input class="form-control" name="time_interval" id="input_time_'.$unique_id.'" placeholder="ie. 15" value="'.$widget_settings['time_interval'].'">
|
<option value="traffic" ' . $selected_traffic . '>Traffic</option>
|
||||||
</div>
|
<option value="uptime" ' . $selected_uptime . '>Uptime</option>
|
||||||
</div>
|
<option value="ping" ' . $selected_ping . '>Response time</option>
|
||||||
<div class="form-group">
|
<option value="cpu" ' . $selected_cpu . '>Processor load</option>
|
||||||
<div class="col-sm-2">
|
<option value="ram" ' . $selected_ram . '>Memory usage</option>
|
||||||
<button type="submit" class="btn btn-default">Set</button>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
<div class="form-group">
|
||||||
';
|
<div class="col-sm-4">
|
||||||
|
<label for="graph_type" class="control-label availability-map-widget-header">Number of Devices</label>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<input class="form-control" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57" name="device_count" id="input_count_' . $unique_id . '" placeholder="ie. 5" value="' . $widget_settings['device_count'] . '">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-4">
|
||||||
|
<label for="graph_type" class="control-label availability-map-widget-header">Time interval (minutes)</label>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<input class="form-control" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57" name="time_interval" id="input_time_' . $unique_id . '" placeholder="ie. 15" value="' . $widget_settings['time_interval'] . '">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<button type="submit" class="btn btn-default">Set</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>';
|
||||||
} else {
|
} else {
|
||||||
$interval = $widget_settings['time_interval'];
|
$interval = $widget_settings['time_interval'];
|
||||||
(integer) $interval_seconds = ($interval * 60);
|
(integer)$interval_seconds = ($interval * 60);
|
||||||
(integer) $device_count = $widget_settings['device_count'];
|
(integer)$device_count = $widget_settings['device_count'];
|
||||||
$common_output[] = '
|
|
||||||
<h4>Top '.$device_count.' devices (last '.$interval.' minutes)</h4>
|
$common_output[] = '<h4>Top ' . $device_count . ' devices (last ' . $interval . ' minutes)</h4>';
|
||||||
';
|
|
||||||
$params = array('user' => $_SESSION['user_id'], 'interval' => array($interval_seconds), 'count' => array($device_count));
|
$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
|
SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total
|
||||||
FROM ports as p, devices as d
|
FROM ports as p, devices as d
|
||||||
WHERE d.device_id = p.device_id
|
WHERE d.device_id = p.device_id
|
||||||
@@ -72,8 +140,8 @@ if (defined('SHOW_SETTINGS') || empty($widget_settings)) {
|
|||||||
ORDER BY total desc
|
ORDER BY total desc
|
||||||
LIMIT :count
|
LIMIT :count
|
||||||
';
|
';
|
||||||
} else {
|
} else {
|
||||||
$query = '
|
$query = '
|
||||||
SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total
|
SELECT *, sum(p.ifInOctets_rate + p.ifOutOctets_rate) as total
|
||||||
FROM ports as p, devices as d, `devices_perms` AS `P`
|
FROM ports as p, devices as d, `devices_perms` AS `P`
|
||||||
WHERE `P`.`user_id` = :user AND `P`.`device_id` = `d`.`device_id` AND
|
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
|
ORDER BY total desc
|
||||||
LIMIT :count
|
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[] = '
|
$common_output[] = '
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
<table class="table table-hover table-condensed table-striped bootgrid-table">
|
<table class="table table-hover table-condensed table-striped bootgrid-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="text-left">Device</th>
|
<th class="text-left">Device</th>
|
||||||
<th class="text-left">Total traffic</a></th>
|
<th class="text-left">' . $table_header . '</a></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>';
|
||||||
';
|
|
||||||
|
|
||||||
foreach (dbFetchRows($query, $params) as $result) {
|
foreach (dbFetchRows($query, $params) as $result) {
|
||||||
$common_output[] = '
|
$common_output[] = '
|
||||||
<tr>
|
<tr>
|
||||||
<td class="text-left">'.generate_device_link($result, shorthost($result['hostname'])).'</td>
|
<td class="text-left">' . generate_device_link($result, shorthost($result['hostname'])) . '</td>
|
||||||
<td class="text-left">'.generate_device_link(
|
<td class="text-left">' . generate_device_link($result, generate_minigraph_image($result, $config['time']['day'], $config['time']['now'], $graph_type, 'no', 150, 21), $graph_params, 0, 0, 0) . '</td>
|
||||||
$result,
|
</tr>';
|
||||||
generate_minigraph_image(
|
|
||||||
$result,
|
|
||||||
$config['time']['day'],
|
|
||||||
$config['time']['now'],
|
|
||||||
'device_bits',
|
|
||||||
'no',
|
|
||||||
150,
|
|
||||||
21
|
|
||||||
),
|
|
||||||
array(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
).'
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
';
|
|
||||||
}
|
}
|
||||||
$common_output[] = '
|
$common_output[] = '
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>';
|
||||||
';
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user