前回は同一サーバ上のApacheログを直接読み込んでlogstashに貯め込む構成を作ったので、今回は次のステップとして、複数サーバのApacheログをlogstashで収集する構成を作ってみる。
…といっても、まずはひとつのサーバ上で試してみることとする。
公式ページのCentralized Setup with Event Parsingを参考にする。
動作させるコンポーネントとデータの流れは、こんな感じ。
![]() |
Figure.1 logstashのアーキテクチャ |
Redisのインストール
まずはRedisをインストールする。/tmp/redisにRedisのソースをダウンロード。
$ cd /tmp $ mkdir redis $ cd redis $ wget http: //redis .googlecode.com /files/redis-2 .4.17. tar .gz $ tar xvzf redis-2.4.17. tar .gz redis-2.4.17/ redis-2.4.17/.gitignore (snip) … |
$ cd redis-2.4.17 $ make cd src && make all make [1]: ディレクトリ ` /tmp/redis/redis-2 .4.17 /src ' に入ります (snip) … |
$ cd .. $ mv redis-2.4.17 /opt/redis |
$ cd /opt/redis $ src /redis-server [27291] 12 Sep 12:58:43 - 0 clients connected (0 slaves), 567120 bytes in use |
shipper(ログを親サーバに転送する役割)を立ち上げる
次に、Apacheのログを集めてRedisに送る機能を実装する。今回は同一サーバ上のApacheログを対象にするが、このセクションで実施する作業を別のサーバでも行えば、そのサーバ上のApacheログも収集することが可能のはず。
まずはlogstashの設定ファイルを作る。
$ cd /opt/logstash $ vi shipper.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | input { file { type => "apache" path => "/var/log/httpd/access_log" } } filter { grok { type => "apache" pattern => "%{COMBINEDAPACHELOG}" } } output { stdout { debug => true debug_format => "json" } redis { host => "127.0.0.1" data_type => "list" key => "logstash" } } |
$ vi shipper.sh |
1 | java -jar logstash-1.1.1-monolithic.jar agent -f shipper.conf |
$ chmod +x shipper.sh $ . /shipper .sh |
[27291] 12 Sep 13:00:29 - 1 clients connected (0 slaves), 567120 bytes in use |
indexer(Redisからデータを受け取りDBに書き込む役割)を立ち上げる
Centralized Setup with Event Parsingの例は、DB(ElasticSearch)を別途インストールせよ、と書いてある。が、今回は簡易テストなので、logstashにバンドルされているElasticSearchを使うことにする。$ cd /opt/logstash $ vi indexer.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | input { redis { host => "127.0.0.1" type => "redis-input" data_type => "list" key => "logstash" message_format => "json_event" } } output { stdout { debug => true debug_format => "json" } elasticsearch { embedded => true # host => "127.0.0.1" } } |
$ vi indexer.sh |
1 | java -jar logstash-1.1.1-monolithic.jar agent -f indexer.conf |
$ chmod +x indexer.sh $ . /indexer .sh |
[27291] 12 Sep 13:00:29 - 2 clients connected (0 slaves), 567120 bytes in use |
この状態で、ブラウザでApacheに適当にアクセスしてみる。
すると、indexerの標準出力に
{"@source":"file://server/var/log/httpd/access_log","@type":"apa che","@tags":[],"@fields":{"clientip":["xx.xx.xx.xxx"],"ident":[ "-"],"auth":["-"],"timestamp":["12/Sep/2012:13:01:32 +0900"],"ZO NE":["+0900"],"verb":["GET"],"request":["/images/true.png"],"htt pversion":["1.1"],"response":["200"],"bytes":["248"],"referrer": 1279358964"],"agent":["\"Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 15.0) Gecko/20100101 Firefox/15.0.1\""]},"@timestamp":"2012-09-1 2T04:01:32.889000Z","@source_host":"server","@source_path":"/var /log/httpd/access_log","@message":"xx.xx.xx.xxx - - [12/Sep/2012 :13:01:32 +0900] \"GET /images/true.png HTTP/1.1\" 200 248 \"htt 58964\" \"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/201 00101 Firefox/15.0.1\""} |
Web GUIを起動する
最後に、ログ検索用のWeb GUIを起動する。起動スクリプトを書く。
$ cd /opt/logstash $ vi web.sh |
1 | java -jar logstash-1.1.1-monolithic.jar web --backend elasticsearch: /// ? local |
$ chmod +x web.sh $ . /web .sh |
![]() |
Figure.2 logstash WebUIでのログ検索 |
期待通りにログ収集と検索が実装できたので、今回はここまで。
0 コメント:
コメントを投稿