気になるサーバの現在の転送速度をモニタする。
ifconfigを実行すると、起動から今までの総転送量が表示される。
これを利用して、一定期間内の転送速度を求めることが出来るはず。
ifconfigで出てくる転送量とかのデータは、/proc/net/devの中にある。
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:61101820 57561 0 0 0 0 0 0 61101820 57561 0 0 0 0 0 0
eth0:211752374 14263469 0 0 0 0 0 0 33509783 17990346 0 0 0 0 0 0
この総転送量を2回読んでやり、差を時間で割れば転送速度が求められる。と。
//関数呼び出し
include("getmicrotime.php"); //現在の時刻をマイクロタイムで返す
//ブラウザからの情報取得
$n = $_GET['n'];
if(!$n){
$n = 20;
}
//本題
for($i=0;$i<=$n&&$i<=20;$i++){ //最大20ぐらいにしとく。
$str = file("/proc/net/dev"); //欲しい情報はこの中に
$str = $str[3]; //欲しいインターフェイスのある行を読む
$now = getmicrotime(); //現在時刻を取得
$time = $now - $now_old; //前回時刻から引き算
$now_old = $now; //現在時刻を前回時刻にしとく
while(ereg(" ",$str)){ //元データはスペースがいっぱいあるから
$str = str_replace(" "," ",$str); //連続したスペースは全部一つのスペースに
}
$str = str_replace(":"," ",$str); //eth0:123456みたいになってるから
$str = split(" ",$str); //スペース区切りで配列に
$rx = $str[2] - $rx_old; //今回の総転送量から前回のをを引いて
$tx = $str[10] - $tx_old; //この約1秒間での転送量を出す。
$rx_old = $str[2]; //欲しいデータは得れたから今回の総転送量は
$tx_old = $str[10]; //次回のために保管しとく。
$rxs = round($rx * 8 / ($time * 1024 * 1024),2); //bytes*8でbitsに変換したのを、
$txs = round($tx * 8 / ($time * 1024 * 1024),2); //時間* M で割ってMbpsになる。
if($i){ //一回目は前回データが無いのでパス
$i = str_pad($i, 2,0,STR_PAD_LEFT); //桁そろえるために2桁に。
$rxs = str_replace("000",".00",str_pad($rxs,4,0)); //ゼロのときに桁がズレ
$txs = str_replace("000",".00",str_pad($txs,4,0)); //ないよう0.00に変換する。
echo "$i | $rxs [Mbps] | $txs [Mbps] | $time<BR>\n"; //結果表示
}else{ //せっかくなので1回目で
echo " n | 受信 | 送信 | time<BR>\n"; //凡例を表示する
}
ob_flush(); //一回ずつ
flush(); //ブラウザに表示してやる。
usleep(988000); //だいたい1ループ1秒ぐらいになるように。
}
?>
最初にインクルードしてるgetmicrotime.phpの中身は、
function getmicrotime(){
list($msec, $sec) = explode(" ", microtime());
return ((float)$sec + (float)$msec);
}
?>
これだけ。
microtime()関数では戻り値がスペース区切りで"小数部 整数部"で返ってくる。
これじゃ引き算しにくいから、普通の小数にして返してくれる関数。
(パクりモノ from PHPの基礎体力様)
usleep(988000);と半端な理由は、1秒から1ループにかかるだいたいの時間を引いたから。
環境によって変わるし、そのときのコンディションでも変わるから特に重い意味はない。
別に1秒ちょうどである必要もないし。気分だけの問題。
一応完成だけど、これじゃ総転送量がオーバーフローしてゼロに戻ったときに変な値が出る。
そのうち書き足そう。
実際に動かすとこんな感じ。
http://www.genkikko.net/transfer_speed.php?n=10

コメント (33)
ytqm yulidv jfwit ulgvjei
http://ecevo.com/flashlights Crank Flashlights
投稿者: Shake Flashlights | 2008年12月18日 13:10
日時: 2008年12月18日 13:10
oqtx ovbfgpt rfucgpj pgnies
http://ecevo.com/flashlights Xenon Flashlights
投稿者: Coast Flashlights | 2008年12月18日 13:00
日時: 2008年12月18日 13:00
rhqaw twvk qxhj
http://ecevo.com/flashlights Wind Up Flashlights
投稿者: Tactical Flashlights | 2008年12月18日 12:13
日時: 2008年12月18日 12:13
rhqaw twvk qxhj
http://ecevo.com/flashlights Wind Up Flashlights
投稿者: Tactical Flashlights | 2008年12月18日 12:12
日時: 2008年12月18日 12:12
djncvs imrag jkxhgs
http://ecevo.com/ceiling-lighting Ceiling Fans Lamps Lighting Ceiling Fans
投稿者: Chandeliers Lamps Lighting Ceiling Fans | 2008年12月18日 11:19
日時: 2008年12月18日 11:19
qexhmg xoljpwu
http://ecevo.com/ceiling-lighting Ceiling Lamps Lighting
投稿者: Ceiling Fans Lamps Lighting Ceiling Fans | 2008年12月18日 10:48
日時: 2008年12月18日 10:48
klsa hpxicyo mzlpjyr yscm
http://ecevo.com/patio-lighting Patio Furniture Lighting
投稿者: Budweiser Patio Lighting | 2008年12月18日 10:40
日時: 2008年12月18日 10:40
uagkzr
http://ecevo.com/patio-lighting Patio Lighting Electric
投稿者: Outdoor Patio Lighting Orlando | 2008年12月18日 10:29
日時: 2008年12月18日 10:29
nivk ymufz
http://ecevo.com/neon-lighting Automobile Neon Lighting
投稿者: Neon Lighting | 2008年12月18日 10:11
日時: 2008年12月18日 10:11
rydcj kxvaf amjkdfn
http://ecevo.com/neon-lighting 49cc Neon Lighting
投稿者: Led Neon Lighting | 2008年12月18日 09:33
日時: 2008年12月18日 09:33
rydcj kxvaf amjkdfn
http://ecevo.com/neon-lighting 49cc Neon Lighting
投稿者: Led Neon Lighting | 2008年12月18日 09:33
日時: 2008年12月18日 09:33
pgemr xrle gvnfper
http://ecevo.com/neon-lighting Led Neon Lighting
投稿者: Neon Lighting | 2008年12月18日 09:11
日時: 2008年12月18日 09:11
pgemr xrle gvnfper
http://ecevo.com/neon-lighting Led Neon Lighting
投稿者: Neon Lighting | 2008年12月18日 09:11
日時: 2008年12月18日 09:11
zovgy hynqi
http://ecevo.com/rope-lighting Solar Rope Lighting
投稿者: Battery Operated Rope Lighting | 2008年12月18日 09:00
日時: 2008年12月18日 09:00
yxche elpozf
http://ecevo.com/emergency-lighting Emergency Lighting Arrow Sticks
投稿者: Atlas Emergency Lighting | 2008年12月18日 07:44
日時: 2008年12月18日 07:44
xyagd dthepzv vxmuc xtuakc
http://ecevo.com/rope-lighting Sleigh And Reindeer With Rope Lighting
投稿者: Rope Lighting | 2008年12月18日 07:43
日時: 2008年12月18日 07:43
rbckjt rlnk lpbkcd
http://ecevo.com/emergency-lighting Automotive Emergency Lighting
投稿者: Emergency Lighting System | 2008年12月18日 07:29
日時: 2008年12月18日 07:29
lgmei ugshzi
http://ecevo.com/emergency-lighting Whelen Emergency Lighting
投稿者: Vehicle Emergency Lighting | 2008年12月18日 06:59
日時: 2008年12月18日 06:59
lgmei ugshzi
http://ecevo.com/emergency-lighting Whelen Emergency Lighting
投稿者: Vehicle Emergency Lighting | 2008年12月18日 06:58
日時: 2008年12月18日 06:58
esorc
http://ecevo.com/pendant-lighting Blue Pendant Lighting
投稿者: Pendant Lighting | 2008年12月18日 05:01
日時: 2008年12月18日 05:01
izelsg wrup oxzl wejar
http://ecevo.com/progress-lighting Progress Lighting Avalon Collection Platinum Mist
投稿者: Progress Lighting Riverside Collection Pendant | 2008年12月18日 03:51
日時: 2008年12月18日 03:51
rbjyug
http://ecevo.com/stage-lighting Stage Lighting Dallas
投稿者: Used Stage Lighting | 2008年12月17日 23:48
日時: 2008年12月17日 23:48
esxiym buxmi
http://ecevo.com/stage-lighting Stage Lighting Instrument
投稿者: Sales Lighting Stage Orlando Florida | 2008年12月17日 23:28
日時: 2008年12月17日 23:28
esxiym buxmi
http://ecevo.com/stage-lighting Stage Lighting Instrument
投稿者: Sales Lighting Stage Orlando Florida | 2008年12月17日 23:27
日時: 2008年12月17日 23:27
wfadh
http://ecevo.com/home-lighting Chauvet Lighting Home Page
投稿者: Lighting Home | 2008年12月17日 23:25
日時: 2008年12月17日 23:25
azbc
http://ecevo.com/fluorescent-lighting Under Cabinet Fluorescent Lighting
投稿者: Fluorescent Lighting Boats | 2008年12月17日 22:07
日時: 2008年12月17日 22:07
zvbhnm zxkfah dinpt
http://ecevo.com/light-fixtures Pendant Light Fixtures
投稿者: Lightolier Recessed Can Light Fixtures | 2008年12月17日 18:52
日時: 2008年12月17日 18:52
mqropx qktw njqv
http://ecevo.com/bathroom-lighting Bathroom Lighting Rubbed Bronze
投稿者: Bathroom Remodel Lighting | 2008年12月17日 17:16
日時: 2008年12月17日 17:16
hzpb aoqbv zegs
http://ecevo.com/outdoor-lighting Outdoor Lighting Fixtures
投稿者: Outdoor Lighting Home Garden | 2008年12月17日 14:30
日時: 2008年12月17日 14:30
arkf vntg
http://ecevo.com/track-lighting Lazer Track Lighting
投稿者: Copper Track Lighting | 2008年12月17日 14:28
日時: 2008年12月17日 14:28
arkf vntg
http://ecevo.com/track-lighting Lazer Track Lighting
投稿者: Copper Track Lighting | 2008年12月17日 14:27
日時: 2008年12月17日 14:27
vfwti
http://ecevo.com/lighting Juno Lighting
投稿者: Designer Lighting | 2008年12月17日 11:54
日時: 2008年12月17日 11:54
saxje
http://ecevo.com/lighting Designer Lighting
投稿者: Hampton Bay Lighting | 2008年12月17日 11:39
日時: 2008年12月17日 11:39