Created
June 19, 2015 21:38
-
-
Save Ranlvor/223a61e8f980bb452206 to your computer and use it in GitHub Desktop.
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
<?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