munin perl版プラグインの便利関数

munin

muninのプラグインを書いているときに、前回の実行結果を参照したくなることがある。一時ファイルを作って保存すればいいんだけど面倒だなーと思ってたら、実はライブラリに用意されていた。探してみるもんだな。

You shall put these lines before using Munin::Plugin functions :

use lib $ENV{'MUNIN_LIBDIR’};
use Munin::Plugin;
(中略)
The module exports these functions: clean_fieldname, set_state_name, save_state, restore_state, tail_open, tail_close.

save_state, restore_state

ステータスを保存・読み込む関数。ハッシュに値を入れておいて読み書きすると便利。プラグイン初回実行時はデータが存在しないので、existsチェックが必要。
データ保存した時刻が必要ならそれもハッシュに入れる。

my %data = restore_state();
:
save_state(%data);

tail_open, tail_close

ファイルを前回の続きから読みたい場合に使う。ログファイルを舐めるときに便利。読み込み位置は別途保存が必要。

サンプル

munin_stats プラグインが参考になります。

munin

Posted by ず@沖縄