サーバーモニタリング · 6 min read · Oct 15, 2025

Debian 10におけるMuninとMonitによるサーバーモニタリング

この記事では、Debian 10(Buster)サーバーをMuninとMonitで監視する方法について説明します。Muninは、サーバーのほぼすべての側面(負荷平均、メモリ使用量、CPU使用量、MySQLスループット、ネットワークトラフィックなど)に関する素晴らしいグラフを生成します。設定はほとんど必要ありません。一方、MonitはApache、MySQL、Postfixなどのサービスの可用性をチェックし、サービスが期待通りに動作していない場合は再起動などの適切なアクションを取ります。この2つを組み合わせることで、現在または差し迫った問題を示すグラフ(例:「すぐに大きなサーバーが必要です。負荷平均が急速に増加しています。」)と、監視対象サービスの可用性を確保するウォッチドッグが得られます。

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

このガイドはDebian 10(Buster)向けに書かれていますが、設定はUbuntuなどの他のディストリビューションにも適用できるはずです。

1 予備ノート

このチュートリアルのすべてのコマンドはrootユーザーとして実行されます。SSHでrootとしてサーバーにログインするか、ターミナルウィンドウを開いてください。root以外のユーザーでログインする場合は、次のコマンドを使用してrootユーザーになります。

su -

私たちのシステムのホスト名はserver1.example.comで、ドキュメントルート/var/www/www.example.com/webを持つウェブサイトwww.example.comがあります。

Muninをインストールする前に、システムが最新であることを確認してください。次のコマンドを実行します。

apt update  
apt upgrade

ApacheはMuninページを表示するために使用され、apache fcgidモジュールはMuninグラフのズーム機能に必要です。aptを使用してapacheとfcgidモジュールをインストールします。

apt install apache2 libcgi-fast-perl libapache2-mod-fcgid

apacheでfcgidモジュールを有効にします。

a2enmod fcgid

2 Muninのインストールと設定

DebianにMuninをインストールするには、次のようにします。

apt install munin munin-node munin-plugins-extra

サーバーがMySQLまたはMariaDBを実行している場合は、MySQLを監視するためにいくつかの追加のMuninプラグインを有効にします。

cd /etc/munin/plugins  
ln -s /usr/share/munin/plugins/mysql_ mysql_  
ln -s /usr/share/munin/plugins/mysql_bytes mysql_bytes  
ln -s /usr/share/munin/plugins/mysql_innodb mysql_innodb  
ln -s /usr/share/munin/plugins/mysql_isam_space_ mysql_isam_space_  
ln -s /usr/share/munin/plugins/mysql_queries mysql_queries  
ln -s /usr/share/munin/plugins/mysql_slowqueries mysql_slowqueries  
ln -s /usr/share/munin/plugins/mysql_threads mysql_threads

次に、Muninの設定ファイル/etc/munin/munin.confを編集する必要があります。dbdir、htmldir、logdir、rundir、tmpldirの行のコメントを外します(デフォルト値は問題ありません)。MuninがHTML出力でlocalhost.localdomainの代わりにserver1.example.comを使用するようにしたいので、シンプルホストツリーセクションでlocalhost.localdomainをserver1.example.comに置き換えます。コメントを外した後、変更されたファイルは次のようになります。

nano /etc/munin/munin.conf
# Muninのための例の設定ファイル、'make build'によって生成された

# 次の3つの変数はRRDデータベース、HTML出力、ログ、およびロック/pidファイルの場所を指定します。すべて
# munin-cronを実行しているユーザーが書き込み可能でなければなりません。すべて
# ここに表示されている値にデフォルト設定されています。
#
dbdir /var/lib/munin
htmldir /var/cache/munin/www
logdir /var/log/munin
rundir /var/run/munin

# HTMLテンプレートを探す場所
#
tmpldir /etc/munin/templates

# 静的wwwファイルを探す場所
#
#staticdir /etc/munin/static

# 一時的なcgiファイルはここにあります。cgiユーザー(通常はnobodyまたはhttpd)によって書き込み可能でなければなりません。
#
# cgitmpdir /var/lib/munin/cgi-tmp

# (正確に1つ)すべてのファイルを含めるディレクトリ。
includedir /etc/munin/munin-conf.d
[...]
# シンプルなホストツリー
[server1.example.com]
 address 127.0.0.1
 use_node_name yes
[...]

Apache 2.4のMunin設定ファイル/etc/munin/apache24.confを見つける必要があります。これは、MuninのHTML出力ディレクトリ/var/cache/munin/wwwに対してmuninというエイリアスを定義しており、これによりこのサーバー上のすべてのウェブサイトから相対パス/munin(例:http://www.example.com/munin)を使用してmuninにアクセスできます。

apacheのMunin設定はデフォルトでは有効になっていません。これはDebianWheezyからJessieに変更されました。新しい設定ディレクトリは/etc/apache2/conf-enabledであり、古いバージョン(WheezyやSqueezeなど)で使用されていた/etc/apache2/conf.dではありません。

次のコマンドを実行して、設定を有効にし、apacheにロードします。

cd /etc/apache2/conf-enabled/  
ln -s /etc/munin/apache24.conf munin.conf  
service apache2 restart

Require localの行をコメントアウトし、代わりにRequire all grantedとOptions FollowSymLinks SymLinksIfOwnerMatchを追加してください(そうしないと、localhostからのみMunin出力にアクセスできるようになります)。

nano /etc/munin/apache24.conf
Alias /munin /var/cache/munin/www

 # Require local
 Require all granted
 Options FollowSymLinks SymLinksIfOwnerMatch


ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph

 # Require local
 # Require all granted
 AuthUserFile /etc/munin/munin-htpasswd
 AuthName "Munin"
 AuthType Basic
 Require valid-user
 Options None

Apacheを再起動します。

systemctl restart apache2.service

次にMuninを再起動します。

systemctl restart munin-node.service

数分待ってMuninが最初の出力を生成できるようにし、その後ブラウザでhttp://www.example.com/munin/にアクセスすると、最初の統計が表示されます。

Muninプロセスモニター

(これはmuninが生成する多くのグラフィックの小さな抜粋です…)

3 Munin出力ディレクトリのパスワード保護(オプションですが強く推奨)

すべての人がサーバーに関するすべての小さな統計を見られるようにしたくない場合は、munin出力ディレクトリをパスワード保護することをお勧めします。

これを行うには、パスワードファイル/etc/munin/munin-htpasswdを作成する必要があります。ユーザー名adminでログインしたいので、次のようにします。

htpasswd -c /etc/munin/munin-htpasswd admin

adminのパスワードを入力します。次に、再度/etc/munin/apache24.confを開きます…

nano /etc/munin/apache24.conf

… “Require all granted”の行をコメントアウトし、赤でマークした行を追加します。

Alias /munin /var/cache/munin/www

 # Require local
 # Require all granted
 AuthUserFile /etc/munin/munin-htpasswd
 AuthName "Munin"
 AuthType Basic
 Require valid-user
 Options None


ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph

 # Require local
 # Require all granted
 AuthUserFile /etc/munin/munin-htpasswd
 AuthName "Munin"
 AuthType Basic
 Require valid-user
 
 SetHandler fcgid-script
 
 
 SetHandler cgi-script
 

次にApacheを再起動します。

systemctl restart apache2.service

4 Muninでの追加モジュールの有効化

Muninコマンド”munin-node-configure –suggest”を使用して、サーバーで有効にできる追加のMuninモジュールの推奨を取得できます。次のコマンドを実行します。

munin-node-configure --suggest

出力は次のようになります。

追加のMuninモジュールの提案

「used」列はモジュールが有効かどうかを示し、「Suggestions」列はサーバーがこのモジュールで監視できるサービスを実行しているかどうかを示します。モジュールを有効にするために、/etc/munin/pluginsにシンボリックリンクを作成します。

ここでは、apache_*モジュールを有効にします。

cd /etc/munin/plugins  
ln -s /usr/share/munin/plugins/apache_accesses  
ln -s /usr/share/munin/plugins/apache_processes  
ln -s /usr/share/munin/plugins/apache_volume

新しい設定を読み込むためにMuninを再起動します。

systemctl restart munin-node.service

5 Monitのインストールと設定

MonitはDebian Busterバックポートリポジトリから入手できます。このリポジトリはデフォルトでは有効になっていないため、まず追加する必要があります。次のコマンドを実行します。

echo 'deb http://ftp.de.debian.org/debian buster-backports main' > /etc/apt/sources.list.d/buster-backports.list

その後、次のコマンドでパッケージリストを更新します。

apt update

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

apt install monit

次に、/etc/monit/monitrcを編集する必要があります。デフォルトの/etc/monit/monitrcには多くの例があり、http://mmonit.com/monit/documentation/でさらに多くの設定例を見つけることができます。ただし、私の場合はproftpd、sshd、mysql、apache、postfixを監視したいと思っており、ポート2812でMonitのWebインターフェースを有効にしたいと思っています。Webインターフェースにadminというユーザー名とhowtoforgeというパスワードでログインしたいと思っており、Monitがroot@localhostにメールアラートを送信するようにしたいので、私のファイルは次のようになります(他のデーモンの例を設定に追加しているので、ファイルを必要に応じて調整できます)。

cp /etc/monit/monitrc /etc/monit/monitrc_orig  
cat /dev/null > /etc/monit/monitrc  
nano /etc/monit/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:howtoforge  
  
check process sshd with pidfile /var/run/sshd.pid  
 start program "/usr/sbin/service ssh start"  
 stop program "/usr/sbin/service ssh stop"  
 if failed port 22 protocol ssh then restart  
 if 5 restarts within 5 cycles then timeout  
  
check process apache with pidfile /var/run/apache2/apache2.pid  
 group www  
 start program = "/usr/sbin/service apache2 start"  
 stop program = "/usr/sbin/service apache2 stop"  
 if failed host localhost 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  
   
# ---------------------------------------------------------------------------------------------  
# 注意:example.pidをサーバーのpid名に置き換えてください。名前はホスト名によって異なります  
# ---------------------------------------------------------------------------------------------  
#check process mysql with pidfile /var/lib/mysql/example.pid  
# group database  
# start program = "/usr/sbin/service mysql start"  
# stop program = "/usr/sbin/service mysql stop"  
# if failed host 127.0.0.1 port 3306 then restart  
# if 5 restarts within 5 cycles then timeout  
  
#check process proftpd with pidfile /var/run/proftpd.pid  
# start program = "/usr/sbin/service proftpd start"  
# stop program = "/usr/sbin/service proftpd stop"  
# if failed port 21 protocol ftp then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process postfix with pidfile /var/spool/postfix/pid/master.pid  
# group mail  
# start program = "/usr/sbin/service postfix start"  
# stop program = "/usr/sbin/service postfix stop"  
# if failed port 25 protocol smtp then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process nginx with pidfile /var/run/nginx.pid  
# start program = "/usr/sbin/service nginx start"  
# stop program = "/usr/sbin/service nginx stop"  
# if failed host 127.0.0.1 port 80 then restart  
#  
#check process memcached with pidfile /var/run/memcached.pid  
# start program = "/usr/sbin/service memcached start"  
# stop program = "/usr/sbin/service memcached stop"  
# if failed host 127.0.0.1 port 11211 then restart  
#  
#check process pureftpd with pidfile /var/run/pure-ftpd/pure-ftpd.pid  
# start program = "/usr/sbin/service pure-ftpd-mysql start"  
# stop program = "/usr/sbin/service pure-ftpd-mysql stop"  
# if failed port 21 protocol ftp then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process named with pidfile /var/run/named/named.pid  
# start program = "/usr/sbin/service bind9 start"  
# stop program = "/usr/sbin/service bind9 stop"  
# if failed host 127.0.0.1 port 53 type tcp protocol dns then restart  
# if failed host 127.0.0.1 port 53 type udp protocol dns then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process ntpd with pidfile /var/run/ntpd.pid  
# start program = "/usr/sbin/service ntp start"  
# stop program = "/usr/sbin/service ntp stop"  
# if failed host 127.0.0.1 port 123 type udp then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process mailman with pidfile /var/run/mailman/mailman.pid  
# group mail  
# start program = "/usr/sbin/service mailman start"  
# stop program = "/usr/sbin/service mailman stop"  
#  
#check process amavisd with pidfile /var/run/amavis/amavisd.pid  
# group mail  
# start program = "/usr/sbin/service amavis start"  
# stop program = "/usr/sbin/service amavis stop"  
# if failed port 10024 protocol smtp then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process courier-imap with pidfile /var/run/courier/imapd.pid  
# group mail  
# start program = "/usr/sbin/service courier-imap start"  
# stop program = "/usr/sbin/service courier-imap stop"  
# if failed host localhost port 143 type tcp protocol imap then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process courier-imap-ssl with pidfile /var/run/courier/imapd-ssl.pid  
# group mail  
# start program = "/usr/sbin/service courier-imap-ssl start"  
# stop program = "/usr/sbin/service courier-imap-ssl stop"  
# if failed host localhost port 993 type tcpssl sslauto protocol imap then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process courier-pop3 with pidfile /var/run/courier/pop3d.pid  
# group mail  
# start program = "/usr/sbin/service courier-pop start"  
# stop program = "/usr/sbin/service courier-pop stop"  
# if failed host localhost port 110 type tcp protocol pop then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process courier-pop3-ssl with pidfile /var/run/courier/pop3d-ssl.pid  
# group mail  
# start program = "/usr/sbin/service courier-pop-ssl start"  
# stop program = "/usr/sbin/service courier-pop-ssl stop"  
# if failed host localhost port 995 type tcpssl sslauto protocol pop then restart  
# if 5 restarts within 5 cycles then timeout  
#  
#check process dovecot with pidfile /var/run/dovecot/master.pid  
# group mail  
# start program = "/usr/sbin/service dovecot start"  
# stop program = "/usr/sbin/service dovecot stop"  
# if failed host localhost port 993 type tcpssl sslauto protocol imap then restart  
# if 5 restarts within 5 cycles then timeout

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

Monit設定のapache部分には次のような内容があります。

   if failed host localhost port 80 protocol http
      and request "/monit/token" then restart

これは、Monitがlocalhostのポート80に接続し、/monit/tokenというファイルにアクセスしようとすることを意味します。これは/var/www/html/monit/tokenであり、私たちのウェブサイトのドキュメントルートは/var/www/htmlです。Monitが成功しない場合、Apacheが実行されていないことを意味し、Monitはそれを再起動します。次に、/var/www/html/monit/tokenというファイルを作成し、そこにランダムな文字列を書き込みます。

mkdir /var/www/html/monit  
echo "hello" > /var/www/html/monit/token

次に、SSL暗号化されたMonit Webインターフェースに必要なpem証明書(/var/certs/monit.pem)を作成します。

mkdir /var/certs  
cd /var/certs

証明書を作成するために必要なOpenSSL設定ファイルは次のようになります。

nano /var/certs/monit.cnf
# RSA証明書を作成 - サーバー

[ req ]
default_bits = 2048
encrypt_key = yes
distinguished_name = req_dn
x509_extensions = cert_type

[ req_dn ]
countryName = 国名(2文字コード)
countryName_default = MO

stateOrProvinceName             = 州または省名(フルネーム)
stateOrProvinceName_default     = Monitoria

localityName                    = 地名(例:都市)
localityName_default            = Monittown

organizationName                = 組織名(例:会社)
organizationName_default        = Monit Inc.

organizationalUnitName          = 組織単位名(例:セクション)
organizationalUnitName_default  = モニタリング技術部

commonName                      = コモンネーム(サーバーのFQDN)
commonName_default              = server.monit.mo

emailAddress                    = メールアドレス
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 dhparam 2048 >> /var/certs/monit.pem
openssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pem
chmod 600 /var/certs/monit.pem

最後に、Monitを起動できます。

service monit restart

ブラウザをhttps://www.example.com:2812/に向けてください(ポート2812がファイアウォールでブロックされていないことを確認してください)。adminとhowtoforgeでログインすると、MonitのWebインターフェースが表示されます。次のようになります。

Debian上のMonitサービスマネージャー

(メイン画面)

SSHプロセスの状態

Apacheプロセスの状態

(SSHdステータスページ)

/etc/monit/monitrcの設定に応じて、monitはサービスが失敗した場合にそれらを再起動し、サービスのプロセスIDが変更された場合に通知メールを送信します。

シェルでMonitのステータスを取得するには、”monit status”コマンドを実行します。

monit status

このコマンドは、監視されているすべてのサービスのステータスを表示します。

コマンドラインでのMonitステータス

6 リンク

Share: X/Twitter LinkedIn

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

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