python で munin plugin を書いてみる
DESCRIPTION
TRANSCRIPT
Python でmunin plugin を書いてみる
2013-04-13 Shizuoka.py
自己紹介
● となか(@ftnk)● インフラエンジニア
○ Solaris / Puppet / Nagios / munin / GrowthForecast / serverspec etc.
○ Python はまれに運用・監視用のスクリプトを書く■ shell script だと面倒で Ruby がない環境の時■ まれにしか書かないので、覚えない
● 開発?○ 最近、serverspec に Solairs 用の matcher 追加
の pull request を送ったりしてます
agenda1. 今回の目的2. munin ?3. munin plugin ?4. munin plugin の構成5. python-munin6. cpu 使用率の plugin を書く
(ただし、Solaris)7. まとめ
今回の目的
● Python がよくわかっていなくても、簡単に munin plugin が書けることを知ってもらう
munin ?● munin はリソース監視ツール
○ リソースの値を取得してグラフ化
● 類似のツール○ mrtg / cacti / CloudForecast / GrowthForecast etc.
munin plugin?● リソースの値の取得とグラフに関する情報を扱
う● リソースの値の取得
○ なんらかのコマンドを実行するなどして値を取得
● グラフに関する情報○ グラフの形式 (draw)
■ LINE / AREA / STACK○ 値のあつかい (type)
■ GAUGE / DERIVE / COUNTER
munin plugin の構成
● 必要な機能○ グラフに関する情報の出力
■ plugin にオプションとして "config" を渡すと出力される
○ リソースの値の取得と出力■ plugin にオプションを渡さなければ、リソースの値が
出力される
グラフに関する情報の出力
● グラフ全体に関する情報○ graph_title: グラフのタイトル○ graph_category: グラフのカテゴリー○ graph_vlabel: 縦軸のタイトル○ graph_scale: 値に合わせてグラフをスケールさせる
か?
グラフに関する情報の出力
● リソースごとのグラフに関する情報○ system.label: system というグラフのラベル○ system.draw: system というグラフの形式○ system.type: system というグラフの値のあつかい
リソースの値の取得と出力
● 値の取得○ 好きなようにとってください
● 出力○ 出力は以下のフォーマットでおこなう
■ system.value (値)
python-munin● 今回は python-munin というライブラリを使って
plugin を書いてみます。● http://samuelks.com/python-munin/● インストール
○ git や tarball でソースを入手○ python setup.py build ○ sudo python setup.py install
python-muninpython-munin を使うと、以下のような感じで plugin が書けます。
from munin import MuninPlugin
class CPUPlugin(MuninPlugin): # グラフ全体の情報
title = "cpu usage (test)"
@property def fields(self): # 各グラフの情報の出力
return fuga
def execute(self): # 値の取得と出力
return hoge
if __name__ == "__main__": CPUPlugin().run()
CPU 使用率の plugin を書く
● 今回は munin 本体に含まれ、shell script で書かれている CPU 使用率の plugin を python-munin を使って書いてみます。
大枠の用意
import commandfrom munin import MuninPlugin
class CPUPlugin(MuninPlugin): # グラフ全体の情報
title = "cpu usage (test)"
@property def fields(self): # 各グラフの情報の出力
return fuga
def execute(self): # 値の取得と出力
return hoge
if __name__ == "__main__": CPUPlugin().run()
● プラグイン内部でコマンドを実行するので、"import command" が必要
グラフ全体の情報
class CPUPlugin(MuninPlugin) title = "cpu usage (test)" args = "--base 1000 -l 0" vlabel = "cpu usage" scale = False category = system
個々のグラフの情報
def fields(self): retun [ ("kernel", dict( label = "system", draw = "AREA", min = "0",
type = "DERIVE", )), ("user", dict( label = "system", draw = "STACK", min = "0",
type = "DERIVE", )), (省略)
]
● 個々のグラフの情報をリストでまとめて返しま
す
● 各グラフの情報は辞書にまとめます
● グラフの描画に前回取得した値との差を使う
ので、type が "DERIVE" です
● グラフは塗り潰しで積み重ねるので、 1 つ目
のグラフの draw を "AREA"、2 つ目以降のグラフの draw を "STACK" にします
config をつけて実行
% python cpu-test.py configgraph_title cpu usage (test)graph_category systemgraph_args --base 1000 -l 0graph_vlabel cpu usagegraph_scale nokernel.draw AREAkernel.min 0kernel.type DERIVEkernel.label systemuser.draw STACKuser.min 0user.type DERIVEuser.label userwait.draw STACKwait.min 0wait.type DERIVEwait.label waitidle.draw STACKidle.min 0idle.type DERIVEidle.label idle
● config をつけて実行すると、左のようにグラフの情報が出力されることを確認できます
値の取得と出力
● 以下のコマンドの出力を集計します% kstat -p -c misc -m cpu_stat -s '/^(user|kernel|wait|idle)$/'cpu_stat:0:cpu_stat0:idle 701652cpu_stat:0:cpu_stat0:kernel 135979cpu_stat:0:cpu_stat0:user 34858cpu_stat:0:cpu_stat0:wait 0cpu_stat:1:cpu_stat1:idle 609950cpu_stat:1:cpu_stat1:kernel 221631cpu_stat:1:cpu_stat1:user 40414cpu_stat:1:cpu_stat1:wait 0cpu_stat:2:cpu_stat2:idle 702211cpu_stat:2:cpu_stat2:kernel 132556cpu_stat:2:cpu_stat2:user 37226cpu_stat:2:cpu_stat2:wait 0cpu_stat:3:cpu_stat3:idle 633591cpu_stat:3:cpu_stat3:kernel 198948cpu_stat:3:cpu_stat3:user 39449cpu_stat:3:cpu_stat3:wait 0
値の取得
def execute(self): stats = commands.getoutput("kstat -p -c misc -m cpu_stat -s '/^(user|kernel|wait|idle)$?/'" )
values = { 'idle':0, 'kernel':0, 'wait':0, 'idle':0 }
for i in stats.splitlines(): key, value = i.split(':')[-1].split('\t') values[key] += int(value)
return values
● commands.getoutput でコマンドの実行結果を取得
● 値は辞書で返す● 集計のため 0 で初期化● コマンドの実行結果を行ご
とに処理して集計
実行
% python cpu-test.py kernel.value 546745idle.value 2077219user.value 124432wait.value 0
グラフ
以下のようなグラフができる
まとめ
● Python をよくわかっていなくても python-munin を使うことで、munin-plugin が書ける