-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed hourly traffic graph showing the wrong byte prefix (Issue #1) - Added an Hourly Traffic table as per #2
- Loading branch information
1 parent
89f7179
commit b3237d2
Showing
3 changed files
with
99 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
/* | ||
/* | ||
* Copyright (C) 2016 Alexander Marston ([email protected]) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
|
@@ -18,11 +18,11 @@ | |
*/ | ||
|
||
// $wSuf (without suffix MB, GB, etc) | ||
function kbytes_to_string($kb, $wSuf = false) { | ||
function kbytes_to_string($kb, $wSuf = false, $byte_notation = null) { | ||
$units = array('TB', 'GB', 'MB', 'KB'); | ||
$scale = 1024 * 1024 * 1024; | ||
$ui = 0; | ||
|
||
$custom_size = isset($byte_notation) && in_array($byte_notation, $units); | ||
|
||
while ((($kb < $scale) && ($scale > 1)) || $custom_size) { | ||
|
@@ -35,33 +35,53 @@ function kbytes_to_string($kb, $wSuf = false) { | |
} | ||
|
||
if ($wSuf == true) { | ||
return sprintf("%0.2f", ($kb/$scale)); | ||
return sprintf("%0.2f", ($kb / $scale)); | ||
} else { | ||
return sprintf("%0.2f %s", ($kb / $scale), $units[$ui]); | ||
} | ||
} | ||
|
||
function get_vnstat_interfaces($path) { | ||
$vnstat_interfaces = array(); // Create an empty array | ||
|
||
$vnstatIF = popen("$path --iflist", "r"); | ||
if (is_resource($vnstatIF)) { | ||
$iBuffer = ''; | ||
while (!feof($vnstatIF)) { | ||
$iBuffer .= fgets($vnstatIF); | ||
} | ||
|
||
$vnstat_temp = trim(str_replace("Available interfaces: ", "", $iBuffer)); | ||
|
||
$vnstat_interfaces = explode(" ", $vnstat_temp); | ||
pclose($vnstatIF); | ||
} | ||
|
||
return $vnstat_interfaces; | ||
} | ||
|
||
function get_vnstat_data($path, $type, $interface) { | ||
function get_largest_value($array) { | ||
return $max = array_reduce($array, function($a, $b) { | ||
return $a > $b['totalUnformatted'] ? $a : $b['totalUnformatted']; | ||
}); | ||
} | ||
|
||
function get_largest_prefix($kb) { | ||
$units = array('TB', 'GB', 'MB', 'KB'); | ||
$scale = 1024 * 1024 * 1024; | ||
$ui = 0; | ||
|
||
while ((($kb < $scale) && ($scale > 1))) { | ||
$ui++; | ||
$scale = $scale / 1024; | ||
} | ||
|
||
return $units[$ui]; | ||
} | ||
|
||
function get_vnstat_data($path, $type, $interface) { | ||
global $byte_formatter; | ||
|
||
$vnstat_information = array(); // Create an empty array for use later | ||
|
||
$vnstatDS = popen("$path --dumpdb -i $interface", "r"); | ||
|
@@ -79,6 +99,7 @@ function get_vnstat_data($path, $type, $interface) { | |
return; | ||
} | ||
|
||
$hourlyGraph = array(); | ||
$hourly = array(); | ||
$daily = array(); | ||
$monthly = array(); | ||
|
@@ -88,11 +109,21 @@ function get_vnstat_data($path, $type, $interface) { | |
$data = explode(";", trim($vnstat_line)); | ||
switch ($data[0]) { | ||
case "h": // Hourly | ||
// Set-up the hourly graph data | ||
$hourlyGraph[$data[1]]['time'] = $data[2]; | ||
$hourlyGraph[$data[1]]['label'] = date("ga", ($data[2] - ($data[2] % 3600))); | ||
$hourlyGraph[$data[1]]['rx'] = kbytes_to_string($data[3], true, $byte_formatter); | ||
$hourlyGraph[$data[1]]['tx'] = kbytes_to_string($data[4], true, $byte_formatter); | ||
$hourlyGraph[$data[1]]['total'] = kbytes_to_string($data[3] + $data[4], true, $byte_formatter); | ||
$hourlyGraph[$data[1]]['totalUnformatted'] = ($data[3] + $data[4]); | ||
$hourlyGraph[$data[1]]['act'] = 1; | ||
|
||
// Set up the hourly table data | ||
$hourly[$data[1]]['time'] = $data[2]; | ||
$hourly[$data[1]]['label'] = date("ga", ($data[2] - ($data[2] % 3600))); | ||
$hourly[$data[1]]['rx'] = kbytes_to_string($data[3], true); | ||
$hourly[$data[1]]['tx'] = kbytes_to_string($data[4], true); | ||
$hourly[$data[1]]['total'] = kbytes_to_string($data[3] + $data[4], true); | ||
$hourly[$data[1]]['rx'] = kbytes_to_string($data[3]); | ||
$hourly[$data[1]]['tx'] = kbytes_to_string($data[4]); | ||
$hourly[$data[1]]['total'] = kbytes_to_string($data[3] + $data[4]); | ||
$hourly[$data[1]]['act'] = 1; | ||
break; | ||
case "d": // Daily | ||
|
@@ -123,28 +154,35 @@ function get_vnstat_data($path, $type, $interface) { | |
} | ||
} | ||
|
||
usort($hourlyGraph, function ($item1, $item2) { | ||
if ($item1['time'] == $item2['time']) return 0; | ||
return $item1['time'] < $item2['time'] ? -1 : 1; | ||
}); | ||
|
||
usort($hourly, function ($item1, $item2) { | ||
if ($item1['time'] == $item2['time']) return 0; | ||
return $item1['time'] < $item2['time'] ? -1 : 1; | ||
}); | ||
|
||
usort($daily, function ($item1, $item2) { | ||
if ($item1['time'] == $item2['time']) return 0; | ||
return $item1['time'] > $item2['time'] ? -1 : 1; | ||
}); | ||
|
||
usort($monthly, function ($item1, $item2) { | ||
if ($item1['time'] == $item2['time']) return 0; | ||
return $item1['time'] > $item2['time'] ? -1 : 1; | ||
}); | ||
|
||
// Sort Top 10 Days by Highest Total Usage first | ||
usort($top10, function ($item1, $item2) { | ||
if ($item1['totalraw'] == $item2['totalraw']) return 0; | ||
return $item1['totalraw'] > $item2['totalraw'] ? -1 : 1; | ||
}); | ||
|
||
switch ($type) { | ||
case "hourlyGraph": | ||
return $hourlyGraph; | ||
case "hourly": | ||
return $hourly; | ||
case "daily": | ||
|