サーバーモニタリング · 2 min read · Oct 16, 2025
CentOS 5.2 における munin と monit を使用したサーバーモニタリング - ページ 2
5 monit のインストールと設定
次に monit をインストールします:
yum install monit次に、monit のシステム起動リンクを作成します:
chkconfig --levels 235 monit onmonit のデフォルト設定ファイルは /etc/monit.conf で、いくつかの設定例が見つかります(http://mmonit.com/wiki/Monit/ConfigurationExamples でさらに多くの設定例が見つかります)すべてコメントアウトされていますが、monit に /etc/monit.d ディレクトリ内の設定ファイルも探すよう指示します。したがって、/etc/monit.conf を変更する代わりに、新しい設定ファイル /etc/monit.d/monitrc を作成します。私の場合、proftpd、sshd、mysql、apache、postfix を監視したいと思います。ポート 2812 で monit ウェブインターフェースを有効にし、https ウェブインターフェースを希望し、ユーザー名 admin とパスワード test でウェブインターフェースにログインし、monit が root@localhost にメールアラートを送信するようにしたいので、私のファイルは次のようになります:
vi /etc/monit.d/monitrc| set daemon 60 set logfile syslog facility log_daemon set mailserver localhost set mail-format { from: [email protected] } set alert root@localhost set httpd port 2812 and SSL ENABLE PEMFILE /var/certs/monit.pem allow admin:test check process proftpd with pidfile /var/run/proftpd.pid start program = "/etc/init.d/proftpd start" stop program = "/etc/init.d/proftpd stop" if failed port 21 protocol ftp then restart if 5 restarts within 5 cycles then timeout check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout check process mysql with pidfile /var/run/mysqld/mysqld.pid group database start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout check process apache with pidfile /var/run/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host www.example.com port 80 protocol http and request "/monit/token" then restart if cpu is greater than 60% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 500 MB for 5 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout check process postfix with pidfile /var/spool/postfix/pid/master.pid group mail start program = "/etc/init.d/postfix start" stop program = "/etc/init.d/postfix stop" if failed port 25 protocol smtp then restart if 5 restarts within 5 cycles then timeout |
(サーバー上に実際に存在するプロセスのみを確認するようにしてください - さもなければ monit は起動しません。つまり、monit に Postfix を確認するよう指示しても、システムに Postfix がインストールされていない場合、monit は起動しません。)
設定ファイルはかなり自己説明的です。オプションについて不明な点がある場合は、monit ドキュメントを参照してください: http://mmonit.com/monit/documentation/monit.html
monit 設定の apache 部分には次のような記述があります:
| if failed host www.example.com port 80 protocol http and request "/monit/token" then restart |
これは、monit が www.example.com のポート 80 に接続し、/monit/token ファイルにアクセスしようとすることを意味します。これは /var/www/www.example.com/web/monit/token です。なぜなら、私たちのウェブサイトのドキュメントルートは /var/www/www.example.com/web だからです。monit が成功しない場合、Apache が実行されていないことを意味し、monit はそれを再起動します。次に、/var/www/www.example.com/web/monit/token ファイルを作成し、そこにランダムな文字列を書き込みます:
mkdir /var/www/www.example.com/web/monit
echo "hello" > /var/www/www.example.com/web/monit/token次に、SSL 暗号化された monit ウェブインターフェースに必要な pem 証明書 (/var/certs/monit.pem) を作成します:
mkdir /var/certs
cd /var/certs証明書を作成するために OpenSSL 設定ファイルが必要です。それは次のようになります:
vi /var/certs/monit.cnf| # create RSA certs - Server RANDFILE = ./openssl.rnd [ req ] default_bits = 1024 encrypt_key = yes distinguished_name = req_dn x509_extensions = cert_type [ req_dn ] countryName = Country Name (2 letter code) countryName_default = MO stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Monitoria localityName = Locality Name (eg, city) localityName_default = Monittown organizationName = Organization Name (eg, company) organizationName_default = Monit Inc. organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = Dept. of Monitoring Technologies commonName = Common Name (FQDN of your server) commonName_default = server.monit.mo emailAddress = Email Address emailAddress_default = [email protected] [ cert_type ] nsCertType = server |
次に、次のように証明書を作成します:
openssl req -new -x509 -days 365 -nodes -config ./monit.cnf -out /var/certs/monit.pem -keyout /var/certs/monit.pemopenssl gendh 512 >> /var/certs/monit.pemopenssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pemchmod 700 /var/certs/monit.pem最後に、monit を起動できます:
/etc/init.d/monit start今、ブラウザを https://www.example.com:2812/ に向けてください(ポート 2812 がファイアウォールでブロックされていないことを確認してください)、admin と test でログインすると、monit ウェブインターフェースが表示されるはずです。次のようになります:

(メイン画面)

(Apache ステータスページ)
/etc/monit.d/monitrc の設定に応じて、monit はサービスが失敗した場合に再起動し、サービスのプロセス ID が変更された場合に通知メールを送信します。
楽しんでください!
6 リンク
- munin: http://munin.projects.linpro.no
- monit: http://mmonit.com/monit
- CentOS: http://www.centos.org
- RPMforge: https://rpmrepo.org/RPMforge
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。