LoginSignup
36
33

More than 5 years have passed since last update.

Windowsイベントログをfluentdプラグインで集めてmongoDBに突っ込む

Last updated at Posted at 2014-10-13

fluent-plugin-windows-eventlog

2017/4/18:
本稿で紹介している Windowsイベントログを収集するためのプラグイン fluent-plugin-winevtlogはfluent直下のプロジェクトとして
fluent-plugin-windows-eventlog
に移行しました。名前も代わっていますのでご注意ください。

0. 前書き

fluentdにはWindowsブランチがあって皆様のissue/プルリクを待ちながら進行しています。
本稿では(おそらく)fluentdをWindowsで使うのであれば最もアクセスしたい!と思われるWindowsイベントログの収集にチャレンジします。

ポイントは

です。おそらくfluentd初のWindows専用プラグインでしょう。

なお本稿ではイベントログの格納先としてmongoDBを使っていますが、mongoDBでなければならない理由は全くありません。まぁなんとなくです。
結果としてfluentdのmongoDB プラグインもWindowsで動きそう、ということは言えるかもしれません。実際は用途に応じてRDBMSに突っ込むなりforwardで飛ばすなり、mongoで動かすなりしましょう。

また、せっかくなのでコマンドプロンプト(stdout)でも確認できるようにしておきましょう。
なので目指す構成は以下の様になります。
都合により1ノードで全部やりますが、実際の場面では、ノードを複数で構成する形になるでしょう。

winevtlog-qiita2.JPG

1. fluentdをWindowsにインストール

キャプチャ.PNG

これはこちらの投稿

Windowsでfluentdを動かす

を参照してください。ただし、相違点として、本稿ではRuby 2.0 の64ビット版を利用しています。
導入により、設定ファイルは C:\fluent\fluent.conf にあるものとします。

※OSのビット数と一致するRubyを使ったほうがベターです。というのも手元の環境では一致しない場合エラーが発生するケースがありました。
Ruby 2.1でのfluentd Windowsブランチの動作は安定していません。本稿の内容を試される場合は、2.0を利用したほうがよいでしょう。
mongoDB自体のセットアップはここではふれません。難しくない&検索で多数ヒットしますので、そちらを参照してください。

2. fluent-plugin-winevtlogをインストール

fluent-plugin-winevtlogはrubygemsで公開されています。
以下のコマンドでインストールします。

gem install fluent-plugin-winevtlog

現時点でバージョンは0.0.3です。なま温かい目とPRで見守りましょう。
ソースはGitHubでホストされています。
fluent-plugin-winevtlog

3. fluent-plugin-mongoをインストール

mongoのためのアウトプットプラグインを導入します。Masahiro Nakagawaさん謹製です。Thanks。以下のコマンドを実行します。

> gem install fluent-plugin-mongo

Successfully installed fluent-plugin-mongo-0.7.3
Parsing documentation for fluent-plugin-mongo-0.7.3
1 gem installed

特に問題ないでしょう。

※mongodb自体のセットアップはここではふれません。難しくない&検索で多数ヒットしますので、そちらを参照してください。

4. fluent-plugin-record-modifierをインストール

再びMasahiro Nakagawaさん謹製です。Thanks。

少し説明します。

ここで利用しているfluent-plugin-winevtlogプラグインはイベントログをWindowsシステムにあわせたロケールで取り出します。そのため、例えば日本語環境であれば、Windows-31Jでのログ取り出しとなりますが、これは何かと都合がよくありません。そこでfluent-plugin-record-modifierを利用してUTF-8へのコード変換をかけてしまう作戦にでます。
以下のコマンドを実行してインストールしましょう。

>fluent-gem install fluent-plugin-record-modifier

Fetching: fluent-mixin-config-placeholders-0.3.0.gem (100%)
Successfully installed fluent-mixin-config-placeholders-0.3.0
Fetching: fluent-plugin-record-modifier-0.2.0.gem (100%)
Successfully installed fluent-plugin-record-modifier-0.2.0
Parsing documentation for fluent-mixin-config-placeholders-0.3.0
Installing ri documentation for fluent-mixin-config-placeholders-0.3.0
Parsing documentation for fluent-plugin-record-modifier-0.2.0
Installing ri documentation for fluent-plugin-record-modifier-0.2.0
2 gems installed

特に問題無いでしょう。
では設定ファイルを記述します。

4. 設定ファイルを書く

さて、必要なプラグインを導入したので設定ファイルを書きましょう。
抑えるべき点は4つです。

  • Windows イベントログの読み取り設定
  • Record Modifierフィルタによる文字エンコード変更設定
  • mongoDBへの出力設定
  • コマンドプロンプトへの出力設定

以下のようにC:\fluent\fluent.confを記述します。

fluent.conf
<source>
  type winevtlog
  channel application
  pos_file C:\temp\mypos
  tag winevt.raw
</source>

<match winevt.raw>
  type record_modifier
  tag winevt.filtered
  char_encoding Windows-31J:utf-8
</match>

<match winevt.filtered>
  type copy
  <store>
    type stdout
  </store>
  <store>
    type mongo
    host localhost
    port 27017
    database fluentd
    collection test
  </store>
</match>

fluent-plugin-winevtlogのpos_file配置先であるc:\tempフォルダは事前に作成しておいてください。
その他fluent-plugin-winevtlogの読み取りの細かいパラメータは fluent-plugin-winevtlogで。

その他のプラグインについてもそれぞれのリポジトリを参照しましょう。

ここではApplicationログのみを読み取る設定となっています。
GitHubにも記載ありますが、現時点、read_from_head的なオプション(イベントログの頭からの読み取り)はありません。fluentd起動後にログされた内容のみが集取されます。

5. いざ!

fluentd起動します。

> c:
> cd c:\fluent
> fluentd -c ./fluent.conf
DL is deprecated, please use Fiddle
2014-10-11 01:34:35 +0900 [info]: starting fluentd-0.10.46
2014-10-11 01:34:35 +0900 [info]: is windows platform : true
2014-10-11 01:34:35 +0900 [info]: spawn command to main (windows) : C:/Ruby200-x64/bin/ruby.exe 'C:/Ruby200-x64/bin/fluentd' -c ./fluent.conf -u
DL is deprecated, please use Fiddle
2014-10-11 01:34:35 +0900 [info]: starting fluentd-0.10.46
2014-10-11 01:34:35 +0900 [info]: is windows platform : true
2014-10-11 01:34:35 +0900 [info]: reading config file path="./fluent.conf"
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-mixin-config-placeholders' version '0.3.0'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-mixin-config-placeholders' version '0.2.4'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-mixin-rewrite-tag-name' version '0.1.0'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-mixin-type-converter' version '0.0.3'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-mongo' version '0.7.3'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-mqtt' version '0.0.3'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-parser' version '0.3.4'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-record-modifier' version '0.2.0'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-watch-process' version '0.0.3'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-winevtlog' version '0.0.3'
2014-10-11 01:34:36 +0900 [info]: gem 'fluent-plugin-winevtlog' version '0.0.2'
2014-10-11 01:34:36 +0900 [info]: gem 'fluentd' version '0.10.46'
2014-10-11 01:34:36 +0900 [info]: using configuration file: <ROOT>
  <source>
    type winevtlog
    channel application
    pos_file C:\temp\mypos
    tag winevt.raw
  </source>
  <match winevt.raw>
    type record_modifier
    tag winevt.filtered
    char_encoding Windows-31J:utf-8
  </match>
  <match winevt.filtered>
    type copy
    <store>
      type stdout
    </store>
    <store>
      type mongo
      host localhost
      port 27017
      database fluentd
      collection test
    </store>
  </match>
</ROOT>
2014-10-11 01:34:36 +0900 [info]: adding source type="winevtlog"
2014-10-11 01:34:36 +0900 [info]: adding match pattern="winevt.raw" type="record_modifier"
2014-10-11 01:34:37 +0900 [info]: adding match pattern="winevt.filtered" type="copy"
      ** Notice: The native BSON extension was not loaded. **

      For optimal performance, use of the BSON extension is recommended.

      To enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set
      and run the following command:

        gem install bson_ext

      If you continue to receive this message after installing, make sure that
      the bson_ext gem is in your load path.

前記の通り現時点read_from_head的なオプションはないので、新しいイベントの待ち状態となっているはずです(イベントが発生しない限り上記のまま動かない)。

時間の有り余っている人には自然にイベントの発生を待つという手もありますが、得てしてプログラマは気が短いので人工的にいきましょう。
もう一つコマンドプロンプトを管理者権限でたちあげて Applicationログをコマンドで書き込みます。
※ソース mytestを新規に登録するので管理者権限が必要です。一度作成したら次からは管理者権限でなくても良いです。

eventcreate /L Application /SO mytest /ID 1000 /T Information /D "これはテスト"

で、fluentdを起動しているコマンドプロンプトを見てみましょう。

2014-10-11 01:35:02 +0900 winevt.filtered: {"channel":"application","record_number":"5347","time_generated":"2014-10-11 01:35:02 +0900",
"time_written":"2014-10-11 01:35:02 +0900","event_id":"1000","event_type":"information","event_category":"0","source_name":"mytest",
"computer_name":"NARUKIOKAHA5013","user":"naruki","description":"これはテスト\r\n"}

でた!
日本語もちゃんとでていますね。

では、mongoDBの方にも格納されているか、コマンド叩いて確認してみましょう。
上記fluent.confの通り、データベース名はfluentd、コレクション名はtestです。

C:\>mongo
MongoDB shell version: 2.6.4
connecting to: test
> use fluentd
switched to db fluentd
> db.test.find()
{ "_id" : ObjectId("54380ada0acb3c0734000001"), "channel" : "application", "record_number" : "5347", "time_generated" : "2014-10-11 01:35:02 +0900",
"time_written" : "2014-10-11 01:35:02 +0900", "event_id" : "1000", "event_type" : "information", "event_category" : "0", "source_name" : "mytest", 
"computer_name" : "NARUKIOKAHA5013", "user" : "naruki", "description" : "これはテスト\r\n", "time" : ISODate("2014-10-10T16:35:02Z") }

でた!

ちなみに
実際にログがバラバラおきていくとこんな感じです。参考まで。
mongoクライアントからの読み出した時のイメージ

read_from_mongo.JPG

6. その他

fluentd

fluentd Windows版のプロセス終了には問題があります。基本的にKILLシグナルによる終了処理を行っている(なぜかプロセスの終了処理がブロックされることへの対策)ので、fluentdおよびプラグインの終了処理は期待通りにならないケースがあると考えられます。
現実的にすべてのケースで問題となるかといえば、そうでない可能性も十二分にありますが、常に意識しておく必要はあるでしょう。十分な検証が必須です。

fluent-plugin-winevtlog

GitHubにも記載ありますが、setupログ、securityログへのアクセスにはUACによる管理者権限への昇格が必要です。これらのログを読み出す場合はfluentdを実行するためのコマンドプロンプトを管理者権限で立ち上げるのを忘れないでください。
特にsetupログについては、管理者権限なしで起動した場合、特段のエラーなくappilicationログの取得処理へとリダイレクトされてしまうため不要な混乱を生じる可能性があります。注意しましょう。

おしまい。

36
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
36
33