Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ function drawHourlyChart()

for ($i = 0; $i < count($hourlyGraph); $i++) {
$hour = $hourlyGraph[$i]['label'];
$inTraffic = kbytesToString($hourlyGraph[$i]['rx'], true, $hourlyLargestPrefix);
$outTraffic = kbytesToString($hourlyGraph[$i]['tx'], true, $hourlyLargestPrefix);
$totalTraffic = kbytesToString($hourlyGraph[$i]['total'], true, $hourlyLargestPrefix);
$inTraffic = bytesToString($hourlyGraph[$i]['rx'], true, $hourlyLargestPrefix);
$outTraffic = bytesToString($hourlyGraph[$i]['tx'], true, $hourlyLargestPrefix);
$totalTraffic = bytesToString($hourlyGraph[$i]['total'], true, $hourlyLargestPrefix);

if (($hourlyGraph[$i]['label'] == "12am") && ($hourlyGraph[$i]['time'] == "0")) {
continue;
}

if ($i == 23) {
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n");
break;
} else {
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
Expand Down Expand Up @@ -149,16 +150,17 @@ function drawDailyChart()

for ($i = 0; $i < count($dailyGraph); $i++) {
$day = $dailyGraph[$i]['label'];
$inTraffic = kbytesToString($dailyGraph[$i]['rx'], true, $dailyLargestPrefix);
$outTraffic = kbytesToString($dailyGraph[$i]['tx'], true, $dailyLargestPrefix);
$totalTraffic = kbytesToString($dailyGraph[$i]['total'], true, $dailyLargestPrefix);
$inTraffic = bytesToString($dailyGraph[$i]['rx'], true, $dailyLargestPrefix);
$outTraffic = bytesToString($dailyGraph[$i]['tx'], true, $dailyLargestPrefix);
$totalTraffic = bytesToString($dailyGraph[$i]['total'], true, $dailyLargestPrefix);

if ($dailyGraph[$i]['time'] == "0") {
continue;
}

if ($i == 29) {
echo("['" . $day . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n");
break;
} else {
echo("['" . $day . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
Expand Down Expand Up @@ -188,12 +190,13 @@ function drawMonthlyChart()

for ($i = 0; $i < count($monthlyGraph); $i++) {
$hour = $monthlyGraph[$i]['label'];
$inTraffic = kbytesToString($monthlyGraph[$i]['rx'], true, $monthlyLargestPrefix);
$outTraffic = kbytesToString($monthlyGraph[$i]['tx'], true, $monthlyLargestPrefix);
$totalTraffic = kbytesToString($monthlyGraph[$i]['total'], true, $monthlyLargestPrefix);
$inTraffic = bytesToString($monthlyGraph[$i]['rx'], true, $monthlyLargestPrefix);
$outTraffic = bytesToString($monthlyGraph[$i]['tx'], true, $monthlyLargestPrefix);
$totalTraffic = bytesToString($monthlyGraph[$i]['total'], true, $monthlyLargestPrefix);

if ($i == 23) {
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n");
break;
} else {
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
Expand Down
57 changes: 34 additions & 23 deletions vnstat.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
*/

// $wSuf (without suffix MB, GB, etc)
function kbytesToString($kb, $wSuf = false, $byte_notation = null)
function bytesToString($bytes, $wSuf = false, $byte_notation = null)
{
//print $byte . (($byte_notation !== null)?(" " . $byte_notation):("")) . " / ";
$units = ['TB', 'GB', 'MB', 'KB'];
$scale = 1024 * 1024 * 1024 * 1024;
$ui = 0;

$custom_size = isset($byte_notation) && in_array($byte_notation, $units);

while ((($kb < $scale) && ($scale > 1)) || $custom_size) {
while ((($bytes < $scale) && ($scale > 1)) || $custom_size) {

if ($custom_size && $units[$ui] == $byte_notation) {
break;
Expand All @@ -36,9 +37,9 @@ function kbytesToString($kb, $wSuf = false, $byte_notation = null)
}

if ($wSuf == true) {
return sprintf("%0.2f", ($kb / $scale));
return sprintf("%0.2f", ($bytes / $scale));
} else {
return sprintf("%0.2f %s", ($kb / $scale), $units[$ui]);
return sprintf("%0.2f %s", ($bytes / $scale), $units[$ui]);
}
}

Expand Down Expand Up @@ -69,13 +70,13 @@ function getLargestValue($array)
});
}

function getLargestPrefix($kb)
function getLargestPrefix($bytes)
{
$units = ['TB', 'GB', 'MB', 'KB'];
$scale = 1024 * 1024 * 1024;
$scale = 1024 * 1024 * 1024 * 1024;
$ui = 0;

while ((($kb < $scale) && ($scale > 1))) {
while ((($bytes < $scale) && ($scale > 1))) {
$ui++;
$scale = $scale / 1024;
}
Expand Down Expand Up @@ -110,17 +111,21 @@ function getVnstatData($path, $type, $interface)
$monthlyGraph = [];
$monthly = [];
$top10 = [];
$version = 1;
if( isset( $vnstatDecoded['jsonversion'] )){
$version = $vnstatDecoded['jsonversion'];
}

$i = 0;
foreach ($vnstatDecoded['interfaces'][0]['traffic']['top'] as $top) {
if (is_array($top)) {
++$i;

$top10[$i]['label'] = date('d/m/Y', strtotime($top['date']['month'] . "/" . $top['date']['day'] . "/" . $top['date']['year']));
$top10[$i]['rx'] = kbytesToString($top['rx']);
$top10[$i]['tx'] = kbytesToString($top['tx']);
$top10[$i]['rx'] = bytesToString($top['rx']);
$top10[$i]['tx'] = bytesToString($top['tx']);
$top10[$i]['totalraw'] = ($top['rx'] + $top['tx']);
$top10[$i]['total'] = kbytesToString($top['rx'] + $top['tx']);
$top10[$i]['total'] = bytesToString($top['rx'] + $top['tx']);
}
}

Expand All @@ -130,10 +135,10 @@ function getVnstatData($path, $type, $interface)
++$i;

$daily[$i]['label'] = date('d/m/Y', mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']));
$daily[$i]['rx'] = kbytesToString($day['rx']);
$daily[$i]['tx'] = kbytesToString($day['tx']);
$daily[$i]['rx'] = bytesToString($day['rx']);
$daily[$i]['tx'] = bytesToString($day['tx']);
$daily[$i]['totalraw'] = ($day['rx'] + $day['tx']);
$daily[$i]['total'] = kbytesToString($day['rx'] + $day['tx']);
$daily[$i]['total'] = bytesToString($day['rx'] + $day['tx']);
$daily[$i]['time'] = mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']);

$dailyGraph[$i]['label'] = date('jS', mktime(0, 0, 0, $day['date']['month'], $day['date']['day'], $day['date']['year']));
Expand All @@ -149,18 +154,24 @@ function getVnstatData($path, $type, $interface)
if (is_array($hour)) {
++$i;

$hourly[$i]['label'] = date("ga", mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourly[$i]['rx'] = kbytesToString($hour['rx']);
$hourly[$i]['tx'] = kbytesToString($hour['tx']);
if( $version == 1 ){
$h = $hours['id'];
} else {
$h = $hour['time']['hour'];
}

$hourly[$i]['label'] = date("ga", mktime($h, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourly[$i]['rx'] = bytesToString($hour['rx']);
$hourly[$i]['tx'] = bytesToString($hour['tx']);
$hourly[$i]['totalraw'] = ($hour['rx'] + $hour['tx']);
$hourly[$i]['total'] = kbytesToString($hour['rx'] + $hour['tx']);
$hourly[$i]['time'] = mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);
$hourly[$i]['total'] = bytesToString($hour['rx'] + $hour['tx']);
$hourly[$i]['time'] = mktime($h, 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);

$hourlyGraph[$i]['label'] = date("ga", mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourlyGraph[$i]['label'] = date("ga", mktime($h, 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']));
$hourlyGraph[$i]['rx'] = $hour['rx'];
$hourlyGraph[$i]['tx'] = $hour['tx'];
$hourlyGraph[$i]['total'] = ($hour['rx'] + $hour['tx']);
$hourlyGraph[$i]['time'] = mktime($hour['id'], 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);
$hourlyGraph[$i]['time'] = mktime($h, 0, 0, $hour['date']['month'], $hour['date']['day'], $hour['date']['year']);
}
}

Expand All @@ -172,10 +183,10 @@ function getVnstatData($path, $type, $interface)
++$i;

$monthly[$i]['label'] = date('F', mktime(0, 0, 0, $month['date']['month'], 10));
$monthly[$i]['rx'] = kbytesToString($month['rx']);
$monthly[$i]['tx'] = kbytesToString($month['tx']);
$monthly[$i]['rx'] = bytesToString($month['rx']);
$monthly[$i]['tx'] = bytesToString($month['tx']);
$monthly[$i]['totalraw'] = ($month['rx'] + $month['tx']);
$monthly[$i]['total'] = kbytesToString($month['rx'] + $month['tx']);
$monthly[$i]['total'] = bytesToString($month['rx'] + $month['tx']);
$monthly[$i]['time'] = mktime(0, 0, 0, $month['date']['month'], 1, $month['date']['year']);

$monthlyGraph[$i]['label'] = date('F', mktime(0, 0, 0, $month['date']['month'], 10));
Expand Down