0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

nginxログ集計

Posted at

概要

nginxのaccess.logやerror.logから条件に応じて集計するコマンド一覧。
(サンプルとして記載しているログのIPアドレスやドメインは適当です。)

access.log

対象の形式

0.136 - - 51.69.40.73 - - [29/Jan/2025:16:35:20 +0900] "GET /wp/wp-login.php HTTP/1.1" 200 3240 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36" "52.68.40.71"

ログファイルの行数

wc -l access.log

圧縮ファイルに対して行う場合

zcat access.log-20250221.gz | wc -l

時間ごとにアクセス数を集計

60分ごとに集計する場合

cat access.log | awk -F'[][]' '{print $2}' | awk -F: '{printf "%s:%s:%02d\n", $1, $2, int($3/60)*60}' | sort | uniq -c

対象時間帯を10分ごとに集計する場合

cat access.log | awk -F'[][]' '{print $2}' | awk -F: '{printf "%s:%s:%02d\n", $1, $2, int($3/10)*10}' | sort | uniq -c

リクエストURLを集計

特定の特定の時間帯のトップ10を集計する場合

cat access.log | awk -F'"'  '{print $2}' | sort | uniq -c | sort -rn | head -n 10

リクエスト元ユーザーエージェント

cat access.log | awk -F'"'  '{print $6}' | sort | uniq -c | sort -rn | head -n 10

リクエスト元IP

cat access.log | awk -F'"'  '{print $8}' | sort | uniq -c | sort -rn | head -n 10

error.log

2025/01/29 17:48:01 [error] 1026355#1026355: *755254 FastCGI sent in stderr: "; PHP message: PHP Warning:  Undefined variable $product_sub_text in /path/to/dir/test.php on line 26" while reading upstream, client: 66.249.64.103, server: aaa.test.jp, request: "GET /snap/1234/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "aaa.test.jp"

PHP Warningを集計

通常ファイルに対して行う場合

grep -oP 'PHP message:.*?(;|PHP message|upstream)' error.log | sort | uniq -c | sort -nr

圧縮ファイルに対して行う場合

zgrep -oP 'PHP message:.*?(;|PHP message|upstream)' error.log-20250110.gz | sort | uniq -c | sort -nr

PHP Fatal Errorを集計

通常ファイルに対して行う場合

grep -oP 'PHP Fatal error:(.*)' error.log | sort | uniq -c | sort -nr

圧縮ファイルに対して行う場合

zgrep -oP 'PHP Fatal error:(.*)' error.log-20250110.gz | sort | uniq -c | sort -nr
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?