Skip to content

Commit

Permalink
Major fix and enhancement added:
Browse files Browse the repository at this point in the history
- Fixed hourly traffic graph showing the wrong byte prefix (Issue #1)
- Added an Hourly Traffic table as per #2
  • Loading branch information
alexandermarston committed Nov 17, 2016
1 parent 89f7179 commit b3237d2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
5 changes: 4 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
// Set to true to set your own interfaces
$use_predefined_interfaces = false;

// Byte format to use in graphs
$byte_formatter = "MB";

if ($use_predefined_interfaces == true) {
$interface_list = array("eth0", "eth1");

Expand All @@ -37,4 +40,4 @@
{
$interface_name[$interface] = $interface;
}
}
}
48 changes: 41 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ function drawChart() {
var data = google.visualization.arrayToDataTable([
['Hour', 'Traffic In', 'Traffic Out', 'Total Traffic'],
<?php
$hourly = get_vnstat_data($vnstat_bin_dir, "hourly", $thisInterface);
$hourlyGraph = get_vnstat_data($vnstat_bin_dir, "hourlyGraph", $thisInterface);

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


if ($i == 23) {
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "]\n");
Expand All @@ -88,7 +89,7 @@ function drawChart() {
var options = {
title: 'Hourly Network Traffic',
subtitle: 'over last 24 hours',
vAxis: {format: '##.## MB'}
vAxis: {format: '##.## <?php echo $byte_formatter; ?>'}
};

var chart = new google.charts.Bar(document.getElementById('hourlyNetworkTrafficGraph'));
Expand All @@ -109,6 +110,7 @@ function drawChart() {
<div id="tabNav" class="container">
<ul class="nav nav-tabs">
<li class="active"><a href="#daily" data-toggle="tab">Daily</a></li>
<li><a href="#hourly" data-toggle="tab">Hourly</a></li>
<li><a href="#monthly" data-toggle="tab">Monthly</a></li>
<li><a href="#top10" data-toggle="tab">Top 10</a></li>
</ul>
Expand Down Expand Up @@ -148,6 +150,38 @@ function drawChart() {
</tbody>
</table>
</div>
<div class="tab-pane" id="hourly">
<table class="table table-bordered">
<thead>
<tr>
<th>Hour</th>
<th>Received</th>
<th>Sent</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
$hourly = get_vnstat_data($vnstat_bin_dir, "hourly", $thisInterface);

for ($i = 0; $i < count($hourly); $i++) {
$hour = $hourly[$i]['label'];
$totalReceived = $hourly[$i]['rx'];
$totalSent = $hourly[$i]['tx'];
$totalTraffic = $hourly[$i]['total'];
?>
<tr>
<td><?php echo $hour; ?></td>
<td><?php echo $totalReceived; ?></td>
<td><?php echo $totalSent; ?></td>
<td><?php echo $totalTraffic; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="tab-pane" id="monthly">
<table class="table table-bordered">
<thead>
Expand Down
70 changes: 54 additions & 16 deletions vnstat.php
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
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -79,6 +99,7 @@ function get_vnstat_data($path, $type, $interface) {
return;
}

$hourlyGraph = array();
$hourly = array();
$daily = array();
$monthly = array();
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit b3237d2

Please sign in to comment.