R,
## transmit : $bw->T at intervals of $bw->usec seconds
\n
## _EOF_;
##
class bandwidth
{
var $R, $T, $usec;
var $BW = array();
## 3 arguments :
## $bps => 0(MBPS,default), 1(MBytes/s)
## $iface => check interface(default to 'eth0')
## $file => create tmp php file(default to '/tmp/bandwidth.cfg.php')
##
## return values :
## $this->R : Received bandwidth
## $this->T : Transmited bandwidth
## $this->usec : time(seconds)
## $this->BW = array('R'=>$this->R,'T'=>$this->T,'usec'=>$this->usec);
##
function bandwidth($bps=0, $iface='eth0', $file='/tmp/bandwidth.cfg.php')
{
if(!$os = $this->support_os())
{
$this->R = $this->T = 'not support this os('.PHP_OS.')';
return 0;
}
$func = 'get_netstat_'.$os;
if(!$stat = $this->$func($iface))
{
$this->R = $this->T = 'can\'t open iface('.$iface.')'.
' or can\'t running netstat command';
return 0;
}
if(function_exists('sem_get') && function_exists('shm_attach'))
{
$_BW = $this->get_shm();
$use_shm = 1;
}
else if(file_exists($file)) @include_once $file;
$time = explode(' ',microtime());
$usec = sprintf('%.6f',$time[0] + $time[1]);
## change values, and to write
##
if($use_shm)
$this->get_shm(array('usec'=>$usec,'R'=>$stat['R_bytes'],'T'=>$stat['T_bytes']));
else {
$content = "";
$this->put_file($file,$content);
}
if($_BW && is_array($_BW))
{
$usec -= $_BW['usec'] ;
$this->usec = number_format($usec,2);
$stat['R_bytes'] -= $_BW['R'];
$stat['T_bytes'] -= $_BW['T'];
$this->R = $this->get_unit($stat['R_bytes']/$usec,$bps);
$this->T = $this->get_unit($stat['T_bytes']/$usec,$bps);
$this->BW = array('R'=>$this->R,'T'=>$this->T,'usec'=>$this->usec);
}
}
function support_os()
{
$os = strtolower(PHP_OS);
if(preg_match('/linux|freebsd/i',$os)) return $os;
}
function get_netstat_linux($iface = 'eth0')
{
if(!$netstat = @file('/proc/net/dev')) return 0;
for($i=3; $i MBPS(MBits/sec)
## $bps(1) => MBytes/sec
##
function get_unit($size, $bps=0)
{
if($bps) $unit = ' MBytes/s';
else { $size *= 8; $unit = ' MBPS'; }
return number_format($size/1048576,2).$unit;
}
function put_diofile($file, $content)
{
if($fp = @dio_open($file,O_WRONLY+O_CREAT,0600)) {
$bytes = dio_write($fp,$content);
dio_close($fp);
}
//return $bytes;
}
function put_file($file, $content)
{
if(function_exists(dio_open)) return $this->put_diofile($file,$content);
if($fp = @fopen($file,'w')) {
fputs($fp, $content);
fclose($fp);
}
}
## http://www.php.net/manual/en/ref.sem.php
## http://www.php.net/manual/en/function.shm-attach.php
##
function get_shm($array=0)
{
$_shm['size'] = 512; // about 260 ~ bytes
$_shm['key'] = 0x7068705f;
$_shm['max'] = 10;
if(!$id['sem'] = sem_get($_shm['key'],$_shm['max'])) return 0;
if(!@sem_acquire($id['sem']))
{ @sem_remove($id['sem']); return 0; }
if(!$id['shm'] = shm_attach($_shm['key'],$_shm['size']))
{ @sem_remove($id['sem']); return 0; }
$return = @shm_get_var($id['shm'],$_shm['key']);
if($array) {
if(!shm_put_var($id['shm'],$_shm['key'],$array))
{ @sem_remove($id['sem']); @shm_remove($id['shm']); return 0; }
}
@shm_detach($id['shm']);
@sem_release($id['sem']);
return $return;
}
} // end of this class
/***
## example
##
$bw = new bandwidth();
echo <<<_EOF_
receive : $bw->R,
transmit : $bw->T at intervals of last $bw->usec seconds
\n
_EOF_;
var_dump($bw->BW);
***/
?>