博客
关于我
nginx实时请求监测-ngxtop
阅读量:796 次
发布时间:2023-03-22

本文共 1856 字,大约阅读时间需要 6 分钟。

ngxtop 实时命令行工具指南

ngxtop 是一个实时命令行工具,专为分析 nginx 访问日志设计,能够将 http 请求的主要指标以表格形式直观展示。它支持按字段过滤请求,能够实时动态显示 nginx 访问请求数据。

安装指南

CentOS 环境安装

#安装python
yum install epel-release
yum install python-pip
#安装ngxtop
pip install ngxtop

Ubuntu 环境安装(以 Ubuntu 24.04.2 LTS 为例)

# 更新系统
apt upgrade -y
# 安装python
apt install python3
# 安装 pip
sudo apt install pip
# 配置 pipx
pipx ensure
# 安装 ngxtop
pathsource ~/.bashrc
pipx install ngxtop

参数说明

ngxtop 提供了丰富的命令行参数,能够根据需求灵活配置。以下是常用参数说明:

  • -c:指定 nginx 配置文件,解析 log_format 和 access_log 路径。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf
  • -i:添加过滤条件。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'
  • -p:在读取日志前应用过滤条件,不匹配的行不会读取。与 -i 的区别在于 -p 会在读取日志前过滤。

  • -l:指定 access-log 文件路径。例如:

    ngxtop -l /usr/local/nginx/logs/access_log.log
  • -f:指定 nginx 日志格式,与 log_format 指令定义的格式一致。

  • --no-follow:强制解析 access-log 的当前内容,默认情况下 ngxtop 只会解析最新的访问日志。

  • -t:实时监控模式下的数据刷新时间间隔,默认为 2 秒。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1
  • -g:按指定字段分组,默认按 request_path 分组。例如按客户端 IP 分组:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr
  • -w:对分组结果进行筛选,类似 SQL 的 having 效果。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -w 'avg_bytes_sent > 30' -t 1
  • -o:指定排序字段。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1
  • -n:设置显示的记录条数,默认为 10 条。例如:

    ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5
  • -a:在分析结果中添加聚合字段,如 sum、avg、min、max 等。

  • -v:启用详细输出。

  • -d:打印每一行及解析记录。

  • -h:打印帮助信息。

  • --version:打印版本信息。

测试示例

指定配置文件

ngxtop -c /usr/local/nginx/conf/nginx.conf

加过滤条件

ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'

解析当前 access_log

ngxtop -c /usr/local/nginx/conf/nginx.conf --no-follow

指定分组条件

ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr

设置刷新时间为 1 秒

ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1

排序

ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1

显示前 5 个结果

ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5

转载地址:http://sxqfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现打印九九乘法表(附完整源码)
查看>>
Objective-C实现打印从 0 到 n 的卡特兰数算法(附完整源码)
查看>>
Objective-C实现打印函数调用堆栈( 附完整源码)
查看>>
Objective-C实现打印月份的日历算法(附完整源码)
查看>>
Objective-C实现打印杨辉三角(附完整源码)
查看>>
Objective-C实现打印某年的历法日期(附完整源码)
查看>>
Objective-C实现打印魔方矩阵(附完整源码)
查看>>
Objective-C实现打格点算法(附完整源码)
查看>>
Objective-C实现批量修改文件类型算法(附完整源码)
查看>>
Objective-C实现找出一个数的质因数primeFactors算法(附完整源码)
查看>>
Objective-C实现找出三角形从上到下的最大路径算法(附完整源码)
查看>>
Objective-C实现找出买卖股票的最大利润算法(附完整源码)
查看>>
Objective-C实现找出二维数组中的鞍点(附完整源码)
查看>>
Objective-C实现找出由两个 3 位数字的乘积构成的最大回文数的算法 (附完整源码)
查看>>
Objective-C实现找到一个数字数组的中值算法(附完整源码)
查看>>
Objective-C实现找到具有 500 个除数的第一个三角形数算法(附完整源码)
查看>>
Objective-C实现找到最近的点对之间的距离算法(附完整源码)
查看>>
Objective-C实现抓包实例(附完整源码)
查看>>
Objective-C实现抽签抓阄(附完整源码)
查看>>
Objective-C实现抽象工厂模式(附完整源码)
查看>>