LoginSignup
9

More than 5 years have passed since last update.

Windowsのtypeperfコマンドをfluentdのin_execで扱う

Posted at

0. 前書き

キャプチャ.JPG

"WindowsのCPU使用率/空きメモリ量ログを fluentd で転送して、GrowthForecastでグラフ化"というエントリーがあります。

この中ではtypeperfコマンドの標準出力をファイルにリダイレクトし、そのファイルをtailプラグインで読み込んでいます。が、この場合、

  • typeperfコマンドの実行
  • リダイレクトしたファイルのローテーションの方法

などを別途考える必要があります。

本稿では、fluentdのin_execプラグイン、およびその他プラグインを利用してfluentdの起動にあわせて起動しつつ、ファイルにリダイレクトせず(=ファルローテーションの方法を考えなくても良い)設定を紹介しています。

具体的には
1. in_execでtypeperfコマンドを叩きます。しかし、この結果はTSVでないので
2. fluent-plugin-parserでパースをかけて期待する形として再エミットします
っていう感じです。

before
ユーザー → typeperf → 標準出力 → ファイル(リダイレクト) → tailで読み取り → 期待するフォーマット
after
in_exec → typepef → 標準出力 → 標準入力 → parser → 期待するフォーマット

fluentdは導入済みであることを前提としています。。
未導入に場合は、以下のエントリーを参照してください。
"Windowsをfluentdで動かす"
 

1. プラグインの導入

in_execはfluentdに標準に含まれているので特にすることはありません。
parserプラグインを以下のコマンドで導入します。
tagomorisさん謹製です。感謝。

> fluent-gem install fluent-plugin-parser

Fetching: fluent-plugin-parser-0.3.4.gem (100%)
Successfully installed fluent-plugin-parser-0.3.4
Installing ri documentation for fluent-plugin-parser-0.3.4
1 gem installed

 

2. fluent.confへの設定の記述

出力は標準出力としています。必要に応じて変えてください。
typeperfの実行間隔は10秒としています。

<source>
  type exec
  command typeperf -sc 1 "\Processor(_Total)\% Processor Time" "\Memory\Available Bytes"
  keys msg
  run_interval 10s
  tag raw.pc.cpumem
</source>

<match raw.pc.cpumem>
 type parser
 remove_prefix raw
 key_name msg
 format /\"(?<date>[0-9:./ ]*)\",\"(?<cpupercent>[0-9]*)\.[0-9]*\",\"(?<memav>[0-9.]*)\.[0-9]*\"/
</match>

<match pc.cpumem>
  type stdout
</match>

 

3. いざ!

動かしましょう。

> fluentd -c C:/fluent/fluent.conf

2014-06-27 10:48:05 +0900 [info]: starting fluentd-0.10.46
2014-06-27 10:48:05 +0900 [info]: is windows platform : true
2014-06-27 10:48:05 +0900 [info]: spawn command to main (windows) : C:/Ruby193/bin/ruby.exe 'C:/Ruby193/bin/fluentd' -c ./fluent.conf -u
2014-06-27 10:48:06 +0900 [info]: starting fluentd-0.10.46
2014-06-27 10:48:06 +0900 [info]: is windows platform : true
2014-06-27 10:48:06 +0900 [info]: reading config file path="./fluent.conf"
2014-06-27 10:48:07 +0900 [info]: gem 'fluent-plugin-parser' version '0.3.4'
2014-06-27 10:48:07 +0900 [info]: gem 'fluentd' version '0.10.46'
2014-06-27 10:48:07 +0900 [info]: using configuration file: <ROOT>
  <source>
    type exec
    command typeperf -sc 1 "\Processor(_Total)\% Processor Time" "\Memory\Available Bytes"
    keys msg
    run_interval 10s
    tag raw.pc.cpumem
  </source>
  <match raw.pc.cpumem>
    type parser
    remove_prefix raw
    key_name msg
    format /\"(?<date>[0-9:./ ]*)\",\"(?<cpupercent>[0-9]*)\.[0-9]*\",\"(?<memav>[0-9.]*)\.[0-9]*\"/
  </match>
  <match pc.cpumem>
    type stdout
  </match>
</ROOT>
2014-06-27 10:48:07 +0900 [info]: adding source type="exec"
2014-06-27 10:48:07 +0900 [info]: adding match pattern="raw.pc.cpumem" type="parser"
2014-06-27 10:48:07 +0900 [info]: adding match pattern="pc.cpumem" type="stdout"
2014-06-27 10:48:19 +0900 [warn]: pattern not match: "(PDH-CSV 4.0)","\\RL32330U\Processor(_Total)\% Processor Time","\\RL32330U\Memory\Available Bytes"
2014-06-27 10:48:19 +0900 pc.cpumem: {"date":"06/27/2014 10:48:19.464","cpupercent":"4","memav":"902524928"}
2014-06-27 10:48:19 +0900 [warn]: pattern not match: 終了しています。お待ちください...                                      
2014-06-27 10:48:19 +0900 [warn]: pattern not match: コマンドは、正しく完了しました。
2014-06-27 10:48:32 +0900 [warn]: pattern not match: "(PDH-CSV 4.0)","\\RL32330U\Processor(_Total)\% Processor Time","\\RL32330U\Memory\Available Bytes"
2014-06-27 10:48:32 +0900 pc.cpumem: {"date":"06/27/2014 10:48:32.107","cpupercent":"1","memav":"906846208"}
2014-06-27 10:48:32 +0900 [warn]: pattern not match: 終了しています。お待ちください...                                      
2014-06-27 10:48:32 +0900 [warn]: pattern not match: コマンドは、正しく完了しました。

ハイ、取れましたね!

no pattern matchが多いです。これはtypeperfを1ショットでコールすると、以下のような4行のメッセージが流れるためです。

"(PDH-CSV 4.0)","\RL32330U\Processor(_Total)\% Processor Time"
"06/27/2014 10:45:23.431","3.376004"
終了しています。お待ちください...
コマンドは、正しく完了しました。

まぁご勘弁を。とりあえず目的は達成です。
 

4. 注意事項とその他

可視化の例としては以下のエントリーを参考にしてください。
"WindowsのCPU使用率/空きメモリ量ログを fluentd で転送して、GrowthForecastでグラフ化"

おしまい。

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
9