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

2024-06-21

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

有谁知道我没有遇到过执行此操作的 API 调用吗?

或者其他人在按带宽收费的多租户环境中使用的模块或其他方法?

Update:

有谁知道可以放在任何 Web 服务器(node.js、apache 等)前面的轻量级代理/服务器,它可以通过检查域来记录这些带宽统计信息?


在不修改 node.js 核心的情况下,最好的选择似乎是使用 bytesRead 和 bytesWritten 变量在套接字级别跟踪它。

这实际上比仅仅测量 http 请求/响应字符串的大小(如其他一些带宽跟踪器计算传输的数据)更准确。

您有 2 个选项:1) 记录每个请求的字节,或者 2) 记录 TCP 连接关闭时的字节。

记录请求级别将提供有用的实时数据。但是,这可能会减慢速度,具体取决于您实现日志记录的方式。最好将其保存在内存中并经常将其转储到磁盘/数据库。

在请求级别记录:

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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node.js 按域计算带宽使用情况 的相关文章

随机推荐

  • 如何动态地将节追加到 Symfony 2 配置中?

    my bundle algorithm blowfish One of md5 blowfish sha256 sha512 这个配置是通过这个配置树完成的 Algorithms and constants to check algorit
  • fgetc 无法识别 EOF [重复]

    这个问题在这里已经有答案了 下面的程序在各种 Solaris Linux 版本上运行良好 但在 AIX 上运行不佳 但是 如果我更换while c EOF with while c 0xff 在 AIX 上它运行得很好 有什么想法吗 我检查
  • mysql 版本号排序

    我有这样的价值观 1 1 2 9 1 2 2 4 1 2 3 4 3 2 14 3 2 1 4 2 我需要使用 mysql 对这些值进行排序 该数据类型是 varbinary 300 所需的输出将类似于 1 1 2 1 2 3 4 2 2
  • C++ 相当于 C# 中的 new Random(seed)

    当我们在 C 中使用随机数生成器时 我们可以定义一个变量 例如 private Random rndGenerator 在课堂上然后打电话 rndGenerator new Random seed 正确地在类的构造函数中 我的问题是 这种定
  • 嵌套文档上的 MongoDB $lookup

    我是 mongo 的新手 正在努力应对以下问题 我有 2 个集合 结构如下 对于我的一生 我不知道如何对学校收藏进行 lookup 阅读其他帖子 我肯定使用 ObjectId 作为参考以及外部字段 下面是我的结构 Alumni id joh
  • 在多行中打印带有列名称的 R 数据框

    我有一个带有长列名称的 R 数据框 所以当我打印数据框时它太宽了 有没有一种简单的方法可以将数据框打印到屏幕上 并且列名出现在多行中 我知道我可以缩短名字 但我不想这样做 当奥斯卡的答案被接受时 我想这可能真的是一个答案 不幸的是 这只是复
  • Sqlite 查询检查 - 小于和大于

    return mDb query DATABASE TABLE new String KEY ROWID KEY LEVEL KEY LEVEL gt 3 lt 5 null null null null 我究竟做错了什么 它返回的值全部高
  • 实时监控网站更新

    我很好奇如何实时监控网站的更新 最好是在 Node js 中 我们以维基百科为例 有人决定用一些相关信息更新一篇文章 我如何订阅该页面并立即在我的 Node js 服务器中获取事件 而不必每 X 秒轮询一次网站 一般来说 如果没有其他可能性
  • cakephp 3 中的 SUM 查询不起作用

    我正在尝试添加同一字段的数据并希望返回我使用以下查询的结果 total this gt Details gt find all array fields gt array sum Details total downtime Details
  • 选择移动 Web HTML5 框架 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • “客户端”对于 Hadoop/HDFS 究竟意味着什么?

    我理解其背后的一般概念 但我希望对 客户 是什么有更多的澄清和明确的定义 例如 如果我只是在终端上写一个 hdfs 命令 它仍然是 客户端 吗 ClientHadoop中是指用于与Hadoop文件系统通信的接口 Hadoop 可使用不同类型
  • 基于多个表的数据更新单个表 SQL Server 2005,2008

    我需要更新表one使用表中的数据two 表一和表二没有任何公共列相关 桌子three与表相关two 例如 表一 reg det 表 reg det id reg id results 101 11 344 表二 临时表 venue resu
  • 如何在没有JDK的情况下运行jcmd?

    我正在尝试弄清楚如何将 jcmd exe 删除到安装在客户端站点的 Windows 服务器上 以便我们可以解决堆和线程问题 不太想安装完整的 JDK 因为它会使环境变得复杂 jcmd exe 肯定需要运行 JDK 中的某些组件 但我无法确定
  • 任务“:app:checkReleaseDuplicateClasses”执行失败

    我的 React Native Android 构建中突然出现构建问题 令人惊讶的是 它是早上建好的 没有做任何改变 但突然就失败了 这就是我得到的错误 知道为什么会发生这种情况吗 在 stack 和 GitHub 中也看到了一些类似的问题
  • [A-Z] 表示 [A-Za-z] 是怎么回事?

    我已经注意到 至少在我使用的一些基于 Unix 的系统上 ls A Z 已经给了我预期的结果ls A Za z 让我无法轻松获得以大写字母开头的该死的文件列表 我刚刚遇到了同样的事情grep 我无法让它停止与小写字母匹配 A Z 直到我最终
  • 找不到 hadoop 安装:必须设置 $HADOOP_HOME 或 hadoop 必须位于路径中

    所以有一点背景 我一直在尝试在 CentOS 6 机器上设置 Hive 我按照 YouTube 视频的说明进行操作 http www youtube com watch v L2lSrHsRpOI http www youtube com
  • 使用Retrofit来消费服务器发送的事件

    我正在尝试使用rest api 1 https mesosphere github io marathon docs rest api html get v2 events将服务器发送的事件发送到客户端 我目前正在使用 square 的改造
  • Mvc脚手架一对多关系

    我正在使用 MVC 4 EF 4 3 和 MVCScaffolding 包 我有以下简单的模型类 public class Product Key public int ID get set Required public string N
  • 如何在目录中查找或获取文件名 Visual Basic.net 中具有特定单词的文件?

    我需要从名称中包含特定字符的目录中获取文件 下面的代码将返回带有以下内容的任何文件 csv扩大 问题是还有其他 csv 文件我需要保留或得不到 Dim FileLocation As DirectoryInfo New DirectoryI
  • Node.js 按域计算带宽使用情况

    如何使用node js作为Web服务器来监控每个域的带宽使用情况 有谁知道我没有遇到过执行此操作的 API 调用吗 或者其他人在按带宽收费的多租户环境中使用的模块或其他方法 Update 有谁知道可以放在任何 Web 服务器 node js