node.js を Web サーバーとして使用して、各ドメインの帯域幅の使用状況を監視するにはどうすればよいですか?
これを行うために出くわしていない API 呼び出しを知っている人はいますか?
または、帯域幅で課金しているマルチテナント環境で他の人が使用したモジュールまたは別の方法はありますか?
アップデート:
ドメインを検査してこれらの帯域幅統計を記録できる任意の Web サーバー (node.js、apache など) の前に配置できる軽量のプロキシ/サーバーを知っている人はいますか?
node.js を Web サーバーとして使用して、各ドメインの帯域幅の使用状況を監視するにはどうすればよいですか?
これを行うために出くわしていない API 呼び出しを知っている人はいますか?
または、帯域幅で課金しているマルチテナント環境で他の人が使用したモジュールまたは別の方法はありますか?
アップデート:
ドメインを検査してこれらの帯域幅統計を記録できる任意の Web サーバー (node.js、apache など) の前に配置できる軽量のプロキシ/サーバーを知っている人はいますか?
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);
}
Http Traceを見てください。
HTTP および WebSocket トラフィックをキャプチャ、デコード、分析する node.js モジュールです。各リクエストのサイズとドメインを見つけることができるので、微調整を加えれば、ここで達成したいことを実行できるはずです。
ノードv0.6.15のUbuntuサーバーでうまく機能しています。「apt-get install libpcap0.8-dev」を実行するだけで済みました。
このモジュールを使用して、HTTP 応答/要求を測定してみてください: https://github.com/hex7c0/transfer-rate
データは、クライアントとサーバーの間でソケットによって送信されます。各 HTTP 応答/要求には、独自のソケットがあります。モジュールはソケットで送信されたバイトを取得します (応答の場合はsocket.bytesWritten 、要求の場合はsocket.bytesRead )。データを送信する時間は、コマンド ( process.hrtime(start) ) によって計算されます。次に、結果を除算して実際の送信レートを取得します。