From 0787c3d20b9ed0a07e363214fa70e6e0212ae619 Mon Sep 17 00:00:00 2001 From: extremeCrazyCoder Date: Tue, 12 Feb 2019 20:47:08 +0100 Subject: [PATCH 1/4] Fixed bug inside getLargestPrefix@vnstat.php changed variable names from kb to bytes getLargestPrefix,bytesToString@vnstat.php changed function name from kbytesToString to bytesToString --- index.php | 18 +++++++++--------- vnstat.php | 39 ++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/index.php b/index.php index 0eca23b..3cc52fe 100644 --- a/index.php +++ b/index.php @@ -110,9 +110,9 @@ 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; @@ -149,9 +149,9 @@ 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; @@ -188,9 +188,9 @@ 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"); diff --git a/vnstat.php b/vnstat.php index 849ecbb..89770ad 100644 --- a/vnstat.php +++ b/vnstat.php @@ -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; @@ -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]); } } @@ -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; } @@ -117,10 +118,10 @@ function getVnstatData($path, $type, $interface) ++$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']); } } @@ -130,10 +131,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'])); @@ -150,10 +151,10 @@ function getVnstatData($path, $type, $interface) ++$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']); + $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]['total'] = bytesToString($hour['rx'] + $hour['tx']); $hourly[$i]['time'] = mktime($hour['id'], 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'])); @@ -172,10 +173,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)); From 88e9e7628d2d94de276ae6c651e619456bd985fa Mon Sep 17 00:00:00 2001 From: Benjamin Close Date: Wed, 19 Jun 2019 21:56:07 +0930 Subject: [PATCH 2/4] Deal with v2 json output from vnstat defining hours differently --- vnstat.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/vnstat.php b/vnstat.php index 849ecbb..d558c04 100644 --- a/vnstat.php +++ b/vnstat.php @@ -110,6 +110,10 @@ 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) { @@ -149,18 +153,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'])); + 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'] = kbytesToString($hour['rx']); $hourly[$i]['tx'] = kbytesToString($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]['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']); } } From 87adc8c52c00975463fc69c2d22719afba769721 Mon Sep 17 00:00:00 2001 From: Benjamin Close Date: Fri, 21 Jun 2019 22:25:34 +0930 Subject: [PATCH 3/4] Add missing break which cause the javascript to become invalid This break makes sure at most 1 day is displayed in the hours graph. --- index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/index.php b/index.php index 0eca23b..589cff3 100644 --- a/index.php +++ b/index.php @@ -120,6 +120,7 @@ function drawHourlyChart() if ($i == 23) { echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n"); + break; } else { echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n"); } From a1722319012981e64668ffd42e409bba735f2766 Mon Sep 17 00:00:00 2001 From: Benjamin Close Date: Tue, 2 Jul 2019 22:02:34 +0930 Subject: [PATCH 4/4] Add missing break statements to daily & monthly output --- index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.php b/index.php index 1536eef..c4e561e 100644 --- a/index.php +++ b/index.php @@ -160,6 +160,7 @@ function drawDailyChart() if ($i == 29) { echo("['" . $day . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n"); + break; } else { echo("['" . $day . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n"); } @@ -195,6 +196,7 @@ function drawMonthlyChart() if ($i == 23) { echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n"); + break; } else { echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n"); }