LoginSignup
24
24

More than 5 years have passed since last update.

fluentd Windows版でタスクスケジューラのログを収集する

Last updated at Posted at 2015-01-13

0. 前書き

Windowsのタスクスケジューラ。
Linuxでいうcronみたいなもんですが、Windowsベースでシステムを組まれている環境では業務上のバッチ処理などでは結構使われていたりします。PCメーカー組み込みのタスクも結構入っていたりしますね。

本投稿ではこのログをfluentdのWindowsブランチで集めることにチャレンジします。

ここではWindows Server 2012で動作確認しています。
Windows Server 2008R2以降あたり(?)でWindowsの仕様変更があり、これ以前のバージョンでは、おそらくこの手順では収集できません。あしからず。(というより本投稿よりもっと簡単な手順でできるはずですが、まぁ今更2008以前ということもないのでやりません)

で、あらためて。
この投稿は

  • Windows Server 2012 日本語版
  • Ruby 2.1 x64(RubyInstaller 2.1.5)
  • fluentd Windowsブランチのバージョン 0.10.46.win1410210001
  • fleunt-plugin-winevtlog 0.0.3

を使用しています。
ポイントは題名のまんまタスクスケジューラのログもfluentdで収集可能なところでしょうか。

なお、本記事ではログの格納はしておらず、標準出力(コマンドプロンプト)に表示するだけです。
MySQLやMongo、BigQueryとして格納したい場合は以下の投稿が参考になるでしょう。

”IISのログをfluentdで集めてWindows上のMySQLに突っ込む”
”Windowsイベントログをfluentdプラグインで集めてmongoDBに突っ込む”
”fluentd Windows版でBigQueryする”

また、Windows内で作業をクローズしたいという要求がないのであれば、Windowsからのログ収集においては以下の手順があります。参考にしてください。

”Collecting Log Data from Windows”

1. タスクスケジューラのタスク履歴を有効にします

既定ではオフになっていますので、タスクスケジューラの画面の右側ペインの「すべてのタスク履歴を無効にする」をクリックし、有効にします(表示がトグルします)。
tasksch.JPG

2. fluentd Windows版の導入

キャプチャ.PNG

以下の記事を参考にして導入します。

”fluentd WindowsブランチをRuby 2.1上で動かす”

この作業の終了で、すでにfluentdがWindowsで動くはずです。

3. fluent-plugin-winevtlogの導入

少なくともWindows Server 2008R2以降において、タスクスケジューラのログはWindowsのイベントログないしはその類型のひとつとして落ちてきます。
これを読み取るためにfluentdプラグイン fluent-plugin-winevtlogを利用します。
以下の投稿を参考に導入します。

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

まぁ要するにはコマンドプロンプトで

> gem install fluent-plugin-winevtlog --no-ri --no-rdoc

> gem install fluent-plugin-record-modifier --no-ri --no-rdoc

を叩けっていう話です。

4. レジストリ操作(ゴニョゴニョ)

button-regedit.png

レジストリを操作します。
これはfluent-plugin-winevtlogが使用しているwin32-eventlogライブラリ(v0.6.2)がWindowsの仕様変更には追随しきれていないので、私どもでゴニョゴニョして整合をとるという作業です。

step1. レジストリエディタで以下の2つのレジストリキーを作成します。

キー
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Microsoft-Windows-TaskScheduler/Operational
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Microsoft-Windows-TaskScheduler/Operational\Microsoft-Windows-TaskScheduler

下のキーは、上のキーの子キーです。名前が似ていますので間違えないようにしましょう。

setp2. 1で作成したキーに以下のエントリと値を作成します

setp.1で作成した後者のキー

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\EventLog\Microsoft-Windows-TaskScheduler/Operational\Microsoft-Windows-TaskScheduler

に次のエントリを作成します。

エントリ名
EventMessageFile REG_SZ C:\Windows\system32\schedsvc.dll
ProviderGuid REG_SZ {de7b24ea-73c8-4a09-985d-5bdadcfa9017}

5. fluentd設定ファイルの記述

キャプチャ5.PNG

「1. fluentd Windowsブランチの導入」をうけて

C:\fluent\fluent.conf

に対して、次のように設定ファイルを書きます。

fluent.conf
<source>
  type winevtlog
  channel Microsoft-Windows-TaskScheduler/Operational
  pos_file C:\fluent\tasksch.pos
  tag tasksch.raw
</source>

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

<match tasksch.filtered>
  type stdout
</match>

6. いざ!

bash-148836_2.png

まず、コマンドプロンプトを立ち上げ、fluentdを起動します。

> fluentd -c C:/fluent/fluent.conf
2015-01-12 13:52:57 +0900 [info]: starting fluentd-0.10.46.win141021001
2015-01-12 13:52:57 +0900 [info]: is windows platform : true
2015-01-12 13:52:57 +0900 [info]: spawn command to main (windows) : C:/Ruby21-x64/bin/ruby.exe 'C:/Ruby21-x64/bin/fluentd' -c C:/fluent/fluent.conf -U 1916
2015-01-12 13:52:57 +0900 [info]: starting fluentd-0.10.46.win141021001
2015-01-12 13:52:57 +0900 [info]: is windows platform : true
2015-01-12 13:52:57 +0900 [info]: reading config file path="C:/fluent/fluent.conf"
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-mixin-config-placeholders' version '0.3.0'
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-mixin-plaintextformatter' version '0.2.6'
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-plugin-bigquery' version '0.2.5'
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-plugin-buffer-lightening' version '0.0.2'
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-plugin-record-modifier' version '0.2.0'
2015-01-12 13:52:58 +0900 [info]: gem 'fluent-plugin-winevtlog' version '0.0.3'
2015-01-12 13:52:58 +0900 [info]: gem 'fluentd' version '0.10.46.win141021001'
2015-01-12 13:52:58 +0900 [info]: using configuration file: <ROOT>
  <source>
    type winevtlog
    channel Microsoft-Windows-TaskScheduler/Operational
    pos_file C:\fluent\tasksch.pos
    tag tasksch.raw
  </source>
  <match tasksch.raw>
    type record_modifier
    tag tasksch.filtered
    char_encoding Windows-31J:utf-8
  </match>
  <match tasksch.filtered>
    type stdout
  </match>
</ROOT>
2015-01-12 13:52:58 +0900 [info]: adding source type="winevtlog"
2015-01-12 13:52:58 +0900 [info]: adding match pattern="tasksch.raw" type="record_modifier"
2015-01-12 13:53:02 +0900 [info]: adding match pattern="tasksch.filtered" type="stdout"

タスクをスケジュールして実行しなければ何もおきないので、試しにスケジュールして実行してみましょう。
コントロールパネルのタスクスケジューラから動くようにします。
ここでは適当に作業時間の一分後くらいに 「fluentd-win-tasksch-test」 というタスク名でコマンドプロンプトが立ち上がるよにしておきました。

待ちます...

...
2015-01-12 13:57:57 +0900 tasksch.filtered: {"channel":"microsoft-windows-taskscheduler/operational","record_number":"2589","time_generated":"2015-01-12 13:57:56 +0900","time_written":"2015-01-12 13:57:56 +0900","event_id":"106","event_type":"information","event_category":"106","source_name":"Microsoft-Windows-TaskScheduler","computer_name":"WIN-1FML22KMKJH","user":"SYSTEM","description":"ユーザー \"WIN-1FML22KMKJH\\Administrator\" はタスク スケジューラのタスク \"\\fluentd-win-tasksch-test\" を登録しました。\r\n"}
2015-01-12 13:59:58 +0900 tasksch.filtered: {"channel":"microsoft-windows-taskscheduler/operational","record_number":"2590","time_generated":"2015-01-12 13:59:57 +0900","time_written":"2015-01-12 13:59:57 +0900","event_id":"107","event_type":"information","event_category":"107","source_name":"Microsoft-Windows-TaskScheduler","computer_name":"WIN-1FML22KMKJH","user":"SYSTEM","description":"タスク スケジューラは、時間による起動条件で、タスク \"\\fluentd-win-tasksch-test\" の \"{76F8D668-A419-47A1-B9CD-4D6C4625FA2D}\" インスタンスを起動しました。\r\n"}
2015-01-12 13:59:58 +0900 tasksch.filtered: {"channel":"microsoft-windows-taskscheduler/operational","record_number":"2591","time_generated":"2015-01-12 13:59:57 +0900","time_written":"2015-01-12 13:59:57 +0900","event_id":"129","event_type":"information","event_category":"129","source_name":"Microsoft-Windows-TaskScheduler","computer_name":"WIN-1FML22KMKJH","user":"SYSTEM","description":"タスク スケジューラは、プロセス ID 3456 でタスク \"\\fluentd-win-tasksch-test\"、インスタンス \"C:\\Windows\\system32\\cmd.EXE\" を起動しました。\r\n"}
2015-01-12 13:59:58 +0900 tasksch.filtered: {"channel":"microsoft-windows-taskscheduler/operational","record_number":"2592","time_generated":"2015-01-12 13:59:57 +0900","time_written":"2015-01-12 13:59:57 +0900","event_id":"100","event_type":"information","event_category":"100","source_name":"Microsoft-Windows-TaskScheduler","computer_name":"WIN-1FML22KMKJH","user":"SYSTEM","description":"タスク スケジューラは、ユーザー \"WIN-1FML22KMKJH\\Administrator\" の \"\\fluentd-win-tasksch-test\" タスクの \"{76F8D668-A419-47A1-B9CD-4D6C4625FA2D}\" インスタンスを開始しました。\r\n"}
2015-01-12 13:59:58 +0900 tasksch.filtered: {"channel":"microsoft-windows-taskscheduler/operational","record_number":"2593","time_generated":"2015-01-12 13:59:57 +0900","time_written":"2015-01-12 13:59:57 +0900","event_id":"200","event_type":"information","event_category":"200","source_name":"Microsoft-Windows-TaskScheduler","computer_name":"WIN-1FML22KMKJH","user":"SYSTEM","description":"タスク スケジューラは、タスク \"\\fluentd-win-tasksch-test\" のインスタンス \"{76F8D668-A419-47A1-B9CD-4D6C4625FA2D}\" で操作 \"C:\\Windows\\system32\\cmd.EXE\" を開始しました。\r\n"}

でた!いいですね!
実際の運用では、ログの格納先に応じて、以下の記事を参考に都合のいいところに格納してください。

MySQLに!
”IISのログをfluentdで集めてWindows上のMySQLに突っ込む”

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

BigQueryに!
”fluentd Windows版でBigQueryする”

7. その他

タスクはログ「オフ」状態での実行がスケジュールされることが多いと思いますが、上記手順はログ「オン」状態のコマンドプロンプトからのfluentd起動となっているため「あれ?どうすんのこれ?」という方もいらっしゃったのではないかと思います。

とどのつまり”fluentdをWindowsサービスとして動かせないの?"、という話ですね。

実際のところ、fluentd Windows版はWindowsサービスとして動かせます。需要があれば機会をみてそのあたりの投稿をしたいと思います。

おわり。

24
24
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
24
24