Mini Shell
Direktori : /opt/sharedrads/ |
|
Current File : //opt/sharedrads/check_traffic |
#!/usr/local/bin/php
<?php
// Finds highest bandwidth users
// CLI arguments
if ($argc < 3) {
die(" Usage: check_bandwidth <month (1-12)> <year (2011)\n");
}
// remove first argument
array_shift($argv);
// assign remaining arguments to variables
$month = $argv[0];
$year = $argv[1];
// Time Vars
$now=time();
$then=mktime(0,0,0,$month,1,$year);
$seconds=$now-$then;
// API Vars
$theServer = "localhost";
$user = "root";
$apiPath = "/xml-api/showbw?month=$month&year=$year";
# Make hash into one long string, in case it isn't already
$rhash = file_get_contents("/root/.accesshash");
$hash = str_replace("\n","",$rhash);
$newhash = str_replace(" ","",$hash);
//echo $newhash;
# Open a socket for HTTPS
$fp = fsockopen("ssl://" . $theServer, 2087, $errno, $errstr, 30);
# Die on error initializing socket
if ($errno == 0 && $fp == FALSE) {
die("Socket Error: Could not initialize socket.");
} elseif ($fp == FALSE) {
die("Socket Error #" . $errno . ": " . $errstr);
}
# Assemble the header to send
$header = "";
$header .= "GET " . $apiPath . " HTTP/1.0\r\n";
$header .= "Host: " . $theServer . "\r\n";
$header .= "Connection: Close\r\n";
$header .= "Authorization: WHM " . $user . ":" . $newhash . "\r\n";
$header .= "\r\n";
# Send the Header
fputs($fp, $header);
# Get the raw output from the server
$rawResult = "";
while (!feof($fp)) {
$rawResult .= @fgets($fp, 128); // Suppress errors with @
}
# Close the socket
fclose($fp);
# Ignore headers
$rawResultParts = explode("\r\n\r\n",$rawResult);
$xmlresult = $rawResultParts[1];
# Output XML
$xmlObject=simplexml_load_string($xmlresult);
foreach( $xmlObject->bandwidth->acct as $acct )
{
$user = $acct->user;
$bytes = $acct->totalbytes;
# Convert bytes to Mbits (8 bits in one byte, div by 1024 and again by 1024)
$mbits = ($bytes*8/1024/1024);
$persec = ($mbits/$seconds);
$mbits = round($persec,2);
echo "$user: $mbits Mb/s\n";
}
?>
Zerion Mini Shell 1.0