LoginSignup
12
14

More than 5 years have passed since last update.

Muninでアラートメールを送信する

Last updated at Posted at 2017-04-05

環境

  • CentOS7.3(ですが、Muninが動いているならなんでもOK)

mailコマンドが使えるか確認

Muninのアラートメールは標準でmailコマンドを使うので、そもそもmailコマンドで外部にメールが送信できるか確認しておく。

echo "test" | mail -s "test mail" xxxxx@gmail.com

などとしてみる。なお、mailコマンド自体のインストールやリレー設定(gmail)についてはこちらをご覧下さい。

監視項目の確認

Muninはきれいな?グラフが表示されるのでなんとなく利用できますが、実際何というプラグインの何という項目かがいまいちピンときません。
まずは適切に監視項目を理解したいところです。

送信設定

Muninのアラートメール設定はmunin.confで行いますが、その典型的な記述例は下記のようになります。

/etc/munin/munin.conf
#mail送信コマンドの設定
contact.admin.command mail -s "Munin Alerts ${var:group}::${var:host}" to@test.com
#送信対象とする警告レベルの設定
contact.admin.always_send warning critical

#サーバ毎に閾値の設定
[localhost]
    address 127.0.0.1
    use_node_name yes
    cpu.user.warning 10 #監視項目

上記の例だと、

  • adminというコンタクトに[Munin Alerts グループ名 ホスト名]というタイトルでメールして!
  • warningとcriticalのときにメール送ってね。
  • で、ローカルホストのcpu.userではwarningを10(%)以上の時にしますね。

という設定をしています。なお、閾値の設定自体は何をwarningとするか?criticalとするかの設定であり、メールの送信とは直接関係ありません。閾値の設定は、cpu.user.warning 10のところになります。これは、

プラグイン名.監視項目.警告レベル 閾値

という書式で指定されています。
つまりアラートを設定するためには、[プラグイン名]、[監視項目]がわからないと設定できません。

グラフにプラグイン名や項目名が表示されてくれればいいのですが、表示さていませんよね。。

プラグイン名を調べる

プラグイン名(および有効・無効)は下記のコマンドで調べられるようです。

munin-node-configure

各種プラグインの種類や説明はある程度こちらで確認できるようです。

かなり大雑把な感じです。

cpuというプラグインがあるようなので動作を見てみます。
監視項目と現在の値が表示されているようです。

munin-run cpu

user.value 28179
nice.value 28842
system.value 16377
idle.value 5657644
iowait.value 157
irq.value 0
softirq.value 536
steal.value 0
guest.value 0

プラグインの設定状況はconfigオプションをつけることで確認できます。warningやcriticalの標準値などもわかります(cpuには設定されていないようですが)。

munin-run cpu config

graph_title CPU usage
graph_order system user nice idle iowait irq softirq
graph_args --base 1000 -r --lower-limit 0 --upper-limit 100
graph_vlabel %
graph_scale no
graph_info This graph shows how CPU time is spent.
graph_category system
graph_period second
system.label system
system.draw AREA
system.min 0
system.type DERIVE
system.info CPU time spent by the kernel in system activities
user.label user
user.draw STACK
user.min 0
user.type DERIVE
user.info CPU time spent by normal programs and daemons
.
.
.

例えばdfだと、下記のようにwarningが92%, criticalが98%に設定されているようです。

munin-run df config
graph_title Disk usage in percent
graph_args --upper-limit 100 -l 0
graph_vlabel %
graph_scale no
graph_category disk
_dev_mapper_cl_root.label /
_dev_mapper_cl_root.warning 92
_dev_mapper_cl_root.critical 98
_dev_sda1.label /boot
_dev_sda1.warning 92
_dev_sda1.critical 98

メールの送信

では、実際にメールを送信してみます。既に紹介した記述をmunin.confに記述します。confファイルをいじってもmuninサーバはデーモンとして常駐してるわけではなく、5分毎にcronで起動しているので再起動の必要はないようです(起動時に反映される)。

munin.confに既に[localhost]が定義されているので、その前後に記述します。

/etc/munin/munin.conf
## for mail
contact.admin.command mail -s "munin test" xxxxx@gmail.com #環境に応じて変更
contact.admin.always_send warning critical

# a simple host tree
[localhost]
    address 127.0.0.1
    use_node_name yes
    cpu.user.warning 10

そもそもメールがちゃんとmuninから送られるかのチェックは下記のように強制送信すれば確認できます。
下記の例ではadminコンタクトに強制的にメールを送信しています。

sudo -u munin sh -c "/usr/share/munin/munin-limits --contact admin --force"

関連リンク

  • メール送信の詳細設定はこちらが参考になります。

その他

Muninは特定のフォーマットでログを吐くと可視化してくれるのでプラグインを開発することでサーバ管理以外にもいろいろと利用できそうです。

12
14
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
12
14