node.js按域计算带宽使用情况

如何使用node.js作为Web服务器监视每个域的带宽使用情况?

有没有人知道一个API调用我没有遇到这样做?

还是其他人在多租户环境中使用的模块或其他方法,您正在通过带宽进行计费?

更新:

有谁知道一个轻量级代理/服务器可以放在任何Web服务器(node.js,Apache等),可以通过检查域logging这些带宽统计前?

       

网上收集的解决方案 "node.js按域计算带宽使用情况"

在不修改node.js内核的情况下,最好的select似乎是使用bytesRead和bytesWrittenvariables在套接字级别进行跟踪。

这实际上比仅仅测量http请求/响应string的大小更准确,因为一些其他带宽跟踪器用于计算传输的数据。

您有2个选项:1)在每个请求logging字节或2)loggingTCP连接closures的字节。

login请求级别将提供可能有用的实时数据。 但是,这取决于您如何实现日志logging,可能会减慢速度。 最好将它保存在内存中,并且每隔一段时间将其转储到磁盘/数据库。

login请求级别:

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/'); 

login套接字级别:

 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 。

它是一个node.js模块,用于捕获,解码和分析HTTP和WebSocketstream量。 它可以find每个请求的大小和域名,所以通过一些调整,你应该能够做到你想在这里完成。

它在我的Ubuntu服务器上使用节点v0.6.15运行良好,我所要做的就是“apt-get install libpcap0.8-dev”。

尝试使用此模块来衡量HTTP响应/请求: https : //github.com/hex7c0/transfer-rate

数据通过套接字在客户端和服务器之间发送。 每个HTTP响应/请求都有自己的套接字。 模块在套接字中发送字节( socket.bytesWritten用于响应,而socket.bytesRead用于请求)。 发送数据的时间由命令( process.hrtime(start) )计算。 然后,我们划分的结果来获得实际的传输速率。