3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アクセスログから任意のランキングを作る汎用的なワンライナー

Posted at

サーバを運用していると、アクセスログからサクッと件数を調べたくなることが多々ある。

時系列でのアクセス件数、ユーザーエージェント毎、リクエストURLなど様々だが、ワンライナーの型はほとんど同じ。

perl -ne 'next unless(/★単純にgrepするキーワード★/);/★集計単位★/;$counts{$1}++;END{for(sort keys %counts){print "$_ $counts{$_}\n"}}' access_log

Apacheのアクセスログ例を元に、具体的な使い方を紹介する。

Googlebotのアクセス件数

単純にgrepするキーワード:Googlebot
集計単位:時間

$ perl -ne 'next unless(/Googlebot/);/2017:(\d\d)/;$counts{$1}++;END{for(sort keys %counts){print "$_ $counts{$_}\n"}}' access_log

03 1
04 11
05 17
06 36
07 20
08 36
09 30
10 33
11 54
12 45
13 62
14 23
15 32
16 38
17 24
18 15

Safariでのアクセス件数

単純にgrepするキーワード:Safari
集計単位:時間

$ perl -ne 'next unless(/Safari/);/2017:(\d\d)/;$counts{$1}++;END{for(sort keys %counts){print "$_ $counts{$_}\n"}}' access_log
03 89
04 1982
05 6149
06 15110
07 18474
08 17452
09 14082
10 11929
11 11365
12 12830
13 9830
14 16637
15 19439
16 19432
17 25928
18 22898

GoogleBotの接続元IP毎のアクセス件数

単純にgrepするキーワード:Googlebot
集計単位:接続元IP

perl -ne 'next unless(/Googlebot/);/^(\S+)/;$counts{$1}++;END{for(sort keys %counts){print "$_ $counts{$_}\n"}}' access_log
66.249.66.205 1
66.249.71.143 1
66.249.71.145 2
66.249.71.147 2
66.249.71.73 182
66.249.71.75 106
66.249.71.76 1
66.249.71.77 56
66.249.71.77, 1
66.249.71.78 1
66.249.79.35 48
66.249.79.39 25
66.249.79.61 51

叩かれたAPIのアクセス件数

単純にgrepするキーワード:なし
集計単位:API(/js/v2/*)

$ perl -ne 'next unless(//);/(\/js\/v2\/\w+)/;$counts{$1}++;END{for(sort keys %counts){print "$_ $counts{$_}\n"}}' access_log
 95215
/js/v2/com 19069
/js/v2/fb 63491
/js/v2/pc 27632
/js/v2/old 26
/js/v2/smartphone 9392

解説

END{集計処理}

抽出するキーでソート、件数をカウントして表示

/★集計単位★/;$counts{$1}++;

集計単位で抽出された部分がここに入る。

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?