Merge pull request #1523 from laf/issue-685

Dynamic dashboard
This commit is contained in:
Daniel Preussker
2015-08-01 10:06:28 +00:00
50 changed files with 14078 additions and 206 deletions
+2 -1
View File
@@ -13,5 +13,6 @@
*/
$device['device_id'] = '-1';
require_once 'includes/print-alerts.php';
require_once 'includes/common/alerts.inc.php';
echo implode('',$common_output);
unset($device['device_id']);
+2 -38
View File
@@ -1,40 +1,4 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2015 Søren Friis Rosiak <sorenrosiak@gmail.com>
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
if ($_SESSION['userlevel'] >= '10') {
$sql = "SELECT `hostname`,`device_id`,`status`,`uptime` FROM `devices` WHERE `ignore` = '0' AND `disabled` = '0' ORDER BY `hostname`";
$sqlcount = "SELECT COUNT(*) FROM `devices` WHERE `ignore` = '0' AND `disabled` = '0' ORDER BY `hostname`";
$rows = dbFetchCell($sqlcount);
echo "<div style='max-width:800px; margin-left:auto; margin-right:auto;'><center><h3>All Devices(" . $rows . ")</h3></center>";
}
else {
$sql = "SELECT D.`hostname`,D.`device_id`,D.`status`,D.`uptime` FROM `devices` AS D, `devices_perms` AS P WHERE D.`device_id` = P.`device_id` AND P.`user_id` = '" . $_SESSION['user_id'] . "' AND D.`ignore` = '0' AND D.`disabled` = '0' ORDER BY D.`hostname`";
$sqlcount = "SELECT COUNT(*) FROM `devices` AS D, `devices_perms` AS P WHERE D.`device_id` = P.`device_id` AND P.`user_id` = '" . $_SESSION['user_id'] . "' AND D.`ignore` = '0' AND D.`disabled` = '0' ORDER BY D.`hostname`";
$rows = dbFetchCell($sqlcount);
echo "<div style='max-width:800px; margin-left:auto; margin-right:auto;'><center><h3>All Devices(" . $rows . ")</h3></center>";
}
foreach(dbFetchRows($sql) as $device) {
if ($device['status'] == '1') {
$btn_type = 'btn-success';
if ($device['uptime'] < $config['uptime_warning']) {
$btn_type = 'btn-warning';
}
}
else {
$btn_type = 'btn-danger';
}
echo "<a href='" .generate_url(array('page' => 'device', 'device' => $device['device_id'])). "' role='button' class='btn " . $btn_type . " btn-xs' title='" . $device['hostname'] . "' style='min-height:25px; min-width:25px; border-radius:0px;'></a>";
}
echo "</div>";
include_once 'includes/common/availability-map.inc.php';
echo implode('',$common_output);
+2 -1
View File
@@ -12,4 +12,5 @@
* the source code distribution for details.
*/
require_once 'includes/print-alerts.php';
require_once 'includes/common/alerts.inc.php';
echo implode('',$common_output);
+2 -1
View File
@@ -160,7 +160,8 @@ if ($config['vertical_summary']) {
}
else {
echo ' <div class="col-md-4">';
include_once 'includes/device-summary-horiz.inc.php';
include_once 'includes/common/device-summary-horiz.inc.php';
echo implode('',$common_output);
}
echo '
+12 -73
View File
@@ -23,84 +23,22 @@
* @subpackage Frontpage
*/
?>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Site');
data.addColumn('number', 'Status');
data.addColumn('number', 'Size');
data.addColumn({type: 'string', role: 'tooltip', 'p': {'html': true}});
data.addRows([
<?php
$locations = array();
foreach (getlocations() as $location) {
$devices = array();
$devices_down = array();
$devices_up = array();
$count = 0;
$down = 0;
foreach (dbFetchRows("SELECT devices.device_id,devices.hostname,devices.status FROM devices LEFT JOIN devices_attribs ON devices.device_id = devices_attribs.device_id WHERE devices.location = ? && devices.disabled = 0 && devices.ignore = 0 GROUP BY devices.hostname", array($location)) as $device) {
if( $config['frontpage_globe']['markers'] == 'devices' || empty($config['frontpage_globe']['markers']) ) {
$devices[] = $device['hostname'];
$count++;
if( $device['status'] == "0" ) {
$down++;
$devices_down[] = $device['hostname']." DOWN";
}
else {
$devices_up[] = $device;
}
}
elseif( $config['frontpage_globe']['markers'] == 'ports' ) {
foreach( dbFetchRows("SELECT ifName,ifOperStatus,ifAdminStatus FROM ports WHERE ports.device_id = ? && ports.ignore = 0 && ports.disabled = 0 && ports.deleted = 0",array($device['device_id'])) as $port ) {
$count++;
if( $port['ifOperStatus'] == 'down' && $port['ifAdminStatus'] == 'up' ) {
$down++;
$devices_down[] = $device['hostname']."/".$port['ifName']." DOWN";
}
else {
$devices_up[] = $port;
}
}
}
}
$pdown = ($down / $count)*100;
if( $config['frontpage_globe']['markers'] == 'devices' || empty($config['frontpage_globe']['markers']) ) {
$devices_down = array_merge(array(count($devices_up). " Devices OK"), $devices_down);
}
elseif( $config['frontpage_globe']['markers'] == 'ports' ) {
$devices_down = array_merge(array(count($devices_up). " Ports OK"), $devices_down);
}
$locations[] = " ['".$location."', ".$pdown.", ".$count.", '".implode(",<br/> ", $devices_down)."']";
}
echo implode(",\n", $locations);
?>
]);
var options = {
region: '<?php echo($config['frontpage_globe']['region'] ? $config['frontpage_globe']['region'] : "world") ?>',
resolution: '<?php echo($config['frontpage_globe']['resolution'] ? $config['frontpage_globe']['resolution'] : "countries") ?>',
displayMode: 'markers',
keepAspectRatio: 1,
magnifyingGlass: {enable: true, zoomFactor: 100},
colorAxis: {minValue: 0, maxValue: 100, colors: ['green', 'yellow', 'red']},
markerOpacity: 0.90,
tooltip: {isHtml: true},
};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
</script>
<script src='https://www.google.com/jsapi'></script>
<?php
include_once("includes/object-cache.inc.php");
include_once 'includes/object-cache.inc.php';
echo '<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<div id="chart_div"></div>
';
include_once 'includes/common/globe.inc.php';
echo implode(',',$common_output);
echo '
</div>
<div class="col-md-4">
<div class="container-fluid">
@@ -120,7 +58,8 @@ echo ' </div>
<div class="row">
<div class="col-md-12">';
$device['device_id'] = '-1';
require_once('includes/print-alerts.php');
require_once('includes/common/alerts.inc.php');
echo implode('',$common_output);
unset($device['device_id']);
echo ' </div>
</div>
+239
View File
@@ -0,0 +1,239 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
/*
* Code for Gridster.sort_by_row_and_col_asc(serialization) call is from http://gridster.net/demos/grid-from-serialize.html
*/
$no_refresh = true;
foreach (dbFetchRows('SELECT * FROM `users_widgets` LEFT JOIN `widgets` ON `widgets`.`widget_id`=`users_widgets`.`widget_id` WHERE `user_id`=?',array($_SESSION['user_id'])) as $items) {
$data[] = $items;
}
if (!is_array($data)) {
$data[] = array('user_widget_id'=>'0','widget_id'=>1,'title'=>'Add a widget','widget'=>'placeholder','col'=>1,'row'=>1,'size_x'=>2,'size_y'=>2,'refresh'=>60);
}
$data = serialize(json_encode($data));
$dash_config = unserialize(stripslashes($data));
?>
<div class="btn-group" role="group">
<a class="btn btn-default disabled" role="button">Widgets</a>
<a class="btn btn-success" role="button" data-toggle="collapse" href="#add_widget" aria-expanded="false" aria-controls="add_widget"><i class="fa fa-plus fa-fw"></i></a>
<a class="btn btn-danger" role="button" id="clear_widgets" name="clear_widgets"><i class="fa fa-trash fa-fw"></i></a>
</div>
<div class="collapse" id="add_widget">
<div class="well">
<?php
foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widgets) {
echo '<a class="btn btn-success" role="button" id="place_widget" name="place_widget" data-widget_id="'.$widgets['widget_id'] .'">'. $widgets['widget_title'] .'</a> ';
}
?>
</div>
</div>
<script src='https://www.google.com/jsapi'></script>
<script src="js/jquery.gridster.min.js"></script>
<span class="message" id="message"></span>
<div class="row">
<div class="col-sm-12">
<div class="gridster grid">
<ul>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
var gridster;
var serialization = <?php echo $dash_config; ?>;
serialization = Gridster.sort_by_row_and_col_asc(serialization);
function updatePos(gridster) {
var s = JSON.stringify(gridster.serialize());
$.ajax({
type: 'POST',
url: '/ajax_form.php',
data: {type: "update-dashboard-config", data: s},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
}
else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred.</div>');
}
});
}
$(function(){
gridster = $(".gridster ul").gridster({
widget_base_dimensions: [100, 100],
widget_margins: [5, 5],
avoid_overlapped_widgets: true,
draggable: {
handle: 'header',
stop: function(e, ui, $widget) {
updatePos(gridster);
},
},
resize: {
enabled: true,
stop: function(e, ui, $widget) {
updatePos(gridster);
}
},
serialize_params: function($w, wgd) {
return {
id: $($w).attr('id'),
col: wgd.col,
row: wgd.row,
size_x: wgd.size_x,
size_y: wgd.size_y
};
}
}).data('gridster');
gridster.remove_all_widgets();
$.each(serialization, function() {
gridster.add_widget(
'<li id="'+this.user_widget_id+'">'+
'\<script\>var timeout'+this.user_widget_id+' = grab_data('+this.user_widget_id+','+this.refresh+',\''+this.widget+'\');\<\/script\>'+
'<header class="widget_header">'+this.title+'<button style="color: #ffffff" type="button" class="close close-widget" data-widget-id="'+this.user_widget_id+'" aria-label="Close"><span aria-hidden="true">&times;</span></button></header>'+
'<div class="widget_body" id="widget_body_'+this.user_widget_id+'">'+this.widget+'</div>'+
'</li>',
parseInt(this.size_x), parseInt(this.size_y), parseInt(this.col), parseInt(this.row)
);
});
$(document).on('click','#clear_widgets', function() {
var widget_id = $(this).data('widget-id');
$.ajax({
type: 'POST',
url: '/ajax_form.php',
data: {type: "update-dashboard-config", sub_type: 'remove-all'},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
gridster.remove_all_widgets();
}
else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred.</div>');
}
});
});
$('a[name="place_widget"]').on('click', function(event, state) {
var widget_id = $(this).data('widget_id');
$.ajax({
type: 'POST',
url: '/ajax_form.php',
data: {type: "update-dashboard-config", sub_type: 'add', widget_id: widget_id},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
var widget_id = data.extra.widget_id;
var title = data.extra.title;
var widget = data.extra.widget;
var size_x = data.extra.size_x;
var size_y = data.extra.size_y;
gridster.add_widget(
'<li id="'+widget_id+'">'+
'\<script\>var timeout'+widget_id+' = grab_data('+widget_id+',60,\''+widget+'\');\<\/script\>'+
'<header class="widget_header">'+title+'<button type="button" class="close close-widget" data-widget-id="'+widget_id+'" aria-label="Close"><span aria-hidden="true">&times;</span></button></header>'+
'<div class="widget_body" id="widget_body_'+widget_id+'">'+widget+'</div>'+
'</li>',
parseInt(size_x), parseInt(size_y)
);
updatePos(gridster);
}
else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred.</div>');
}
});
});
$(document).on( "click", ".close-widget", function() {
var widget_id = $(this).data('widget-id');
$.ajax({
type: 'POST',
url: '/ajax_form.php',
data: {type: "update-dashboard-config", sub_type: 'remove', widget_id: widget_id},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
gridster.remove_widget($('#'+widget_id));
updatePos(gridster);
}
else {
$("#message").html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred.</div>');
}
});
});
});
function grab_data(id,refresh,data_type) {
new_refresh = refresh * 1000;
$.ajax({
type: 'POST',
url: '/ajax_dash.php',
data: {type: data_type},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
$("#widget_body_"+id).html(data.html);
}
else {
$("#widget_body_"+id).html('<div class="alert alert-info">' + data.message + '</div>');
}
},
error: function () {
$("#widget_body_"+id).html('<div class="alert alert-info">Problem with backend</div>');
}
});
setTimeout(function() {
grab_data(id,refresh,data_type);
},
new_refresh);
}
$('#new-widget').popover();
</script>