Private GIT

Skip to content
Snippets Groups Projects
Commit 023e3fc2 authored by Gary Allan's avatar Gary Allan
Browse files

top10_percentage widget

- Deduplicate shared code into top10_hosts_lib.php.
- Cleanup of top10_hosts_lib.php code.
- Use multi-byte compatible functions when shortening text. #1753
parent a627870d
Branches
No related tags found
No related merge requests found
<?php
/**
* Shared code between top10_hosts_v4.php
* and top10_hosts_v6.php
* Shared top10_widget code
*
* @param string $type_ip
* @param bool $type_percentage
* @param integer $height
* @param integer $slimit
*/
function top10_widget($type_ip, $type_percentage, $height, $slimit) {
# required functions
if(!is_object(@$User)) {
require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
# classes
$Database = new Database_PDO;
$User = new User ($Database);
$Tools = new Tools ($Database);
$Subnets = new Subnets ($Database);
$Result = new Result ();
}
global $Subnets;
global $Tools;
global $User;
# user must be authenticated
$User->check_user_session ();
$type_name = $type_ip . "top10". ($type_percentage ? "Percentage" : "Hosts");
$top_subnets = array();
$all_subnets = $Tools->fetch_top_subnets($type, 1000000, false);
$all_subnets = $Tools->fetch_top_subnets($type_ip, 1000000, $type_percentage);
# Find subnets with user access, label duplicates.
$unique = array();
$valid_subnets = 0;
if (is_array($all_subnets)) {
foreach($all_subnets as $subnet) {
if ($Subnets->check_permission($User->user, $subnet->id) == "0") { continue; }
if ($Subnets->check_permission($User->user, $subnet->id) == "0") continue;
/* We've found $slimit entries */
if ($valid_subnets >= $slimit) { break; }
if ($valid_subnets >= $slimit) break;
/* Make fields human readable */
$subnet->subnet = $Subnets->transform_to_dotted($subnet->subnet);
......@@ -48,7 +47,25 @@ if (is_array($all_subnets)) {
}
# only print if some hosts exist
if($valid_subnets>0) {
if($valid_subnets==0) {
$msg = $type_percentage ? _("Add some hosts to subnets to calculate usage percentage") : _("Add some hosts to subnets to show graph of used hosts per subnet");
print "<hr>";
print "<blockquote style='margin-top:20px;margin-left:20px;'>";
print "<p>"._("No $type_ip hosts configured")."</p>";
print "<small>".$msg."</small>";
print "</blockquote>";
#remove loading
?>
<script type="text/javascript">
$(document).ready(function() {
$("#<?php print $type_name; ?>").fadeOut('fast');
});
</script>
<?php
return;
}
?>
<script type="text/javascript">
$(function () {
......@@ -59,13 +76,21 @@ if($valid_subnets>0) {
# cast
$subnet = (array) $subnet;
if ($type_percentage === true) {
# set percentage because of localisation
$subnet['percentage'] = str_replace(",", ".", $subnet['percentage']);
$display_item = $subnet['percentage'];
} else {
$display_item = $subnet['usage'];
}
# odd/even if more than 5 items
if($valid_subnets > 5) {
if ($m&1) { print "['|<br>" . addslashes($subnet['description']) . "', $subnet[usage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
else { print "['" . addslashes($subnet['description']) . "', $subnet[usage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
if ($m&1) { print "['|<br>" . addslashes($subnet['description']) . "', $display_item, '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
else { print "['" . addslashes($subnet['description']) . "', $display_item, '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
}
else {
print "['" . addslashes($subnet['description']) . "', $subnet[usage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],";
print "['" . addslashes($subnet['description']) . "', $display_item, '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],";
}
}
?>
......@@ -78,7 +103,7 @@ if($valid_subnets>0) {
?>
//open link
$('#<?php print $type; ?>top10Hosts').bind('plotclick', function(event, pos, item) {
$('#<?php print $type_name; ?>').bind('plotclick', function(event, pos, item) {
//set prettylinks of not
if ($('#prettyLinks').html()=="Yes") { var plink = $("div.iebase").html()+"subnets/"+all_links[item.datapoint[0]]['sectionId']+"/"+ all_links[item.datapoint[0]]['id']+"/"; }
else { var plink = $("div.iebase").html()+"index.php?page=subnets&section="+all_links[item.datapoint[0]]['sectionId']+"&subnetId="+all_links[item.datapoint[0]]['id'] + ""; }
......@@ -103,7 +128,7 @@ if($valid_subnets>0) {
}
var previousPoint = null;
$("#<?php print $type; ?>top10Hosts").bind("plothover", function (event, pos, item) {
$("#<?php print $type_name; ?>").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
......@@ -116,16 +141,15 @@ if($valid_subnets>0) {
y = item.datapoint[1];
showTooltip(item.pageX, item.pageY,
data[x][2] + "<br>" + y + " hosts");
data[x][2] + "<br>" + y + "<?php print $type_percentage ? _("% used") : _(" hosts"); ?>");
}
$("#<?php print $type; ?>top10Hosts").css({'cursor':'pointer'});
$("#<?php print $type_name; ?>").css({'cursor':'pointer'});
}
else {
$("#tooltip").remove();
previousPoint = null;
$("#<?php print $type; ?>top10Hosts").css({'cursor':'default'});
$("#<?php print $type_name; ?>").css({'cursor':'default'});
}
});
......@@ -137,7 +161,7 @@ if($valid_subnets>0) {
barWidth: 0.6,
lineWidth: 1,
align: "center",
fillColor: "rgba(170, 70, 67, 0.8)"
fillColor: "<?php print $type_percentage ? "rgba(69, 114, 167, 0.7)" : "rgba(170, 70, 67, 0.8)"; ?>"
}
},
xaxis: {
......@@ -148,7 +172,9 @@ if($valid_subnets>0) {
show: true
},
yaxis: {
<?php if ($type_percentage) print "max: ".str_replace(",", ".", $top_subnets[0]->percentage); ?>
},
margin: {
top: 10,
left: 30,
......@@ -166,8 +192,8 @@ if($valid_subnets>0) {
show: false
},
shadowSize: 10,
highlightColor: '#AA4643',
colors: ['#AA4643' ],
highlightColor: "<?php print $type_percentage ? '#4572A7' : '#AA4643'; ?>",
colors: [ "<?php print $type_percentage ? '#4572A7' : '#AA4643'; ?>" ],
grid: {
show: true,
aboveData: false,
......@@ -186,50 +212,12 @@ if($valid_subnets>0) {
mouseActiveRadius: 3
}
};
<?php
if($valid_subnets!=0) {
?>
$.plot($("#<?php print $type; ?>top10Hosts"), [ data ], options);
<?php } else { ?>
$("#<?php print $type; ?>top10Hosts").hide();
<?php } ?>
});
</script>
<?php
if($valid_subnets==0) {
print "<hr>";
print "<blockquote style='margin-top:20px;margin-left:20px;'>";
print "<p>"._("No $type hosts configured")."</p>";
print "<small>"._("Add some hosts to subnets to show graph of used hosts per subnet")."</small>";
print "</blockquote>";
}
?>
<?php
}
else {
print "<hr>";
print "<blockquote style='margin-top:20px;margin-left:20px;'>";
print "<p>"._("No $type hosts configured")."</p>";
print "<small>"._("Add some hosts to subnets to show graph of used hosts per subnet")."</small>";
print "</blockquote>";
#remove loading
?>
<script type="text/javascript">
$(document).ready(function() {
$("#<?php print $type; ?>top10Hosts").fadeOut('fast');
$.plot($("#<?php print $type_name; ?>"), [ data ], options);
});
</script>
<?php
}
?>
<div id="<?php print $type; ?>top10Hosts" class="top10" style="height: <?php print $height; ?>px; width: 95%; margin-left: 3%; padding: 0px; position: relative; ">
<div id="<?php print $type_name; ?>" class="top10" style="height: <?php print $height; ?>px; width: 95%; margin-left: 3%; padding: 0px; position: relative; ">
<div style="text-align:center;padding-top:50px;"><strong><?php print _('Loading statistics'); ?></strong><br><i class='fa fa-spinner fa-spin'></i></div>
</div>
<?php
}
......@@ -44,6 +44,5 @@ if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") {
}
# get subnets statistic
$type = 'IPv4';
include "top10_hosts_lib.php";
require( "top10_hosts_lib.php" );
top10_widget('IPv4', false, $height, $slimit);
......@@ -44,6 +44,5 @@ if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") {
}
# get subnets statistic
$type = 'IPv6';
include "top10_hosts_lib.php";
require( "top10_hosts_lib.php" );
top10_widget('IPv6', false, $height, $slimit);
......@@ -42,222 +42,5 @@ if($_SERVER['HTTP_X_REQUESTED_WITH']!="XMLHttpRequest") {
}
# get subnets statistic
$type = 'IPv4';
$top_subnets = array();
$all_subnets = $Tools->fetch_top_subnets($type, 1000000, true);
# Find subnets with user access, label duplicates.
$unique = array();
$valid_subnets = 0;
foreach($all_subnets as $subnet) {
if ($Subnets->check_permission($User->user, $subnet->id) == "0") { continue; }
/* We've found $slimit entries */
if ($valid_subnets >= $slimit) { break; }
/* Make fields human readable */
$subnet->subnet = $Subnets->transform_to_dotted($subnet->subnet);
$subnet->descriptionLong = $subnet->description;
$subnet->description = $Tools->shorten_text($subnet->description, 20);
/* detect and rename duplicates */
if(isset($unique[$subnet->description])) {
$subnet->description = $subnet->description.' #'.sizeof($unique[$subnet->description]);
}
$unique[$subnet->description][] = $valid_subnets++;
/* Save */
$top_subnets[] = $subnet;
}
# set maximum for graph
$max = str_replace(",", ".", $top_subnets[0]->percentage);
# only print if some hosts exist
if($valid_subnets>0) {
?>
<script type="text/javascript">
$(function () {
var data = [
<?php
foreach ($top_subnets as $m => $subnet) {
# cast
$subnet = (array) $subnet;
# set percentage because of localisation
$subnet['percentage'] = str_replace(",", ".", $subnet['percentage']);
# odd/even if more than 5 items
if($valid_subnets > 5) {
if ($m&1) { print "['|<br>" . addslashes($subnet['description']) . "', $subnet[percentage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
else { print "['" . addslashes($subnet['description']) . "', $subnet[percentage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],"; }
}
else {
print "['" . addslashes($subnet['description']) . "', $subnet[percentage], '" . addslashes($subnet['descriptionLong']) . " ($subnet[subnet]/$subnet[mask])'],";
}
}
?>
];
//show tooltips
function showTooltip(x, y, contents) {
$('<div id="tooltip">' + contents + '</div>').css( {
position: 'absolute',
display: 'none',
top: y - 29,
left: x,
border: '1px solid white',
'border-radius': '4px',
padding: '4px',
'font-size': '11px',
'background-color': 'rgba(0,0,0,0.7)',
color: 'white'
}).appendTo("body").fadeIn(500);
}
//set JS array for clickable event
<?php
$allLinks = json_encode($top_subnets);
echo "var all_links = ". $allLinks. ";\n";
?>
//open link
$('#IPv4top10').bind('plotclick', function(event, pos, item) {
//set prettylinks of not
if ($('#prettyLinks').html()=="Yes") { var plink = $("div.iebase").html()+"subnets/"+all_links[item.datapoint[0]]['sectionId']+"/"+ all_links[item.datapoint[0]]['id']+"/"; }
else { var plink = $("div.iebase").html()+"index.php?page=subnets&section="+all_links[item.datapoint[0]]['sectionId']+"&subnetId="+all_links[item.datapoint[0]]['id'] + ""; }
//open
document.location = plink;
});
var previousPoint = null;
$("#<?php print $type; ?>top10").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0],
y = item.datapoint[1];
showTooltip(item.pageX, item.pageY,
data[x][2] + "<br>" + y + "% used");
}
$("#<?php print $type; ?>top10").css({'cursor':'pointer'});
}
else {
$("#tooltip").remove();
previousPoint = null;
$("#<?php print $type; ?>top10").css({'cursor':'default'});
}
});
var options = {
series: {
bars: {
show: true,
barWidth: 0.6,
lineWidth: 1,
align: "center",
fillColor: "rgba(69, 114, 167, 0.7)"
}
},
xaxis: {
mode: "categories",
tickLength: 0,
color: '#666',
tickLength: 1,
show: true
},
yaxis: {
max: <?php print $max; ?>
},
margin: {
top: 10,
left: 30,
bottom: 10,
right: 10
},
grid: {
hoverable: true,
clickable: true
},
bars: {
barWidth: 0.9
},
legend: {
show: false
},
shadowSize: 2,
highlightColor: '#4572A7',
colors: ['#4572A7' ],
grid: {
show: true,
aboveData: false,
color: "#666",
backgroundColor: "white",
/* margin: number or margin object */
/* labelMargin: number */
/* axisMargin: number */
/* markings: array of markings or (fn: axes -> array of markings) */
borderWidth: 0,
borderColor: null,
minBorderMargin: null,
clickable: true,
hoverable: true,
autoHighlight: true,
mouseActiveRadius: 3
}
};
<?php
if($valid_subnets!=0) {
?>
$.plot($("#<?php print $type; ?>top10"), [ data ], options);
<?php } else { ?>
$("#IPv4top10").hide();
<?php } ?>
});
</script>
<?php
if($valid_subnets==0) {
print "<hr>";
print "<blockquote style='margin-top:20px;margin-left:20px;'>";
print "<p>"._("No $type hosts configured")."</p>";
print "<small>"._("Add some hosts to subnets to show graph of used hosts per subnet")."</small>";
print "</blockquote>";
}
?>
<?php
}
else {
print "<hr>";
print "<blockquote style='margin-top:20px;margin-left:20px;'>";
print "<p>"._("No $type hosts configured")."</p>";
print "<small>"._("Add some hosts to subnets to calculate usage percentage")."</small>";
print "</blockquote>";
#remove loading
?>
<script type="text/javascript">
$(document).ready(function() {
$("#IPv4top10").fadeOut('fast');
});
</script>
<?php
}
?>
<div id="IPv4top10" class="top10" style="height:<?php print $height; ?>px;width:95%;margin-left:3%;">
<div style="text-align:center;padding-top:50px;"><strong><?php print _('Loading statistics'); ?></strong><br><i class='fa fa-spinner fa-spin'></i></div>
</div>
require( "top10_hosts_lib.php" );
top10_widget('IPv4', true, $height, $slimit);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment