12
7

More than 5 years have passed since last update.

nginxのアクセスログ解析にalpとngxtop

Posted at

ISUCON用にnginxのアクセスログ解析して各ページごとの応答速度やサイズ、ステータスコード等いくつかの観点から集計ができる簡単に使えそうなツールとしてalpとngxtopがよさそうなのでインストール方法と簡単な使い方をまとめた。

環境: CentOS7

alp

alp導入(CentOS7)

  • alpコマンドインストール
# curl -L -O https://github.com/tkuchiki/alp/releases/download/v0.3.1/alp_linux_amd64.zip
# yum install -y unzip
# unzip alp_linux_amd64.zip
# mv alp /usr/bin/alp
  • nginx log formatの指定
http{

    ~~~

    ##  /etc/nginx/nginx.confのlog_formatとaccess_logの行を以下のように変更
    ##  

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #
    #access_log  /var/log/nginx/access.log  main;

    log_format ltsv "time:$time_local"
                "\thost:$remote_addr"
                "\tforwardedfor:$http_x_forwarded_for"
                "\treq:$request"
                "\tstatus:$status"
                "\tmethod:$request_method"
                "\turi:$request_uri"
                "\tsize:$body_bytes_sent"
                "\treferer:$http_referer"
                "\tua:$http_user_agent"
                "\treqtime:$request_time"
                "\tcache:$upstream_http_x_cache"
                "\truntime:$upstream_http_x_runtime"
                "\tapptime:$upstream_response_time"
                "\tvhost:$host";

    access_log  /var/log/nginx/access.log  ltsv;

一度ログを空にしてから設定変更反映

# echo -n "" > /var/log/nginx/access.log
# systemctl restart nginx

使い方

## 出力はpathごとの統計情報
## COUNT: アクセス回数
## MIN、MAX、SUM、AVG: 
# alp -f /var/log/nginx/access.log

+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+
| COUNT |  MIN  |  MAX  |  SUM  |  AVG  |  P1   |  P50  |  P99  | STDDEV | MIN(BODY) | MAX(BODY) | SUM(BODY) | A
VG(BODY) | METHOD | URI |
+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+
|     1 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |  0.000 |   612.000 |   612.000 |   612.000 |  
 612.000 | GET    | /   |
+-------+-------+-------+-------+-------+-------+-------+-------+--------+-----------+-----------+-----------+--
---------+--------+-----+



## アクセス回数順にソート(-rで降順)
#alp -f /var/log/nginx/access.log --cnt -r

ngxtop

インストール

# yum install -y epel-release
# yum install -y python-pip
# pip install ngxtop

ngxtopはnginx.confのlog_formatを見てよしなにログをパースしてくれるみたいなので
alpのlog_formatをそのまま使っている。

使い方

## topコマンドのように以下のコマンド入力後のnginxのアクセスを解析して
# 以下のようにpathごとに内容を表示する
# nginxtop

running for 380 seconds, 56 records processed: 0.15 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|      56 |          393.321 |     8 |    18 |    30 |     0 |

Detailed:
| request_path   |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|----------------+---------+------------------+-------+-------+-------+-------|
| /              |      26 |          188.308 |     8 |    18 |     0 |     0 |
| /test          |      22 |          571.000 |     0 |     0 |    22 |     0 |
| /favicon.ico   |       8 |          571.000 |     0 |     0 |     8 |     0 |


## ログファイルを解析して出力する場合
# ngxtop --no-follow

## 出力されるpathの上限を変更する(デフォルト 10)

# ngxtop -n 100 --no-follow
12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7