ngxtop的使用

前几天看见有运维类的微信公众号在介绍这个东西,嗯,都是抄来抄去的,还是要靠自己读懂程序,硬伤啊。

日志的收集、分析、统计,这些对于运维来说都很重要的。

我们从一开始的GREP\AWK\SED命令,到大牛们写工具,直到现在,都是ELK或阿里云了。。

所以我现在介绍的就是一款工具,实时分析NGINX日志,可惜不能生成报告,这比GOACCESS差多了。

官网地址 https://github.com/lebinh/ngxtop

[root@longquan software]# git clone https://github.com/lebinh/ngxtop.git
[root@longquan software]# cd ngxtop/
[root@longquan ngxtop]# python setup.py install
Traceback (most recent call last):
  File "setup.py", line 1, in <module>
    from setuptools import setup
ImportError: No module named setuptools
yum install python-pip -y   //会自动安装setuptools
[root@longquan ngxtop]# python setup.py install
安装成功倒数一行是Finished processing dependencies for ngxtop==0.0.2
安装完成后,就可以使用ngxtop --help查看怎么使用了
这里参考一下 http://www.ttlsa.com/nginx/nginx-modules-ngxtop-ttlsa
ngxtop使用详解
# ngxtop --help
ngxtop - ad-hoc query for nginx access log.
 
Usage:
    ngxtop [options]
    ngxtop [options] (print|top|avg|sum) <var> ...
    ngxtop info
    ngxtop [options] query <query> ...
 
Options:
    -l <file>, --access-log <file>   指定日志文件的完整路径
    -f <format>, --log-format <format>  log_format指令指定的日志格式 [默认: combined]
    --no-follow  ngxtop default behavior is to ignore current lines in log
                     and only watch for new lines as they are written to the access log.
                     Use this flag to tell ngxtop to process the current content of the access log instead.
    -t <seconds>, --interval <seconds>  report interval when running in follow mode [default: 2.0]  更新频率
 
    -g <var>, --group-by <var>  根据变量分组 [默认: request_path]
    -w <var>, --having <expr>  having clause [default: 1]
    -o <var>, --order-by <var>  排序规则(默认是访问计数) [默认: count]
    -n <number>, --limit <number>  显示的条数 [default: 10]
    -a <exp> ..., --a <exp> ...  add exp (must be aggregation exp: sum, avg, min, max, etc.) into output  添加表达式(一般是聚合表达式如: sum, avg, min, max 等)到输出中。
 
    -v, --verbose  更多的输出   //这里我发现了使用了SQLITE数据库
    -d, --debug  print every line and parsed record
    -h, --help  当前帮助信息.
    --version  输出版本信息.
 
    高级选项:
    -c <file>, --config <file>  运行ngxtop解析nginx配置文件
    -i <filter-expression>, --filter <filter-expression>  filter in, records satisfied given expression are processed.  只处理符合规则的记录
    -p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.
 
范例:
    All examples read nginx config file for access log location and format.
    If you want to specify the access log file and / or log format, use the -f and -a options.
 
    "top" like view of nginx requests
    $ ngxtop
 
    404前十的请求
    $ ngxtop top request_path --filter 'status == 404'
 
    总流量前十的请求
    $ ngxtop --order-by 'avg(bytes_sent) * count'
 
    访问量前十的ip地址
    $ ngxtop --group-by remote_addr
 
    输出400以上状态吗的请求以及请求来源
    $ ngxtop -i 'status >= 400' print request status http_referer
 
    Average body bytes sent of 200 responses of requested path begin with 'foo':
    $ ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")'
 
    使用common日志格式分析远程服务器Apache访问日志  //我想远程查看NGINX日志,不过出错了,IOError: [Errno 4] Interrupted system call
    $ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common

[root@longquan vhost]# ngxtop
Error: Access log file is not provided and ngxtop cannot detect it from your config file (/usr/local/nginx/conf/nginx.conf).
提示没有日志,因为我们一般都 include vhost/*.conf;是吧?
OK,没事,那我们把虚拟主机搬过来呗。但是我有很多虚拟主机怎么办,看起来不太科学。仔细看帮助,其实这个软件也跟我们一个高级选项呢!
那好,我们还是按照include的方式加载虚拟主机。
cat /usr/local/nginx/conf/vhost/qiye.glq.com.conf 
       log_format  access2 '$remote_addr - $remote_user [$time_local] $host '
                                   '"$request" $status $body_bytes_sent "$request_body" $request_time '
                                   '"$http_referer" "$http_user_agent" $http_x_forwarded_for '
                                   '$upstream_addr $upstream_response_time';
server {
    listen 80;
    server_name qiye.glq.com;
    ......
    access_log  /data/wwwlogs/qiye.glq.com.log access2;
这里主要有2点要注意!
1、log_format写到虚拟主机配置文件里来,当然拉,你不能写到不SERVER里面啊。这里我们取名为access2
2、日志写绝对路径,不写?报错你就会写了。

然后执行命令
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf 
我的虚拟主机只有一个日志文件,所以不会出现 Multiple access logs detected in configuration:
就需要指定ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf 就能实时显示了。
根据帮助写几个常用命令。
每10秒统计一次
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf  -t 10  
默认就显示10行,我们显示多点
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf  -n 20
处理当前已经写入的日志文件,而不是实时处理新添加到日志文件的日志     
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf   --no-follow
你可以自定义显示的变量,简单列出需要显示的变量。使用 "print" 命令显示自定义请求,就是你日志格式里面的变量啦。
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf  print request http_user_agent remote_addr
显示请求最多的客户端IP地址
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf top remote_addr
显示状态码是404的请求
ngxtop -c /usr/local/nginx/conf/vhost/qiye.glq.com.conf -i 'status == 404' print request status

其他还是靠自己去根据帮助,实际应用了。

转载于:https://my.oschina.net/longquan/blog/741107

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值