Muninの監視ノード名を変更した時にデータを引き継ぐ更新スクリプトを作ってみた

Muninで監視ノードのグループ名やノード名を変更した場合、データファイルがノード名に紐付いているので設定ファイルを変更しただけでは蓄積したデータを引き継ぐ事ができません。ノード名の変更にあわせてデータファイルを適切に更新する必要があります。Muninをepelなどからパッケージでインストールした場合、デフォルトのディレクトリ構成は

データディレクト /var/lib/munin
HTMLファイル /var/www/html/munin

になっています。この場合監視ノードのデータファイルは以下の場所に存在します。

  • /var/lib/munin/<グループ名>/<ホスト名>-<監視項目>.rrd
  • /var/lib/munin/state-<グループ名>-<ホスト名>.storable
  • /var/www/html/munin/<グループ名>/<ホスト名>/

また以下のデータはグラフ閲覧時に作成されるので(graph_strategy が cgi の場合)、ノード名変更時に削除しておいた方がよいです。

  • /var/lib/munin/munin-cgi-graph/<グループ名>/<ホスト名>/

グループ名やホスト名が更新された場合、これらをルールに則って更新しなければなりません。

スクリプトで一括更新

ノード名の変更ってそんなにするものではないとも思いますが、無いわけでもなく今までは rename(1) コマンドとかで手作業でやっていました。ただ今回複数のノードを整理する必要が出てきてめんどくさかったので、今後の事も考えて更新するスクリプトを書いてみました。以下で公開しています。

できることは以下です。

  • ノード名変更時に引き継ぐデータの移行処理
  • 監視ノード削除時に対象のデータを削除

実行の手順としては

  1. ファイルに実行権付与
  2. スクリプト内の datadirhtmldircgitmpdir を環境に合わせて設定
  3. munin.conf でノードの設定更新
  4. 実行権限つけて引数なしで実行

です。インタラクティブに実行できるようにしてあるのであとは実行すればわかると思います。

環境は

  • CentOS6(RHEL6) 64bit / Ubuntu
  • Munin2.0系

を想定しています。rename(1) コマンドをスクリプト内で使っていますが、Ubuntu だとコマンドの実装が違うとかいう話を聞いたのでそちらの動作確認はしていません。Ubuntu の簡易チェックを入れて対応しました。

使用例
  • ノード名を変更(example.com を example.jp に変更)
[hoge.example.com]
    address hoge.example.com
    use_node_name yes

[hoge.example.jp]
    address hoge.example.jp
    use_node_name yes

に変更した場合、以下を実行します。

$ sudo /opt/sbin/munin-modify-data.sh
Please select the action
1) rename
2) delete
3) exit
Enter menu number> 1
Please enter OLD GROUP name > example.com
Please enter OLD NODE name > hoge.example.com
Please enter NEW GROUP name [example.com] > example.jp
Please enter NEW NODE name [hoge.example.com] > hoge.example.jp

OLD Group: example.com
OLD Node : hoge.example.com
New Group: example.jp
New Node : hoge.example.jp

OK? [y/n]: y
Waiting for the munin-update and munin-html processes to finish.

move state files:
        /var/lib/munin/state-example.com-hoge.example.com.storable >>> /var/lib/munin/state-example.jp-hoge.example.jp.storable
rename data files:
        rename example.com/hoge.example.com example.jp/hoge.example.jp /var/lib/munin/example.com/hoge.example.com-*
move html files:
        /var/www/html/munin/example.com/hoge.example.com/ >>> /var/www/html/munin/example.jp/hoge.example.jp/
delete graph directory:
        /var/lib/munin/cgi-tmp/munin-cgi-graph/example.com/hoge.example.com/

delete the empty directory '/var/lib/munin/example.com'
delete the empty directory '/var/www/html/munin/example.com'
delete the empty directory '/var/lib/munin/cgi-tmp/munin-cgi-graph/example.com'
  • ノード名を変更(hoge グループの hoge-ap を hoge グループの foo-ap に変更)
[hoge;hoge-ap]
    address hoge-ap.internal
    use_node_name yes

[hoge;foo-ap]
    address foo-ap.internal
    use_node_name yes

に変更した場合、以下を実行します。

$ sudo munin-modify-data.sh
Please select the action
1) rename
2) delete
3) exit
Enter menu number> 1
Please enter OLD GROUP name > hoge
Please enter OLD NODE name > hoge-ap
Please enter NEW GROUP name [hoge] >
Please enter NEW NODE name [hoge-ap] > foo-ap

OLD Group: hoge
OLD Node : hoge-ap
New Group: hoge
New Node : foo-ap

OK? [y/n]: y
Waiting for the munin-update and munin-html processes to finish.
.......
move state files:
        /var/lib/munin/state-hoge-hoge-ap.storable >>> /var/lib/munin/state-hoge-foo-ap.storable
rename data files:
        rename hoge/hoge-ap hoge/foo-ap /var/lib/munin/hoge/hoge-ap-*
move html files:
        /var/www/html/munin/hoge/hoge-ap/ >>> /var/www/html/munin/hoge/foo-ap/
delete graph directory:
        /var/lib/munin/cgi-tmp/munin-cgi-graph/hoge/hoge-ap/

delete the empty directory '/var/lib/munin/cgi-tmp/munin-cgi-graph/hoge'
  • ノードデータを削除
$ sudo munin-modify-data.sh
Please select the action
1) rename
2) delete
3) exit
Enter menu number> 2
Please enter DELETE GROUP name > hoge
Please enter DELETE NODE name > foo-ap

DELETE Group: hoge
DELETE Node : foo-ap

OK? [y/n]: y
Waiting for the munin-update and munin-html processes to finish.
...
delete state files:
        /var/lib/munin/state-hoge-foo-ap.storable
delete data files:
        /var/lib/munin/hoge/foo-ap-*
delete html files:
        /var/www/html/munin/hoge/foo-ap/
delete graph directory:
        /var/lib/munin/cgi-tmp/munin-cgi-graph/hoge/foo-ap/

delete the empty directory '/var/lib/munin/hoge'
delete the empty directory '/var/www/html/munin/hoge'
delete the empty directory '/var/lib/munin/cgi-tmp/munin-cgi-graph/hoge'

設定で graph_strategy の設定は cgi が前提です。(cronはほぼ使わないですよね?)

処理的には以下の感じです。

  • cronで munin-update と munin-html が動いている場合はプロセスが終了するまで処理が待機します。めんどくさい場合は cron の実行処理を一時的に止めておいたほうがいいかもしれません。
  • ノードの移動で元のグループにノードがいなくなった場合、グループのディレクトリも削除します。
  • 同じくノードの削除の場合もグループにノードがいなくなった場合、グループのディレクトリも削除します。

データのアップデートのプロセスが動いていないタイミングを見て実行するようにしていますが、厳密なデータの整合性までは見ていないので手順には気をつけてください。例えば munin.conf のノード名を変更する前にデータの移動を先にした場合、古いノードのデータが残骸として残るかもしれません。
データ移行中の一時的なデータの欠損が許容できるなら、

  1. cron でのデータ収集を一時的に停止
  2. munin.confの設定を変更
  3. スクリプトの実行
  4. cron でのデータ収集再開

とするのが無難でしょうか。

まとめ

コマンドの引数で情報渡して実行するのも考えたんですが、インタラクティブな方が個人的に使いやすかったのでこの形にしました。*1
何か気づいた点やおかしな点があれば教えてください。

*1:AWS などのクラウド環境でサーバの自動登録と削除とかでデータの削除とかもするならバッチ処理的な動作もあった方がいいかもとは思いますが。