サーバー監視 · 4 min read · Oct 08, 2025

完璧な負荷分散と高可用性のウェブクラスター:Ubuntu 8.04 Hardy Heron上でXenを実行する2台のサーバー - ページ7

13. サーバー監視:muninとmonitを使用して(web1、web2)

この章では、muninとmonitを使用してウェブサーバーノードを監視する方法を説明します。Muninは、サーバーのほぼすべての側面(負荷平均、メモリ使用量、CPU使用量、MySQLスループット、eth0トラフィックなど)について、あまり設定を必要とせずに素晴らしいグラフィックを生成します。一方、monitはApache、MySQL、Postfixなどのサービスの可用性をチェックし、サービスが期待通りに動作していない場合は再起動などの適切なアクションを取ります。この2つの組み合わせにより、現在または今後の問題(「すぐにより大きなサーバーが必要です。負荷平均が急速に増加しています。」)を認識できるグラフィックと、監視されたサービスの可用性を確保するウォッチドッグが提供されます。

muninは複数のサーバーを監視できますが、ここではインストールされているシステムの監視についてのみ説明します。

13.1 muninのインストールと設定

apt-get install munin munin-node

次に、muninの設定ファイル/etc/munin/munin.confを編集する必要があります。

mv /etc/munin/munin.conf /etc/munin/munin.conf.bak
vi /etc/munin/munin.conf

web1.example.comで

dbdir   /var/lib/munin
htmldir /var/www/example/web/monitoring
logdir  /var/log/munin
rundir  /var/run/munin
tmpldir /etc/munin/templates
[web1.example.com]
    address 127.0.0.1
    use_node_name yes

web2.example.comで

dbdir   /var/lib/munin
htmldir /var/www/example/web/monitoring
logdir  /var/log/munin
rundir  /var/run/munin
tmpldir /etc/munin/templates
[web2.example.com]
    address 127.0.0.1
    use_node_name yes

web1とweb2の両方で

次に、ディレクトリ/var/www/example/web/monitoringを作成し、その所有権をユーザーとグループmuninに変更します。そうしないと、muninはそのディレクトリに出力を配置できません。その後、muninを再起動します:

mkdir -p /var/www/example/web/monitoring
chown munin:munin /var/www/example/web/monitoring
/etc/init.d/munin-node restart

すべての人がサーバーに関するすべての小さな統計を見られないようにするために、/var/www/example/web/monitoringディレクトリにパスワード保護を設定することをお勧めします。

これを行うには、/var/www/example/web/monitoringに.htaccessファイルを作成します:

vi /var/www/example/web/monitoring/.htaccess

AuthType Basic
AuthName "メンバーのみ"
AuthUserFile /var/www/example/monitoring/.htpasswd

require valid-user

次に、パスワードファイル/var/www/example/.htpasswdを作成する必要があります。ユーザー名adminでログインしたいので、次のようにします:

htpasswd -c /var/www/example/web/monitoring/.htpasswd admin

adminのパスワードを入力し、完了です!

これで、次のアドレスでレポートにアクセスできます(データを収集するのに数分かかります):

http://www.example.com:10001/monitoring(web1.example.com用)

および

http://www.example.com:20001/monitoring(web2.example.com用)。

13.2 monitのインストールと設定

monitをインストールするには、次のようにします:

apt-get install monit

次に、/etc/monit/monitrcを編集する必要があります。デフォルトの/etc/monit/monitrcには多くの例があり、http://www.tildeslash.com/monit/doc/examples.phpでさらに多くの設定例を見つけることができます。ただし、私の場合はproftpd、mysql、apache、postfixを監視したいので、ポート2812でmonitのWebインターフェースを有効にし、https Webインターフェースを希望し、ユーザー名adminとパスワードtestでWebインターフェースにログインし、monitがroot@localhostにメールアラートを送信するようにしたいので、私のファイルは次のようになります:

web1.example.comで

cp /etc/monit/monitrc /etc/monit/monitrc_orig
cat /dev/null > /etc/monit/monitrc
vi /etc/monit/monitrc

set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: [email protected] }
set alert [email protected]
set httpd port 2812 and
     SSL ENABLE
     PEMFILE  /var/certs/monit.pem
     allow admin:test
#check process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
#   start program = "/etc/init.d/vsftpd start"
#   stop program  = "/etc/init.d/vsftpd stop"
#   if failed host 192.168.1.104 port 21 protocol ftp 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/mysql start"
   stop program = "/etc/init.d/mysql 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/apache2.pid
   group www
   start program = "/etc/init.d/apache2 start"
   stop program  = "/etc/init.d/apache2 stop"
   if failed host 192.168.1.104 port 80  protocol http
      and request "/example/web/monit/token" then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% 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
check process named with pidfile /var/lib/named/var/run/bind/run/named.pid
   group bind
   start program = "/etc/init.d/bind9 start"
   stop  program = "/etc/init.d/bind9 stop"
   if failed port 53 then restart
   if 5 restarts within 5 cycles then timeout

web2.example.comで

cp /etc/monit/monitrc /etc/monit/monitrc_orig
cat /dev/null > /etc/monit/monitrc
vi /etc/monit/monitrc

set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: [email protected] }
set alert [email protected]
set httpd port 2812 and
     SSL ENABLE
     PEMFILE  /var/certs/monit.pem
     allow admin:test
#check process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
#   start program = "/etc/init.d/vsftpd start"
#   stop program  = "/etc/init.d/vsftpd stop"
#   if failed host 192.168.1.105 port 21 protocol ftp 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/mysql start"
   stop program = "/etc/init.d/mysql 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/apache2.pid
   group www
   start program = "/etc/init.d/apache2 start"
   stop program  = "/etc/init.d/apache2 stop"
   if failed host 192.168.1.105 port 80  protocol http
      and request "/example/web/monit/token" then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% 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
check process named with pidfile /var/lib/named/var/run/bind/run/named.pid
   group bind
   start program = "/etc/init.d/bind9 start"
   stop  program = "/etc/init.d/bind9 stop"
   if failed port 53 then restart
   if 5 restarts within 5 cycles then timeout

設定ファイルはかなり自己説明的です。オプションについて不明な点がある場合は、monitのドキュメントを参照してください:http://www.tildeslash.com/monit/doc/manual.php

monit設定のapache部分には次のような記述があります:

if failed host www.example.com port 80 protocol http
      and request "/example/web/monit/token" then restart

これは、monitがwww.example.comのポート80に接続し、/monit/tokenというファイルにアクセスしようとすることを意味します。これは、私たちのウェブサイトのドキュメントルートが/var/www/example/webであるため、/var/www/example/web/monit/tokenです。monitが成功しない場合、Apacheが実行されていないことを意味し、monitはそれを再起動します。

web1とweb2の両方で

次に、ファイル/var/www/example/web/monit/tokenを作成し、そこにランダムな文字列を書き込みます:

mkdir /var/www/example/web/monit
echo “hello” > /var/www/example/web/monit/token

次に、SSL暗号化されたmonit Webインターフェースに必要な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.pem

openssl gendh 512 >> /var/certs/monit.pem

openssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pem

chmod 700 /var/certs/monit.pem

その後、/etc/default/monitを編集してmonitデーモンを有効にします。startupを1に変更し、CHECK_INTERVALSをmonitがシステムをチェックする間隔(秒単位)に設定します。私は60(秒)を選択したので、私のファイルは次のようになります:

vi /etc/default/monit

# monitのinitスクリプトのデフォルト
# /etc/init.d/monitによってソースされる
# maintainer scriptsによって/etc/default/monitにインストールされる
# Fredrik Steen 
# monitを起動するにはこの変数を設定する必要があります
startup=1
# monitが実行する間隔を変更するには、この変数のコメントを外して変更します。
CHECK_INTERVALS=60

最後に、monitを起動できます:

/etc/init.d/monit start

ブラウザをhttps://192.168.1.104:2812/またはhttps://192.168.1.105:2812/に向け、adminとtestでログインすると、monitのWebインターフェースが表示されるはずです。

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。