Skip to content

Instantly share code, notes, and snippets.

@hadleyrich
Created June 28, 2012 11:01
Show Gist options
  • Save hadleyrich/3010678 to your computer and use it in GitHub Desktop.
Save hadleyrich/3010678 to your computer and use it in GitHub Desktop.
Hacked this up a couple years back for someone to show data usage from a local RADIUS file system database on pfSense.
<?php
//online time
//sessions
$logdir = '/var/log/radacct/192.168.1.1';
$users = array();
$packets = array();
function format_bytes($b, $p=null) {
$units = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
$c=0;
if(!$p && $p !== 0) {
foreach($units as $k => $u) {
if(($b / pow(1024,$k)) >= 1) {
$r["bytes"] = $b / pow(1024,$k);
$r["units"] = $u;
$c++;
}
}
return number_format($r["bytes"],2) . " " . $r["units"];
} else {
return number_format($b / pow(1024,$p)) . " " . $units[$p];
}
}
if ($dir = opendir($logdir)) {
while (false !== ($file = readdir($dir))) {
if (strpos($file, 'detail-') === false) {
continue;
}
$handle = fopen($logdir.'/'.$file, 'r');
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
// Match empty lines
if (strlen(trim($buffer)) == 0) {
continue;
}
// Match lines that don't start with tabs
if ($buffer{0} != "\t") {
if (isset($packet['type'])) {
$packets[] = $packet;
}
$packet = array();
}
if (strpos($buffer, 'Acct-Status-Type')) {
$bits = explode(' = ', $buffer);
$data = trim($bits[1]);
$packet['type'] = $data;
}
if (strpos($buffer, 'User-Name')) {
$bits = explode(' = ', $buffer);
$data = str_replace('"', '', trim($bits[1]));
$packet['user'] = $data;
}
if (strpos($buffer, 'Acct-Input-Octets')) {
$bits = explode(' = ', $buffer);
$data = trim($bits[1]);
$packet['input_octets'] = $data;
}
if (strpos($buffer, 'Acct-Output-Octets')) {
$bits = explode(' = ', $buffer);
$data = trim($bits[1]);
$packet['output_octets'] = $data;
}
if (strpos($buffer, 'Acct-Input-Gigawords')) {
$bits = explode(' = ', $buffer);
$data = trim($bits[1]);
$packet['input_gigawords'] = $data;
}
if (strpos($buffer, 'Acct-Output-Gigawords')) {
$bits = explode(' = ', $buffer);
$data = trim($bits[1]);
$packet['output_gigawords'] = $data;
}
}
fclose($handle);
// Grab the last packet
if (isset($packet['type'])) {
$packets[] = $packet;
}
}
}
foreach ($packets as $packet) {
if ($packet['type'] == 'Stop') {
if (empty($users[$packet['user']])) {
$users[$packet['user']] = array(
'in' => 0,
'out' => 0,
'total' => 0,
);
}
$bytes_in = $packet['input_octets'];
$bytes_out = $packet['output_octets'];
if (!empty($packet['input_gigawords'])) {
$bytes_in += ($packet['input_gigawords'] * 4294967296);
}
if (!empty($packet['output_gigawords'])) {
$bytes_out += ($packet['output_gigawords'] * 4294967296);
}
$users[$packet['user']]['in'] += $bytes_in;
$users[$packet['user']]['out'] += $bytes_out;
$users[$packet['user']]['total'] += $bytes_in + $bytes_out;
}
}
foreach ($users as $user => $info) {
print "$user: ".format_bytes($info['total']).' ('.format_bytes($info['in']).'/'.format_bytes($info['out']).")\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment