monitで簡単サーバー監視


Monitは、プロセス監視やログ監視をしてくれる軽量なツールです。

リソースはNewRelic使ってるから、Zabbixとかは入れるほどでも無いしという場合などに便利です。

Ubuntu 10.10にMonitをインストール

[bash]
$ sudo apt-get install monit
$ sudo dpkg -l | grep monit
ii monit 1:5.0.3-3 A utility for monitoring and managing daemon
$ sudo service monit start
Starting daemon monitor: monit won’t be started/stopped
unless it it’s configured
please configure monit and then edit /etc/default/monit
and set the "startup" variable to 1 in order to allow
monit to start
[/bash]

起動するには、指示の通り/etc/default/monitのstartupに1を設定

[text]
startup=1
[/text]

起動

[bash]
$ sudo service monit start
Starting daemon monitor: empty config, please edit /etc/monit/monitrc.
[/bash]

まずはミニマムな設定をしてみます。
/etc/monit/monitrcの以下の箇所のコメントを外して、適当な値を設定します。
[text]
set daemon 120
set logfile syslog facility log_daemon
set mailserver smtp.gmail.com port 587 username "xxxxx@co-meeting.com" password "mypassword" using tlsv1
set alert sysadm@foo.bar
set httpd port 2812 and
allow localhost
allow admin:monit
include /etc/monit/conf.d/*
[/text]
set daemon 120は120秒間隔でチェックが実行されるということです。
set mailserver …の設定はGmailのSMTPサーバーの例です。
また、各種監視設定はmonitrcにも続けて記述できますが、include /etc/monit/conf.d/*を指定して、conf.dに監視対象ごとにファイルを分けて設定します。
set httpd port 2828 and …は管理用のWeb画面を使わなくてもmonitコマンドからもアクセスされるため設定は必須です。

設定が終わったら再起動します。
[bash]
$ sudo service monit restart
[/bash]
起動を確認するためにWebインターフェースを表示してみます。
http://xxx.xxx.xxx.xxx:2812/にアクセスし、基本認証にadmin/monitを入れると以下の画面が表示されます。
monit-web-interface

幾つか監視設定を追加してみます。
監視設定のサンプルは公式サイトのConfiguration Examplesに充実しているので参考にしてください。

Nginx

/etc/monit.d/conf.d/nginx.conf
を作成し以下を記述
[text]
# Nginx
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
[/text]

MongoDB

おおよそのデーモンのpidは/var/runにありますが、MongoDBはpidfilepathを指定していない場合は/mondod.lockになります。
dbpath=/data/mondodb/の場合、/etc/monit.d/conf.d/mongodb.confを以下のように記述します
[text]
# MongoDB
check process mongodb
with pidfile "/data/mongodb/mongod.lock"
start program = "/sbin/start mongodb"
stop program = "/sbin/stop mongodb"
if failed port 28017 protocol http
and request "/" with timeout 10 seconds then restart
if 5 restarts within 5 cycles then timeout
[/text]

Redis
[text]
check process redis-server with pidfile "/var/run/redis.pid"
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
if failed host 127.0.0.1 port 6379 then restart
if 5 restarts within 5 cycles then timeout
[/text]

以下の内容を

参考になるリンク:
Installing Monit in Ubuntu 12.04 Servers
Linux resource monitoring and some tuning tips
monitrcの設定項目
被制御デーモンの登録 (monitrc)
monit と少し仲良くなったお話 – monitでApache2, MySQLを監視

,