LoginSignup
33
33

More than 5 years have passed since last update.

apacheのログファイルをltsv形式でより便利に!!

Posted at

はじめltsvってapacheのモジュールかと思ってましたら・・そうじゃなくって、単純にLogFormatをそのような形式にするってだけなんですね。^^; なので形式とか表示順とかは気にしなくて良いのです。
やってみてわかりましたが、ltsvってとっても便利!特に、td-agentの設定が楽になって更にうれしい感じです!今回は、いかに、ltsvが扱いやすくって、それを使ったTreasure Dataもいかに柔軟に活用できるかなんてのを説明したいと思います。(利点を充分に伝えきれない自信あり!)

ltsvを利用するにあたってapache httpd.confに以下の通り設定されているか確認してください。yumでインストールとかした場合は、基本的に入っているので、あまり気にする必要はないかもしれません

/etc/httpd/conf/httpd.conf
LoadModule log_config_module modules/mod_log_config.so

log_config_moduleのカスタム変数については、こちらを参照してください。

・・というわけでこんなltsvログフォーマットを作成してみました。ltvsのフォーマットは、

[ラベル名]:[変数名]

という形式で書きます。
そしてTABは、\t になります。ログフォーマット(テンプレート名)名は、"apache_ltsv"としました。

以下のように作成してみました。

httpd.conf
LogFormat "time:%{%d/%b/%Y:%H:%M:%S %z}t\tx_forwarded_for:%{X-Forwarded-For}i\tremote_host:%h\trequest:%r\tlast_status:%>s\tsize:%b\treferer:%{Referer}i\tuser_agent:%{User-Agent}i\tv_host:%{Host}i" apache_ltsv

改行を入れてみました。実際の記述は改行無しで入れてください

LogFormat
"time:%{%d/%b/%Y:%H:%M:%S %z}t\t
x_forwarded_for:%{X-Forwarded-For}i\t
remote_host:%h\t
request:%r\t
last_status:%>s\t
size:%b\t
referer:%{Referer}i\t
user_agent:%{User-Agent}i\t
v_host:%{Host}i" apache_ltsv
  • 併せて、logの出力設定に以下の項目を追記します。二行目の、CustomLog logs/access_log_ltsv apache_ltsv が今回新規に追加した分になります。
httpd.conf
#
# For a single logfile with access, agent, and referer information
# (Combined Logfile Format), use the following directive:
#
CustomLog logs/access_log combined
CustomLog logs/access_log_ltsv apache_ltsv

apache再起動

# /etc/init.d/httpd restart

ログの確認

# head /var/log/httpd/access_log_ltsv
time:[22/Jul/2013:16:40:33 +0900] x_forwarded_for:- remote_host:66.249.XXX.YYY request:GET /wordpress/?p=1548 HTTP/1.1 last_status:200 size:42160 referer:- user_agent:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html) v_host:www.s-quad.com
time:[22/Jul/2013:16:40:36 +0900] x_forwarded_for:- remote_host:202.XXX.XXX.YYY request:GET /wordpress/wp-content/uploads/2011/07/2011-07-01_213004.jpg HTTP/1.0 last_status:200 size:87346 referer:http://www.s-quad.com/wordpress/?p=987 user_agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E) v_host:www.s-quad.com
time:[22/Jul/2013:16:40:36 +0900] x_forwarded_for:- remote_host:202.XXX.XXX.YYY request:GET /favicon.ico HTTP/1.0 last_status:404 size:289 referer:- user_agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E) v_host:www.s-quad.com

tab区切りになっていて、且つ、設定したラベルが表示されているのが確認できるかと思います

何が便利なの?

  • なんの項目だかわかりやすい!
  • タブ区切りの為、ワンライナーを作りやすい!!

例えば、accessログのうちもっとも、アクセス回数の多いリモートホストを降順に表示する、なんていった場合は、こんなワンライナーな感じで書きます


# cut -f3 /var/log/httpd/access_log_ltsv | sort | uniq -c | sort -r
35 remote_host:163.XXX.17.43
20 remote_host:202.XXX.2.102
16 remote_host:XXX.19.58.156
16 remote_host:XXX.179.35.3
15 remote_host:XXX.101.164.209
15 remote_host:XXX.160.194.73
14 remote_host:XXX.118.185.164
14 remote_host:XXX.232.30.232
13 remote_host:XXX.16.89.141
8 remote_host:::1
8 remote_host:XXX.138.98.193
8 remote_host:202.XXX.245.194
8 remote_host:114.180.XXX.245

もちろん、一般的なapacheのaccess_logもできないわけではないですが、正規表現とか考えると、ちょっとめんどくさいですよね。私自身覚えるのが面倒な人なんであんまし書きたくはないです。

あ、一点注意点としては、アクセスログディレクトリのパーミッションは必ず変更しておくこと!ですかね?^^

# chmod o+rx /var/log/httpd

td-agent

td-agent (fluentdでのフォーマット指定はこんな感じでできますよ!

/etc/td-agent/td-agent.conf
<source>
  type tail
  path /var/log/httpd/access_log_ltsv
  format ltsv
  time_key time
  time_format %d/%b/%Y:%H:%M:%S %z
  tag td.apache.access
  pos_file /var/log/td-agent/apache_access.pos
</source>

<match td.apache.access>
  type copy
  <store>
    type tdlog
    apikey c77c321a5510f0fbbb294222dad62732eec2dd0d8
    auto_create_table
    buffer_type file
    buffer_path /var/log/td-agent/buffer/td
</store>

<store>;
  type file
  path /var/log/td-agent/access_ltsv.log
</store>
</match>

hiveqlで、リモートホスト別アクセスランキング TOP 10を出力してみましょう!

先ほどのワンライナーで記述した方法を、hiveqlで実現してみます。実際の運用では、超ロングスパンで出力したデータを集計とかするのであれば、間違えなく、Treasure Dataを利用したほうが良いと思います。ってなわけで、簡単ではありますが、集計クエリーっぽいものを作成してみました。

  • hiveqlを利用して、リモートホスト別アクセスカウントをとる
# td query -w -d apache "SELECT v['remote_host'] as remote_addr,count(v['remote_host']) as cnt FROM access group by v['remote_host'] order by cnt desc limit 10"
MapReduce time taken: 54.736 seconds
Time taken: 54.901 seconds
Status : success
Result :
+-----------------+-----+
| remote_addr | cnt |
+-----------------+-----+
| XXX.XX.187.66 | 69 |
| 202.232.222.11 | 46 |
| 122.29.XXX.YY | 17 |
| 118.111.121.173 | 16 |
| 202.232.222.149 | 15 |
| 202.111.30.196 | 15 |
| 203.136.98.161 | 15 |
| 219.163.84.243 | 15 |
| 111.117.149.4 | 15 |
| 219.223.111.81 | 15 |
+-----------------+-----+
10 rows in set
33
33
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
33
33