ngxtop 实时分析 nginx 日志

本贴最后更新于 1334 天前,其中的信息可能已经水流花落

Ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top。ngxtop,可以实时了解到当前nginx的访问状况,在做性能测试过程中不需要tail日志看屏幕刷新。

一、安装ngxtop

ngxtop是python编写,采用pip安装,可能有些用户没有安装pip

输入如下信息,可以从输出中可以看到没有安装pip

[root@localhost ~]# pip install ngxtop

bash: pip: command not found...

[root@localhost ~]# python

bash: python: command not found...

下面加上pip的安装步骤:

pip安装

CentOS/RHEL需先安装EPEL:yum -y install epel-release &&yum -y install python-pip*

备注:这里以centos7为例可以安装成功

image.png

安装ngxpip,但是下面出现了错误

[root@docker ~]# pip install ngxtop

Collecting ngxtop

Downloading https://files.pythonhosted.org/packages/c7/20/9aafbe173b596dfbc996f1b8b957da88d160cf1c5279179d3b4990f40e00/ngxtop-0.0.2-py2.py3-none-any.whl

Collecting tabulate (from ngxtop)

Downloading https://files.pythonhosted.org/packages/57/6f/213d075ad03c84991d44e63b6516dd7d185091df5e1d02a660874f8f7e1e/tabulate-0.8.7.tar.gz (50kB)

100% |████████████████████████████████| 51kB 14kB/s
Collecting pyparsing (from ngxtop)

Could not find a version that satisfies the requirement pyparsing (from ngxtop) (from versions: )

No matching distribution found for pyparsing (from ngxtop)

You are using pip version 8.1.2, however version 20.2.2 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

升级pip

[root@docker ~]# pip install --upgrade pip

image.png

再一次安装

[root@docker ~]# pip install ngxtop

image.png

二、nginx常用参数

--no-follow 处理以前的日志,实时日志不做处理

-t : 刷新频率,默认2秒

-g : 按变量分组,默认显示 request_path

-w : 筛选 [default: 1]

-o : 输出的排序方式,默认: 访问数

-n :显示top多条,默认前top 10条

-a : 对输出字段做处理,可选 sum, avg, min, max

-v或 --verbose: 详细输出

-d或 --debug: debug模式,输出每行及记录

-h或 --help: 显示帮助详细

--version: 显示版本信息

高级参数

-c : 指定nginx配置文件,自动分析日志格式

-i <filter-expression: 满足表达式的过滤将被处理

三、ngxtop使用示例

  1. 实时监控日志

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log

image.png

  1. 对日志进行分析

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow

image.png

  1. 按request_path且是500的前10请求

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow top request_path --filter 'status==500'

image.png

  1. 按总avg_bytes_sent最高的前10:

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow --order-by 'avg(bytes_sent)*count'

image.png

  1. 按remote address进行排序前10:

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow --group-by remote_addr

image.png

  1. 显示500或更高返回状态码的且只显示request、status、http_referer这三列信息:

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow -i 'status>=500' print request status http_referer

image.png

  1. 显示bytes_sent平均值且状态码为200且request_path以www开始的前10:

[root@docker html]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow avg bytes_sent --filter 'status==200 and request_path.startswith("www")'

image.png

更多使用,可以直接使用ngxtop -h

回帖
请输入回帖内容 ...