Skip to content

Instantly share code, notes, and snippets.

@fliphess
Created August 31, 2016 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliphess/c4d96a36d964d0890b0ed76a2de70bd0 to your computer and use it in GitHub Desktop.
Save fliphess/c4d96a36d964d0890b0ed76a2de70bd0 to your computer and use it in GitHub Desktop.
A wrapper around vnstat to view network usage of a server
<?php
header('Pragma: no-cache');
header('Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate');
$domain = $_SERVER["SERVER_NAME"];
$url = $_SERVER["SCRIPT_NAME"];
$options = ['summary', 'hours', 'days', 'months', 'top10'];
$option = filter_input(INPUT_GET, "option", FILTER_SANITIZE_STRING);
if (!isset($option) or empty($option) or !in_array($option, $options)) {
$option = 'summary';
}
$command = sprintf('nice -n 10 vnstati -i eth0 --transparent --style 2 -ne --%s -o - 2>&1', $option);
$image_base = shell_exec(sprintf($command, $option));
if (preg_match('Not enough data available yet', $image_base)) {
die('Not enough data available yet');
}
$image_source = base64_encode($image_base);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bandwidth Statistics</title>
<link rel="shortcut icon" type="image/png" href="https://downtek.com/favicon.ico"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js "></script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid responsive">
<div id="navbar" class="navbar-collapse pull-right">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Available Views <span class="caret"></span></a>
<ul class="dropdown-menu">
<?php foreach ($options as $value) { printf('<li><a href="%s?option=%s">%s</a></li>', $url, $value, $value); } ?>
</ul>
</li>
</ul>
<ul class="nav navbar-text pull-right">Current view: <?php echo $option; ?></ul>
</div>
</div>
</nav>
<center>
<div class="container fluid center">
<div class="vnstati" id="layout"><img src='data:image/png;base64,<?php echo $image_source; ?>' alt='Error while processing image'/></a>
</div>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment