2012-04-11 28 views
2

node.jsをWebサーバとして使用して各ドメインの帯域幅の使用状況を監視するにはどうすればよいですか?node.jsドメインごとの帯域幅使用量を計算する

APIコールを知っている人はいますか?

また、帯域幅で課金しているマルチテナント環境で他の人が使用したモジュールや別の方法はありますか?

更新:

は、誰もが任意のWebサーバーの前に置くことができ、軽量プロキシ/サーバーのドメインを検査することによって、これらの帯域幅の統計情報を記録することができます(Node.jsのは、Apacheなど)を知っています

+0

Express、Connect、カスタムコードを使用していますか? – loganfsmyth

+0

モジュールまたはフレームワークにロックされていません。 node.jsをWebサーバーとして使用して達成する方法を知りたいだけです。 – Matt

+0

これを行うAPI呼び出しはありません。 Node.js Webサーバーを制御していますか、ホストしている他の人々のアプリケーションを監視していますか?前者の場合、私はいくつかのコードを提案することができます。後者の場合は、私はプロキシを提案することができます。 –

答えて

0

Http Traceをご覧ください。

HTTPとWebSocketのトラフィックをキャプチャ、デコード、分析するnode.jsモジュールです。 各リクエストのサイズとドメインを見つけることができるので、いくつか調整することで、ここで達成したいことを実行できるはずです。

ノードv0.6.15の私のUbuntuサーバーではうまくいきました。「apt-get libpcap0.8-devをインストールする」だけでした。

+0

これはちょうど応答ボディに返されたメッセージのサイズを記録していると思われます。 – Matt

3

node.jsコアを変更しないと、最も良い選択肢は、bytesReadおよびbytesWritten変数を使用してソケットレベルでそれを追跡することです。

実際には、転送されたデータを計算するために他の帯域幅トラッカーが行うように、http要求/応答文字列のサイズを測定するより正確です。

2つのオプションがあります。1)各要求でバイトを記録する。または2)TCP接続のバイトをログに記録する。

リクエストレベルでロギングすると、リアルタイムデータが提供され、便利なデータになります。しかし、ロギングの実装方法によっては処理が遅くなる可能性があります。おそらくそれをメモリに保存し、それを頻繁にディスク/ DBにダンプするのが最善です。

要求レベルのログ:ソケットレベルで

var http = require('http'); 
var server = http.createServer(function (req, res) { 
    // setup a counter for bytes already logged 
    req.connection.bytesLogged = (req.connection.bytesLogged || 0); 

    // output the bytes read by the socket 
    console.log('Socket [' + req.connection.remoteAddress + '] [' + req.headers.host + '] - Bytes Read: ' + req.connection.bytesRead); 

    // calculate the bytes of the request (includes headers and other tcp overhead - more realistic) 
    req.bytes = req.connection.bytesRead - req.connection.bytesLogged; 
    // output the bytes size of the request (note this is calculated after the TCP packets have been collected from the network and parsed by the HTTP parser 
    console.log('Request [' + req.connection.remoteAddress + '] [' + req.headers.host + '] - Bytes: ' + req.bytes); 

    // log the bytes to a memory array, DB or disk (implementation not shown) 
    // [code here] 

    // add the request bytes to the counter 
    req.connection.bytesLogged = req.connection.bytesLogged + req.bytes; 

    // normal http server processing like return document or file 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('ok'); 
}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.1.1:1337/'); 

ログイン:

var http = require('http'); 
var server = http.createServer(function (req, res) { 

    // create some variables for data we want to log 
    // 
    // due to the way node.js works the remoteAddress and remotePort will not 
    // be available in the connection close event 
    // we also need to store the domain/host from the http header because the http 
    // request also won't exist when the connection close event runs 
    var remoteAddress = req.connection.remoteAddress; 
    var remotePort = req.connection.remotePort; 
    var host = req.headers.host; 
    var connection = req.connection; 

    // output bytes read by socket on each request 
    console.log('HTTP Request [' + remoteAddress + ':' + remotePort + '] [' + host + '] - connection Bytes Read: ' + connection.bytesRead); 

    // setup handle for connection close event 
    // 
    // to avoid the handle being added multiple times we add the handle onto 
    // the connection object which will persist between http requests 
    // 
    // we store the handler so we can check whether it has already been added 
    // to the connection listeners array - a less robust alternative would be to 
    // add a flag like connection.closeHandleAdded = true 
    connection.handle = connection.handle || {}; 
    if (!connection.handle.onconnectionClose) { 
    connection.handle.onconnectionClose = function() { 
     onconnectionClose(remoteAddress, remotePort, host, connection.bytesRead); 
    } 
    } 

    // check whether the close handle has already been added to the connection 
    // if not add it 
    if(connection.listeners('close').indexOf(connection.handle.onconnectionClose) == -1) 
    { 
    // attach handler to connection close event 
    connection.on('close', connection.handle.onconnectionClose); 
    // set connection idle timeout to 5 secs for testing purposes (default is 2min) 
    connection._idleTimeout = 5000; 
    } 

    // process http request as required 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('ok'); 

}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.1.1:1337/'); 

function onconnectionClose (remoteAddress, remotePort, host, bytesRead) { 
    console.log('connection Closed [' + remoteAddress + ':' + remotePort + '] [' + host + '] - connection Bytes Read: ' + bytesRead); 
} 
0

HTTP応答/要求を測定するために、このモジュールを使用してみてください:https://github.com/hex7c0/transfer-rate

は、データが間に送信されますクライアントとサーバーをソケットで接続します。各HTTP応答/要求にはそれぞれ独自のソケットがあります。モジュールはソケットで送信バイトを取得します(応答はsocket.bytesWritten、要求はsocket.bytesRead)。データを送信する時間は、コマンド(process.hrtime(開始))によって計算されます。次に、結果を分割して実際の伝送レートを取得します。

関連する問題