监控流量

来源:互联网 发布:软件加油站系统 编辑:程序博客网 时间:2024/04/29 08:07
$dev = isset($argv)?$argv:"eth0";while(true){    $result = file_get_contents("/proc/net/dev");    preg_match("/{$dev}:(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)/", $result, $preg_arr);    $lastre = $thisre;    $lasttr = $thistr;    $thisre = $preg_arr[1];    $thistr = $preg_arr[2];    $re = hbw($thisre - $lastre);    $tr = hbw($thistr - $lasttr);    echo "\33[2J";    printf("\33[1;1H%-12sss\n", 'Device', 'In', 'Out');    printf("\33[2;1H%-12sss\n", $dev, $re, $tr);    sleep(1);}function hbw($size) {    $size *= 8;    if($size > 1024 * 1024 * 1024) {        $size = round($size / 1073741824 * 100) / 100 . ' Gbps';    } elseif($size > 1024 * 1024) {        $size = round($size / 1048576 * 100) / 100 . ' Mbps';    } elseif($size > 1024) {        $size = round($size / 1024 * 100) / 100 . ' Kbps';    } else {        $size = $size . ' Bbps';    }    return $size;}
原创粉丝点击