LoginSignup
40
37

More than 5 years have passed since last update.

Dstatのプラグインの書き方

Last updated at Posted at 2014-06-01

Dstatはメジャーなツールですが、プラグインの書き方についてはあまりドキュメントがなかったので、ここに書いてみようと思います。

Hello, World!

dstat_hello_world.pyを作って、(CentOSの場合)/usr/share/dstat/にコピーします

class dstat_plugin(dstat):
    def __init__(self):
        self.name   = 'hello_world'
        self.type   = 'f'
        self.width  = 5
        self.scale  = 1000
        self.vars   = ('hello', 'world')

    #def check(self):
    #    # Check anything

    def extract(self):
        self.val['hello'] = 1
        self.val['world'] = 10000

dstat --hello-worldを実行すると以下が出力されます。

hello_world
hello world
1.00  10.0k
1.00  10.0k
1.00  10.0k
...

解説

extractが一定時間ごとに呼び出されるので、その時の値をセットすればdstatがよきにはからってくれます。

また/usr/share/dstat/*.pyのプラグインが参考になると思います。

以下はソースコードからの推測ですが…(間違ってたらごめんなさい)

  • self.width

    • 表示時の文字列の幅です。5〜7ぐらいがレイアウトをぶちこわさない感じです
  • self.type: 値の型

    • s: 文字列
    • i: Integer (たぶん)
    • d: Integer (たぶん)
    • f: float
    • b: byte
  • self.scale

    • 1000とか1024を設定しておくと、その単位で1kとか1mみたいな表示にしてくれます
  • オプションはDSTAT_MYSQL_USERみたいな環境変数で渡すのがお作法のようです

    • オプションはcheckで検証して、間違っていたら例外を投げます
40
37
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
40
37