利用ngxtop工具对nginx日志实时分析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用ngxtop工具对nginx日志实时分析相关的知识,希望对你有一定的参考价值。

参考:通过ngxtop实时监控webserver的访问情况


一、部署

  1. 准备:python2.7.3+、SQLite(否则会报错)

  2. 编译安装SQLite

    1. cd /usr/local/src && wget    #下载安装包,最新安装包请参考
    2. tar xf sqlite-autoconf-3230100.tar.gz && cd sqlite-autoconf-3230100  #解压安装包
    3. ./configure --prefix=/usr/local/sqlite && make && make install  #自定义路径编译安装
  3. 重新编译安装python

    1. cd /usr/local/src &&wget   #下载安装包,最新包请参考
    2. tar xf Python-2.7.14.tgz && cd Python-2.7.14  #解压安装
    3. 配置setup.py中sqlite路径
      
      qlite_inc_paths = [ '/usr/include',
      		    '/usr/include/sqlite',
      		    '/usr/include/sqlite3',
      			'/usr/local/include',
      			'/usr/local/include/sqlite',
      			'/usr/local/include/sqlite3',
      			'/usr/local/sqlite/include',
      		    ]
    4. ./configure --prefix=/usr/local/python2.7 && make && make install #自定义路径编译安装


  4. 安装ngxtop

    1. pip install ngxtop
    2. 源码编译安装
      wget https://github.com/lebinh/ngxtop/archive/1c200d51fbae7824a30159714669146d6b214210.zip
      unzip ngxtop-1c200d51fbae7824a30159714669146d6b214210.zip
      cd ngxtop-1c200d51fbae7824a30159714669146d6b214210
      python setup.py install

二、使用

  1. 帮助help

    1. # 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
      
      -v, --verbose  更多的输出
      
      -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
  2. 示例:

    1. ngxtop -c ./conf/nginx.conf  -l ./logs/access.log top remote_addr request

    

        参考:https://www.linuxprobe.com/real-analyse-nginx-ngxtop.html 


以上是关于利用ngxtop工具对nginx日志实时分析的主要内容,如果未能解决你的问题,请参考以下文章

分析日志的工具: 日志易

巧用字典树算法,轻松实现日志实时聚类分析

实时海量日志分析系统的架构设计实现以及思考

利用 go/ast 语法树做代码生成

bandit工具分析

如何使用 AST 树或其他工具进行静态代码逻辑分析?