Windowsのイベントログをfluentdで取り扱いたい
fluentdというログ収集のための便利なソフトがあるそうな。ちょっと前まで、いわゆる、普通のアプリケーションしか作ったことがなかったので、まったく知りませんでした。
ということで、体験。今回の記事ではとりあえずfluentdで集めるところまで。いつかMongoDBに突っ込む予定です。
参考にしたのは以下の記事です。
blog.idcf.jp
まずrubyのインストール(fluentdのため)
以下のサイトから、2.2.5をインストールしました(x64)。PATHはインストーラのオプションで忘れずに通します。
Downloads
その後、元記事に従ってdevkitもダウンロードして展開します。展開先も元記事に従って「c:\ruby」にしました。
元記事に従って、c;\rubyディレクトリで以下を実行します。テキストファイルを編集することになっていますが、
既にそのようになっていました。
C:\ruby>ruby dk.rb init [INFO] found RubyInstaller v2.2.5 at C:/Ruby22-x64 Initialization complete! Please review and modify the auto-generated 'config.yml' file to ensure it contains the root directories to all of the installed Rubies you want enhanced by the DevKit. C:\ruby>ruby dk.rb install [INFO] Updating convenience notice gem override for 'C:/Ruby22-x64' [INFO] Installing 'C:/Ruby22-x64/lib/ruby/site_ruby/devkit.rb' C:\ruby>
gitのインストール
元サイトに従い以下からインストール。
Git - Downloads
私は宗教的な理由により、as-isを選んでます。
fluentdのダウンロード
ここで、cloneは元サイト通りに行きましたが、checkoutはエラーになりました。
そのまますっ飛ばしてうまくいってます。
つまり、以下を実行しました。
git clone https://github.com/fluent/fluentd.git cd fluentd gem install bundler bundle bundle exec rake build gem install pkg\fluentd-0.14.6.gem -N
起動のテスト
とりあえず起動のテストをするだけでよいので、サービス登録はせず、起動だけしてみました。
mkdir c:\fluentd\log fluentd -c C:\fluentd\example\in_forward.conf -o c:\fluentd\log\fluent.log
すると、何やらログファイルに書き出されます。
また、別のコマンドプロンプトウィンドウで以下を実行すると、その内容もログに出てきました!
echo {"string":"Hello Fluentd Windows Service"} | fluent-cat test
ログのイメージはこんな感じ。
2016-09-21 00:12:47 +0900 [info]: gem 'fluentd' version '0.14.6' 2016-09-21 00:12:47 +0900 [info]: adding match pattern="test" type="stdout" 2016-09-21 00:12:47 +0900 [info]: adding source type="forward" 2016-09-21 00:12:47 +0900 [info]: using configuration file: <ROOT> <system> rpc_endpoint "0.0.0.0:24444" </system> <source> @type forward </source> <match test> @type stdout </match> </ROOT> 2016-09-21 00:12:47 +0900 [info]: listening fluent socket on 0.0.0.0:24224 2016-09-21 00:13:11 +0900 test: {"string":"Hello Fluentd Windows Service"}
次にイベントログを収集するためのプラグインをインストールします
C:\fluentd>gem install fluent-plugin-winevtlog Fetching: win32-eventlog-0.6.6.gem (100%) Successfully installed win32-eventlog-0.6.6 Fetching: fluent-plugin-winevtlog-0.0.4.gem (100%) Successfully installed fluent-plugin-winevtlog-0.0.4 Parsing documentation for win32-eventlog-0.6.6 unable to convert "\xE3" from ASCII-8BIT to UTF-8 for CHANGES, skipping Installing ri documentation for win32-eventlog-0.6.6 Parsing documentation for fluent-plugin-winevtlog-0.0.4 Installing ri documentation for fluent-plugin-winevtlog-0.0.4 Done installing documentation for win32-eventlog, fluent-plugin-winevtlog after 2 seconds 2 gems installed
また、手順に従って、record-modifierも入れておきましょう。
C:\fluentd>gem install fluent-plugin-record-modifier Fetching: fluent-plugin-record-modifier-0.5.0.gem (100%) Successfully installed fluent-plugin-record-modifier-0.5.0 Parsing documentation for fluent-plugin-record-modifier-0.5.0 Installing ri documentation for fluent-plugin-record-modifier-0.5.0 Done installing documentation for fluent-plugin-record-modifier after 1 seconds 1 gem installed
さて、今度はfluent_winevt.confを設定します。
<source> type winevtlog channel application,security pos_file C:/fluentd/log/eventlog.pos read_interval 5 tag winevt.raw </source> #<match winevt.raw> # type record_modifier # char_encoding Windows-31J:utf-8 # tag winevt.filtered #</match> <match winevt.raw> type file path C:/fluentd/test/output time_slice_format %Y%m%d_%H%M%S time_slice_wait 5s </match>
ここで私の環境では問題が発生しました。真ん中の文字コード変換のところ、有効だとエラーになりました。
とりあえず、shift_jisのまま出るようにしたのが上のバージョンです。
すると以下のようにログがとれますよ。
2016-09-24T01:14:25+09:00 winevt.raw {"channel":"application","record_number":"5646","time_generated":"2016-09-24 01:14:24 +0900","time_written":"2016-09-24 01:14:24 +0900","event_id":"999","event_type":"information","event_category":"0","source_name":"EvtTest","computer_name":"DESKTOP-82ICNJ6","user":"takuma","description":"テストメッセージ\r\n"}
ちなみに、イベントをテスト的に発生させるには管理者コマンドプロンプトで以下を実行します。
eventcreate /L Application /SO mytest /ID 1000 /T Information /D "これはテスト"
文字コード変換がうまくいかないのは、要調査。
以上。
2016/10/19 追記:このイベントログをmongoDBに入れるという処理を以下の投稿にて実施しています。
takumats.hatenablog.com