apacheアクセスログなどの時系列ログを、fluentd(td-agent)からMongoDB(ドキュメント指向データベース)とElasticSearch(検索エンジン)に取り込み、確認する方法を記載しています。
apache稼働サーバ(192.168.1.10)と、MongoDBとElasticSearch稼働サーバ(192.168.1.20)は、別サーバとしています。
1.apache稼働サーバの td-agent.conf
apache稼働サーバ(192.168.1.10)におけるtd-agent.conf 設定を記載します。
source ディレクティブ設定(入力元)は以下になります。
<source> type tail path /var/log/httpd/access_log pos_file /var/log/td-agent/httpd-access_log.pos tag apache.access format apache2 </source>
match ディレクティブ設定は以下です。
<match apache.access> type forward flush_interval 10s <server> host 192.168.1.20 port 24224 weight 60 </server> </match>
td-agent.conf の 設定を変更したら、td-agent を再起動します。
2.MongoDBとElasticSearch稼働サーバの td-agent.conf
MongoDBとElasticSearch稼働サーバ(192.168.1.20)におけるtd-agent.conf 設定を記載します。
source ディレクティブ設定(入力元)は以下になります。
<source> type forward port 24224 </source>
match ディレクティブ設定は以下です。
<match apache.access> type copy <store> type elasticsearch index_name adminpack type_name apache include_tag_key true tag_key @log_name host localhost port 9200 logstash_format true flush_interval 10s </store> <store> type mongo host localhost port 27017 database fluentd collection apache_access flush_interval 10s </store> </match>
td-agent.conf の 設定を変更したら、td-agent を再起動します。
3.ブラウザでWebアクセス
apacheアクセスログを取得する為、apache稼働サーバ(192.168.1.10)にブラウザでアクセスします。
4.MongoDB で取込確認
以下のコマンドでMongoDBに取り込まれている事を確認します。
$ mongo MongoDB shell version: 2.4.9 connecting to: test > use fluentd switched to db fluentd > db.apache_access.count(); 123 > (このタイミングでWebアクセスします) > db.apache_access.count(); 132 >
MongoDBに取り込まれている事が確認できました。
条件を指定して確認する事も出来ます。
> db.apache_access.count({"path" : /projects/}); 28 >
5.ElasticSearchで取り込み確認
ElasticSearch の便利なGUIツールであるKibana3で確認してみると、取り込まれている事が確認できます。
ついでに、Kibana2 画面でも見てみます。
この仕組みは面白く、便利に使えそうです。
構成を模索しながら、使える仕組みにしていきたいと思います。