NB. The ‘$(document).ready’ function will need some input from the script expert on how to set an appropriate ‘yaxis: { max:’ figure during install. 55000 works perfectly on my 400Mb VPS.
widgets\data.php
<?php
$interface = venet0;
session_start();
$rx[] = @file_get_contents("/sys/class/net/venet0/statistics/rx_bytes");
$tx[] = @file_get_contents("/sys/class/net/venet0/statistics/tx_bytes");
sleep(1);
$rx[] = @file_get_contents("/sys/class/net/venet0/statistics/rx_bytes");
$tx[] = @file_get_contents("/sys/class/net/venet0/statistics/tx_bytes");
$tbps = $tx[1] - $tx[0];
$rbps = $rx[1] - $rx[0];
$round_rx=round($rbps/1024, 1);
$round_tx=round($tbps/1024, 1);
$time=date("U")."000";
$_SESSION['rx'][] = "[$time, $round_rx]";
$_SESSION['tx'][] = "[$time, $round_tx]";
//$data['label'] = "1";
//$data['data'] = $_SESSION['rx'];
# to make sure that the graph shows only the
# last minute (saves some bandwitch to)
if (count($_SESSION['rx'])>60)
{
$x = min(array_keys($_SESSION['rx']));
unset($_SESSION['rx'][$x]);
$x2 = min(array_keys($_SESSION['tx']));
unset($_SESSION['tx'][$x2]);
}
// # json_encode didnt work, if you found a workarround pls write m
//echo json_encode($data, JSON_FORCE_OBJECT);
echo '[ { "data":['.implode($_SESSION['rx'], ",").'],"label": "Download"}, ';
echo '{ "data":['.implode($_SESSION['tx'], ",").'],"label": "Upload"} ';
echo ']';
?>
/inc/panel.header.php
$(document).ready(function() {
var options = {
series: {
lines: {
show: [true],
fill: [true],
fillColor: { colors: [{ opacity: 0.02 }, { opacity: 1}] }
}
},
border: { show: [true, false] },
points: { show: false },
xaxis: { mode: "time" },
yaxis: { max: 55000, min: 0, axislabel: "MB/s"}, // The max value will need to be set accordingly for each server's internet speed.
colors: ["#6600AA", "#10D2E5"],
shadowSize:
};
var data = [];
var placeholder = $("#mainbw");
$.plot(placeholder, data, options);
var iteration = 0;
function fetchData() {
++iteration;
function onDataReceived(series) {
// we get all the data in one go, if we only got partial
// data, we could merge it with what we already got
data = series;
var updateInterval = 30;
$.plot($("#mainbw"), data, options);
fetchData();
}
$.ajax({
url: "widgets/data.php",
method: 'GET',
dataType: 'json',
success: onDataReceived
});
}
setTimeout(fetchData, 30);
});