LoginSignup
42
42

More than 5 years have passed since last update.

WindowsのCPU使用率/空きメモリ量ログを fluentd で転送して、GrowthForecastでグラフ化

Last updated at Posted at 2014-06-10

0.前書き

WindowsのCPU使用率/空きメモリ量のログを、fluentd で転送し、GrowthForecastでグラフ化します。

Windowsの監視におけるfluentdのユースケースとしてはフォワーダーとしてnxlogを使うパターンが紹介されています。
http://docs.fluentd.org/ja/articles/windows

が、 ここではあえてfluentdのWindowsブランチを使って直接fluentdがログを読み、そのままGrowthforを使うことにチャレンジしてみます

目指す構成はこんな感じ。

キャプチャ.PNG

CPU使用率/空きメモリ量の取得には、Windowsに標準添付されている

typeperf

コマンドを使用します。これはWindowsのパフォーマンスオブジェクトにアクセスし様々な情報を取得し標準出力することができるツールで、間隔指定および繰り返し回数も指定できます。
これをファイルにリダイレクトし、tailプラグインで読み出し、growthforecastプラグインで出力します。

ポイントは
* Windows上でfluentdを動かしている
* ログの収集をfluentdのみで実現できている
ところです。多分。

 

1. GrowthForecastのサーバーを立てます。

38ed9c8f-4165-8dd3-3450-31477b2a62e32.png

これはLinuxでたてます。
以下のいのうえたくやさんのサイトがよくまとまっていると思います。

GrowthForecast を CentOS 6.3 にインストールして Supervisor で管理してみた

Grwothforecastを起動し、また、5125番ポートで通信できるようにしておきましょう。

 

2. 監視対象のWindowsにfluentdを導入します。

キャプチャ.PNG

以下の投稿内容と同等のところまでできるようにfluentdをセットアップします。

WIndowsでfluentdを動かす

以降、fluentdの設定ファイルは以下の場所にあるものとします。

C:\fluent\fluent.conf

 

3. fluentdのGrowthforecastプラグインを導入します。

tagomorisさん謹製です。感謝。
以下のコマンドを実行します。

> fluent-gem install fluent-plugin-growthforecast
Fetching: uuidtools-2.1.4.gem (100%)
Fetching: fluent-mixin-config-placeholders-0.2.4.gem (100%)
Fetching: resolve-hostname-0.0.4.gem (100%)
Fetching: fluent-plugin-growthforecast-0.2.7.gem (100%)
Successfully installed uuidtools-2.1.4
Successfully installed fluent-mixin-config-placeholders-0.2.4
Successfully installed resolve-hostname-0.0.4
Successfully installed fluent-plugin-growthforecast-0.2.7
4 gems installed
Installing ri documentation for uuidtools-2.1.4...
Installing ri documentation for fluent-mixin-config-placeholders-0.2.4...
Installing ri documentation for resolve-hostname-0.0.4...
Installing ri documentation for fluent-plugin-growthforecast-0.2.7...
Installing RDoc documentation for uuidtools-2.1.4...
Installing RDoc documentation for fluent-mixin-config-placeholders-0.2.4...
Installing RDoc documentation for resolve-hostname-0.0.4...
Installing RDoc documentation for fluent-plugin-growthforecast-0.2.7...

> 

 

4. CPU使用率/空きメモリ量の取得およびファイル書き出しを開始します。

コマンドプロンプトを立ち上げ、以下のコマンドを実行します。

typeperf -si 10 "\Processor(_Total)\% Processor Time" "\Memory\Available Bytes" >> C:\logs\cpumem.log

上記コマンドは

  • 10秒間隔でCPUの使用率と空きメモリ量をバイト単位で取得し、
  • その結果をC:\logs\cpumem.logファイルに書き込みます。

C:\logsフォルダは事前に作成しておかないとエラーとなります。実行できたらファイルをメモ帳で開いて書き込みが行われていることを確認しましょう。ただし、10秒間隔での書き込みですので、ちょっと待ってから確認しましょう。

ファイルへのサンプル出力結果は以下の様になります。
サンプルなので1秒間隔です。

"06/08/2014 14:04:06.930","30.112302","1388998656.000000"
"06/08/2014 14:04:07.931","30.906585","1387864064.000000"
"06/08/2014 14:04:08.931","25.428495","1382842368.000000"
"06/08/2014 14:04:09.931","28.137116","1384321024.000000"
"06/08/2014 14:04:10.932","34.829438","1386487808.000000"
"06/08/2014 14:04:11.933","38.335650","1379934208.000000"
"06/08/2014 14:04:12.933","38.306032","1381634048.000000"
"06/08/2014 14:04:13.934","28.939293","1380098048.000000"

これをtailプラグインで読み取り、growthforecastプラグインで出力するようにfluentdの設定ファイルを記述すれば良いわけです。

※上記コマンドを実行した場合、Ctrl+Cによる終了まで、CPU使用率とメモリの空き容量がcpumem.logに無限に追記されていきます。
実際の運用では、typeperfコマンドのオプションとして実行回数を指定したり、ログファイルがローテションできるようなバッチファイルを組む必要があるでしょう。

2014.06.28 追記
in_execを使ってファイルのリダイレクトを必要としない(=ファイルローテーションを考慮しなくてよい)設定例を別エントリーで記載しました。興味あれば参考にしてください。
Windowsのtypeperfコマンドをfluentdのin_execで扱う

 

5. fluentdの設定ファイルを記述します。

キャプチャ5.PNG

#2のステップにより、以下の場所に設定ファイルがあります。

C:\fluent\fluent.conf

このファイルに以下の内容を記述します。
この内容以外の設定は全てコメントアウトしちゃってください。

<source>
 type tail
 format /\"(?<date>[0-9:./ ]*)\",\"(?<cpupercent>[0-9]*)\.[0-9]*\",\"(?<memav>[0-9.]*)\.[0-9]*\"/
 path C:/logs/cpumem.log
 tag pc.cpumem
</source>
<match pc.cpumem>
    type growthforecast
    gfapi_url http://---your growthforecast ip here---:5125/api/
    service my_server_status
    tag_for section
    name_keys cpupercent,memav
</match>

typeperfによる値の出力は小数形式でおこなわれるのですが、grwothforecastは小数をうけつけません。なので、ここではtailの正規表現で小数分を切り捨てちゃっています。てへぺろ。

※手元の環境のtypeperfでは上記正規表現で問題ないのですが、Windowsのバージョンなどによっては形式の微妙な違いがあるかもしれません。その場合は状況にあわせて正規表現を変更してください。

 

6.いざ!

fluentdを実行しましょう。
もし#4でのtypeperfコマンドを止めているのであれば、先に動かしましょう。
そして以下のコマンドを実行します。

> C:
> cd \
> fluentd -c ./fluent/fluent.conf
2014-06-09 14:10:02 +0000 [info]: starting fluentd-0.10.46
2014-06-09 14:10:02 +0000 [info]: is windows platform : true
2014-06-09 14:10:02 +0000 [info]: spawn command to main (windows) : C:/Ruby193/bin/ruby.exe 'C:/Ruby193/bin/fluentd' -c ./fluent/fluent.conf -u
2014-06-09 14:10:02 +0000 [info]: starting fluentd-0.10.46
2014-06-09 14:10:02 +0000 [info]: is windows platform : true
2014-06-09 14:10:02 +0000 [info]: reading config file path="./fluent/fluent.conf"
2014-06-09 14:10:02 +0000 [info]: gem 'fluent-mixin-config-placeholders' version '0.2.4'
2014-06-09 14:10:02 +0000 [info]: gem 'fluent-plugin-growthforecast' version '0.2.7'
2014-06-09 14:10:02 +0000 [info]: gem 'fluentd' version '0.10.46'
2014-06-09 14:10:02 +0000 [info]: using configuration file: <ROOT>
  <source>
    type tail
    format /\"(?<date>[0-9:./ ]*)\",\"(?<cpupercent>[0-9]*)\.[0-9]*\",\"(?<memav>[0-9.]*)\.[0-9]*\"/
    path C:/logs/cpumem.log
    tag pc.cpumem
  </source>
  <match pc.cpumem>
    type growthforecast
    gfapi_url http://------------:5125/api/
    service my_server_status
    tag_for section
    name_keys cpupercent,memav
  </match>
</ROOT>
2014-06-09 14:10:02 +0000 [info]: adding source type="tail"
2014-06-09 14:10:02 +0000 [warn]: 'pos_file PATH' parameter is not set to a 'tail' source.
2014-06-09 14:10:02 +0000 [warn]: this parameter is highly recommended to save the position to resume tailing.
2014-06-09 14:10:02 +0000 [info]: adding match pattern="pc.cpumem" type="growthforecast"
2014-06-09 14:10:02 +0000 [info]: following tail of C:/logs/cpumem.log





もしこの段でエラーが出た場合、何かの設定を間違えてしまっている可能性が高いです。エラー内容を確認し対応しましょう。

問題なければブラウザで 

http://growthforecastのアドレス:5125

を開き、スケールを8hoursに変更しておきます。
キャプチャ.PNG

growthforecastは一分間ごとでの集計処理となるため、1分まってから画面を更新しましょう。

でた!
キャプチャ.PNG

メモリの空きが少ないですね。
なぜかというとmicroインスタンスだから:)。

ぼーっと見ていても変化なし。当たり前だけど。
キャプチャ2.PNG

Chromeのタブを50個開いたらちょっと動いた:)
キャプチャ3.PNG

 

7.その他

typeperfコマンドは、プロセス単位のメモリ使用状況や、ネットワーク・インタフェースの転送バイト量など、いろいろなカウンタが取れます。そこはお好みで。

また、以下の投稿の

WIndowsでfluentdを動かす

7.注意と解決策にもある通り、現在のfluend Windows版は終了処理に問題があります。終了するときはコマンドプロンプト毎終了するか、同じく上記内にリンクされているCoolioを使いましょう。

2014.06.28 追記
in_execを使ってファイルのリダイレクトを必要としない(=ファイルローテーションを考慮しなくてよい)設定例を別エントリーで記載しました。興味あれば参考にしてください。
Windowsのtypeperfコマンドをfluentdのin_execで扱う

おわり

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