当前位置:首页 > 技术分享

Nginx日志实时分析利器(Ngxtop)分析请求次数及带宽占用情况

admin2年前 (2023-12-12)技术分享3055

Ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop。有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail日志看屏幕刷新。


一、安装ngxtop

由于ngxtop是python编写,我们采用pip安装,可能有些用户没有安装pip,下面我们一块加上pip的安装步骤:


pip安装

Fedora:yum -y install python-pip

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

Debian/Ubuntu:apt-get install python-pip

安装完pip就可以安装ngxtop

//安装 ngxtop
sudo pip install ngxtop
//启动
sudo ngxtop
//如果启动报错 sudo: ngxtop: command not found

ngxtop使用参数


ngxtop 参数 print|top|avg|sum

ngxtop info 显示日志格式信息


-l <file>或--access-log <file> 设置日志路径

-f <format>或--log-format <format> 设置日志格式,默认格式combined,另外一种较常用格式为common

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

-t <seconds> 或 --interval <seconds> 刷新频率,默认2秒

-g <var>或 --group-by <var> 按变量分组,默认显示 request_path

-w <var>或 --having <expr> 筛选 [default: 1]

-o <var>或 --order-by <var> 输出的排序方式,默认: 访问数

-n <number>或 --limit <number> 显示top多条,默认前top 10条

-a <exp> ...或 --a <exp> ... 对输出字段做处理,可选 sum, avg, min, max

-v或 --verbose 详细输出

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

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

--version 显示版本信息


高级参数


-c <file>或 --config <file> 指定nginx配置文件,自动分析日志格式

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

-p <filter-expression>或 --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.

另外一些变量可以在分析时用到,名字含义同日志格式里的设置:remote_addr、remote_user、time_local、request、request_path、status、body_bytes_sent、http_referer、http_user_agent。


二、Ngxtop使用实例

实时监控日志

ngxtop -l /usr/local/nginx/logs/access.log

日志分析

ngxtop -l /usr/local/nginx/logs/access.log --no-follow

按rquest_path且是404的前10请求:

ngxtop -l /usr/local/nginx/logs/access.log --no-follow top request_path --filter 'status == 404'

按总bytes sent最高的前10:

ngxtop -l /usr/local/nginx/logs/access.log --no-follow --order-by 'avg(bytes_sent) * count'

按remote address进行排序前10:

ngxtop -l /usr/local/nginx/logs/access.log --no-follow --group-by remote_addr

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

ngxtop -l /usr/local/nginx/logs/access.log --no-follow -i 'status >= 400' print request status http_referer

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

ngxtop -l /usr/local/nginx/logs/access.log --no-follow avg bytes_sent --filter 'status == 200 and request_path.startswith("www")'

关于 ngxtop 的更多用法,可通过 ngxtop -h 查询。

扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/569.html

分享给朋友:

“Nginx日志实时分析利器(Ngxtop)分析请求次数及带宽占用情况” 的相关文章

mysql update不支持set子查询更新 的解决办法

mysql update不支持set子查询更新 的解决办法

先看示例:SELECT uin,account,password,create_user_uin_tree FROM sys_user结果:表中的create_user_uin_tree标识该条记录由谁创建。创…

php Aes 加密模式ECB填充pkcs5padding base64

最近做支付项目用到了aes加密不过试了好多办法总是和官方给出的结果不一样,找了很久终于找到了测试结果同 http://tool.chacuo.net/cryptaes/ <?php /**  * [Aes&nb…

PHP和Redis实现在高并发下的抢购及秒杀功能

抢购、秒杀是平常很常见的场景,面试的时候面试官也经常会问到,比如问你淘宝中的抢购秒杀是怎么实现的等等。抢购、秒杀实现很简单,但是有些问题需要解决,主要针对两个问题:一、高并发对数据库产生的压力二、竞争状态下如何解决库存的正确减少("…

在CentOS 5.x 6.x里使用yum源 换阿里云国内源换vault.centos.org源

阿里云CentOS 5 的系统,无法用yum来安装应用软件。  原因:CentOS 5 在2017-03-31日已经结束支持,不再提供维护更新,所以包括阿里云镜像站的文件可能都是过时或已经有部分文件缺失。 &n…

Chrome 最全历史版本下载以及chromedriver下载

https://vikyd.github.io/download-chromium-history-version/#/ https://dl.lancdn.com/landian/soft/chrome/m/ chromedriver的版…

阿里云盘 分享脚本 抢先实现文件分享功能

Win端基本所有浏览器均可进行安装安装地址:https://bbs.tampermonkey.net.cn/thread-427-1-1.html如何安装:Tampermoneky安装:谷歌浏览器参考:https://bbs.tamperm…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。