Skip to content

Instantly share code, notes, and snippets.

@Ranlvor
Created June 19, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ranlvor/223a61e8f980bb452206 to your computer and use it in GitHub Desktop.
Save Ranlvor/223a61e8f980bb452206 to your computer and use it in GitHub Desktop.
<?php
global $interface;
global $mesurements;
global $file;
$interface = "br-fftr:";
$mesurements = 120;
$file = "/var/www/html/realtime-stats/bandwidth.json";
function data_add(&$array, $value) {
global $mesurements;
array_push($array, $value);
while(sizeof($array) > $mesurements)
array_shift($array);
}
function getMesurement() {
global $interface;
$raw = file_get_contents("/proc/net/dev");
$time = time();
$lines = explode("\n", $raw);
foreach($lines as $line) {
$line = trim($line);
$line = preg_replace("/ +/", " ", $line);
$columns = explode(" ", $line);
if($columns[0] === $interface) {
$in = $columns[1];
$out = $columns[9];
return array($time, $in, $out);
}
}
}
function doDelta($previous, $current) {
return array(
$current[0],
$current[1]-$previous[1],
$current[2]-$previous[2]
);
}
function save($data) {
global $file;
$content = json_encode($data);
file_put_contents($file,$content);
}
$data = array();
$previous = getMesurement();
while (true) {
$current = getMesurement();
$dataPoint = doDelta($previous, $current);
$previous = $current;
data_add($data, $dataPoint);
save($data);
print_r($dataPoint);
sleep (1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment