脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

muninのプラグインを自作する方法

muninのプラグインを自作する方法です。
流れとしては、①プラグインを作成して、②シムリンクを貼って有効化する、という流れになります
今回はテストとして、ログイン中のプロセス数を監視するプラグインを作成します

1. プラグインの作成

muninのプラグインは/etc/munin/pluginsの下ではなく、/usr/share/munin/pluginsの下にあります。
その中で使用したいpluginのみの/etc/munin/plugins以下にシムリンクを貼るという形になります

cd /usr/share/munin/plugins
ls -l 
  total 1548
  -rwxr-xr-x 1 root root  1516 Jan 27  2014 acpi
  -rwxr-xr-x 1 root root  3258 Jan 27  2014 amavis
  .
  .
  .
  -rwxr-xr-x 1 root root  2184 Jan 27  2014 yum
  -rwxr-xr-x 1 root root 28675 Jan 27  2014 zimbra_

今回はtest_pluginという名前で自作プラグインを作成します

vi test_plugin
======================================
#!/bin/sh
#%# family=contrib
#%# capabilities=autoconf
PROC=${0##*/files_}
TMPFILE="$MUNIN_PLUGSTATE/munin-files_${PROC}.state"

### for checking status of plugin
### `munin-node-configure` or `${this file_name} autoconf`
if [ "$1" = "autoconf" ]; then
        if [ -x /usr/bin/who ]; then
                echo yes
                exit 0
        else
                echo "no (no who cmd)"
                exit 0
        fi
fi

### for configuration
if [ "$1" = "config" ]; then
        echo graph_category Users
        echo graph_title count of login users

        # Y Label Name
        echo graph_vlabel Y-ziku_dayo

        # Y Label Value
        ## --lower-limit x
        ## --upper-limit x
        ## --rigid
        ## --base 1024
        echo graph_args --lower-limit 0 --upper-limit 10

        # each line name
        echo server1.label server1_login_users
        echo server2.label server2_login_users

        # each line style [LINE1|LINE2|LINE3|AREA|STACK]
        echo server1.draw LINE2
        echo server2.draw LINE2

        # each line threshold
        echo server1.warning  10
        echo server1.critical 20
        echo server2.warning  3
        echo server2.critical 5

        exit 0
fi

# collect
CMD=`who | wc -l`
echo "server1.value ${CMD}"
echo "server2.value 5"

======================================

コード説明

#!/bin/sh
#%# family=contrib
#%# capabilities=autoconf
PROC=${0##*/files_}
TMPFILE="$MUNIN_PLUGSTATE/munin-files_${PROC}.state"
  • ここらへんはmuninを使用するための設定の記述です
  • インストール先を変えていたりしない場合最初はコピペでいいでしょう


### for checking status of plugin
### `munin-node-configure` or `${this file_name} autoconf`
if [ "$1" = "autoconf" ]; then
        if [ -x /usr/bin/who ]; then
                echo yes
                exit 0
        else
                echo "no (no who cmd)"
                exit 0
        fi
fi
  • 次はpluginファイルが使用できるかどうかの確認に関する箇所です
  • whoコマンドのbinファイルが存在すればyes, しなければnoを返します
  • このコマンドは以下で確認できます
$ ./test_plugin autoconf
yes

$ munin-node-configure | grep test_plugin
test_plugin                | no  |
  ### => binファイルは存在しますが、まだプラグインをactivateしていないのでnoとなっています


### for configuration
if [ "$1" = "config" ]; then
        echo graph_category Users
        echo graph_title count of login users

        # Y Label Name
        echo graph_vlabel Y-ziku_dayo

        # Y Label Value
        ## --lower-limit x
        ## --upper-limit x
        ## --rigid
        ## --base 1024
        echo graph_args --lower-limit 0 --upper-limit 10

        # each line name
        echo server1.label server1_login_users
        echo server2.label server2_login_users

        # each line style [LINE1|LINE2|LINE3|AREA|STACK]
        echo server1.draw LINE2
        echo server2.draw LINE2

        # each line threshold
        echo server1.warning  10
        echo server1.critical 20
        echo server2.warning  3
        echo server2.critical 5

        exit 0
fi
echo graph_category ${カテゴリー名}
  • どのカテゴリーに所属させるかの設定をします

f:id:portaltan:20160314114649p:plain

echo graph_title ${グラフ名}
  • グラフに付ける名前を指定します

f:id:portaltan:20160314115104p:plain

echo graph_vlabel ${Y軸名}
  • Y軸に付けたい名前を設定します

f:id:portaltan:20160314120621p:plain

echo graph_args ${オプション}

–lower-limit ${数字} y軸のスタート地点を設定します
–upper-limit ${数字} y軸の上限を設定します
–rigid y軸の上下値のリミットを強制します
–base 1024 この数字を超える場合に1kと表示されます
f:id:portaltan:20160314115157p:plain

echo ${ラベル名}.label ${ラベルに付けたいタイトル}

f:id:portaltan:20160314115556p:plain

echo ${ラベル名}.draw [LINE1|LINE2|LINE3|AREA|STACK]
  • グラフの描画スタイルを指定します
    • LINE1 1ピクセルのライン
    • LINE2 2ピクセルのライン (デフォルト)
    • LINE3 3プクセルのライン
    • AREA 塗りつぶします
    • STACK 積み重ねます
echo ${ラベル名}.warning / ${ラベル名}.critical
  • 引数にとった数字を超えるとwarning|critical扱いになる

f:id:portaltan:20160314120551p:plain


# collect
CMD=`who | wc -l`
echo "server1.value ${CMD}"
echo "server2.value 5"
  • 実際にデータを取得する部分の記述になります
  • echo "${ラベル名}.value ${値}"の記法で設定します
  • 今回は確認のためにserver2は5で固定にしてあります(warningを発生させるため)

f:id:portaltan:20160314121116p:plain


2. プラグインのactivate
自作したプラグインは、まだmunin側に読み込まれていないので、activateします

cd /etc/munin/plugins

# シムリンクを貼る
ln -s /usr/share/munin/plugins/test_plugin test_plugin
ls -l test_plugin
  lrwxrwxrwx 1 root root 36 Mar 14 02:41 test_plugin -> /usr/share/munin/plugins/test_plugin

# munin側に自動で読み込ませる
sudo munin-node-configure --suggest

# 確認
sudo munin-node-configure | grep test_plugin
  test_plugin                | yes  |

# muninとapacheの再起動
/etc/init.d/munin-node restart
/etc/init.d/apache2 restart

以上で自作したpluginの項目が反映されるようになります