日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

kataribeをnginxのアクセスログ解析に使ってみた備忘録

isuconの準備中にkataribeという単語を聞いて、さらに下記の資料を参考にしていたらkataribeが現れたのでちょっと触ってみようと思いました。

ISUCONの勝ち方 YAPC::Asia Tokyo 2015 | PPT

やったことはnginx, kataribeのインストールとnginxの設定変更です。
試した時はcentos6のdockerコンテナに入れていきましたが、今年のIsuconはubuntuらしいので、ubuntuに入れ直してみようと思います。
インストール方法は違えど、設定自体は同じになるかと思います。

1.諸々のインストール

ubuntuのdockerコンテナを手に入れた所からやっています。
versionは14.04です。

$docker pull ubuntu:14.04
$docker run -i -t ubuntu:14.04 /bin/bash

$cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"

#必要になるもののインストール
$apt-get install curl git

#nginxインストール
$apt-get install nginx
...

# GoLangインストール kataribeインストール用です
#apt-get updateしないとインストールできなかったのでしています
$apt-get update
$apt-get install golang
$mkdir -p ~/gocode && echo 'export GOPATH=$HOME/gocode' >> ~/.bashrc
$echo 'export PATH=$GOPATH/bin:$PATH' >> ~/.bashrc
$source ~/.bashrc
$go get github.com/matsuu/kataribe

2.諸々の設定

下記を見るとnginxのログフォーマットの設定にrequest_timeを追加して上げる必要があるようです。
/etc/nginx/nginx.confに追加してあげましょう。

github.com

# 略

+	log_format with_time '$remote_addr - $remote_user [$time_local] '
+			     '"$request" $status $body_bytes_sent '
+			     '"$http_referer" "$http_user_agent" $request_time';

+	access_log /var/log/nginx/access.log with_time;
-	access_log /var/log/nginx/access.log;

	error_log /var/log/nginx/error.log;

# 略

3. 動かしてみる

nginxを再起動して、curlで叩いてログを吐かせた後にkataribeを実行してみましょう。

#再起動
$service nginx restart
 * Restarting nginx nginx                                                                                                       [ OK ]

#curlでログを出力させる
$curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#ログ確認
$cat /var/log/nginx/access.log
::1 - - [22/Sep/2015:08:31:31 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" 0.000

#kataribe実行
$cat /var/log/nginx/access.log |kataribe -f $GOPATH/src/github.com/matsuu/kataribe/kataribe.toml
open kataribe.toml: no such file or directory
ls $GOPATH/src/github.com/matsuu/kataribe/kataribe.toml
/root/gocode/src/github.com/matsuu/kataribe/kataribe.toml

#上記のように-f をつけてもkataribe.tomlが認識してくれなかったのでカレントディレクトリにcpしてしまってます。
$cp $GOPATH/src/github.com/matsuu/kataribe/kataribe.toml ./
$cat /var/log/nginx/access.log |kataribe
Sort By Count
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request
    1  0.000  0.000000  0.000000  0.000  0.000  0.000  0.000  0.000  0.000    1    0    0    0  GET / HTTP/1.1

Sort By Total
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request
    1  0.000  0.000000  0.000000  0.000  0.000  0.000  0.000  0.000  0.000    1    0    0    0  GET / HTTP/1.1

Sort By Mean
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request
    1  0.000  0.000000  0.000000  0.000  0.000  0.000  0.000  0.000  0.000    1    0    0    0  GET / HTTP/1.1

Sort By Standard Deviation
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request
    1  0.000  0.000000  0.000000  0.000  0.000  0.000  0.000  0.000  0.000    1    0    0    0  GET / HTTP/1.1

Sort By Maximum(100 Percentile)
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request
    1  0.000  0.000000  0.000000  0.000  0.000  0.000  0.000  0.000  0.000    1    0    0    0  GET / HTTP/1.1

TOP 1 Slow Requests
1  0.000  GET / HTTP/1.1

まあなんにもやってないのでこんな感じですね。
もしnginxでlog_formatに追記してないと下記のようなエラーがでます。

Sort By Count
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request

Sort By Total
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request

Sort By Mean
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request

Sort By Standard Deviation
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request

Sort By Maximum(100 Percentile)
Count  Total      Mean    Stddev    Min    P50    P90    P95    P99    Max  2xx  3xx  4xx  5xx  Request

TOP 0 Slow Requests
panic: runtime error: index out of range

goroutine 1 [running]:
runtime.panic(0x515c20, 0x68a457)
	/usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6
main.showTop(0x0, 0x0, 0x0)
	/root/gocode/src/github.com/matsuu/kataribe/main.go:243 +0x425
main.main()
	/root/gocode/src/github.com/matsuu/kataribe/main.go:389 +0xe34

fのオプションはどうして動かないんだろう・・・(´・ω・`)
見に行ってみたところ この バージョンらしい。
Golang学んでソースコード読んでみようかなぁ
Isuconでは上手く使っていきたいなぁと思います。