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

参考:经过ngxtop实时监控webserver的访问状况html


1、部署python

  1. 准备:python2.7.3+、SQLite(不然会报错)linux

  2. 编译安装SQLitenginx

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

    1. cd /usr/local/src &&wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz  #下载安装包,最新包请参考
    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. 安装ngxtopgithub

    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

2、使用web

  1. 帮助helpsql

    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. 示例:express

    1. ngxtop -c /usr/local/nginx/conf/nginx.conf -l /usr/local/nginx/logs/access.log top remote_addr request #默认2秒统计一次,默认显示前十的访问IP地址和请求 -l 指定日志文件路径
    2. ngxtop -c /usr/local/nginx/conf/nginx.conf -t 10 -l /usr/local/nginx/logs/access.log top remote_addr request #-t 10 指定10秒钟统计一次
    3. ngxtop -c /usr/local/nginx/conf/nginx.conf  -l /usr/local/nginx/logs/access.log -i 'status == 400' print request status #-i 指定过滤条件
    4. ngxtop -c /usr/local/nginx/conf/nginx.conf  -l /usr/local/nginx/logs/access.log top remote_addr request -n 20 #-n 20 指定显示20行
    5. 使用普通格式从远程服务器解析apache日志
      
      ssh user@remote_server tail -f /var/log/apache2/access.log | ngxtop -f common

  3. ngxtop单条命令没法执行:apache

报错意思是说ngxin执行文件要加到PATH路径中,加软连接
ln -s /usr/local/nginx/sbin/nginx /sbin
#修改环境变量
# vim /etc/profile
    export PATH=$PATH:/usr/local/nginx/sbin

source /etc/profile

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

相关文章
相关标签/搜索