本教程适用于以下操作系统版本
- Debian 10 (Buster)
- Debian 8 (Jessie)
- Debian 7 (Wheezy)
- Debian 6 (Squeeze)
- 0
- Debian 3.1 (Sarge)
本页目录
- 1 预备说明
- 2 安装和配置 Munin
- 3 为 Munin 输出目录设置密码保护(可选但强烈推荐)
- 4 在 Munin 中启用其他模块
- 5 安装和配置 Monit
- 6 链接
在本文中,我描述如何用 Munin 和 Monit 监控你的 Debian 10 (Buster) 服务器。Munin 无需太多配置就能生成关于服务器几乎所有方面的漂亮小图表(平均负载、内存使用、CPU 使用、MySQL 吞吐量、网络流量等),而 Monit 检查 Apache、MySQL、Postfix 等服务的可用性,如果发现某个服务未按预期运行,就采取重启等适当的行动。将两者结合可获得完整的监控:图表让你看到当前或即将出现的问题(例如"我们很快需要一台更大的服务器,我们的平均负载正在快速上升。"),以及一个确保被监控服务可用性的看门狗。
虽然你可以用 Munin 监控多台服务器,但这里我们只讨论监控其安装所在的系统。
本指南针对 Debian 10 (Buster) 编写,但该配置稍作修改也应适用于 Ubuntu 等其他发行版。
1 预备说明
本教程中的所有命令都以 root 用户运行。通过 SSH 以 root 身份登录你的服务器,或打开一个终端窗口。如果你以非 root 的其他用户登录,请使用命令
su -
在继续之前成为 root 用户。
我们系统的主机名是 server1.example.com ,其上有一个网站 www.example.com ,文档根目录为 /var/www/www.example.com/web 。
在开始安装 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 时,启用一些额外的 Munin 插件来监控 MySQL:
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 输出中使用名称 server1.example.com 而不是 localhost.localdomain ,因此我们在简单主机树部分将 localhost.localdomain 替换为 server1.example.com 。去掉注释后,修改后的文件如下所示:
nano /etc/munin/munin.conf
# Example configuration file for Munin, generated by 'make build' # The next three variables specifies where the location of the RRD # databases, the HTML output, logs and the lock/pid files. They all # must be writable by the user running munin-cron. They are all # defaulted to the values you see here. # dbdir /var/lib/munin htmldir /var/cache/munin/www logdir /var/log/munin rundir /var/run/munin # Where to look for the HTML templates # tmpldir /etc/munin/templates # Where to look for the static www files # #staticdir /etc/munin/static # temporary cgi files are here. note that it has to be writable by # the cgi user (usually nobody or httpd). # # cgitmpdir /var/lib/munin/cgi-tmp # (Exactly one) directory to include all files from. includedir /etc/munin/munin-conf.d [...] # a simple host tree [server1.example.com] address 127.0.0.1 use_node_name yes [...]
我们应找到 Munin 的 Apache 2.4 配置文件 /etc/munin/apache24.conf - 它定义了一个名为 munin 的别名,指向 Munin 的 HTML 输出目录 /var/cache/munin/www ,这意味着我们可以通过相对路径 /munin 从此服务器上的所有网站访问 munin(例如 http://www.example.com/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 <Directory /var/cache/munin/www> # Require local Require all granted Options FollowSymLinks SymLinksIfOwnerMatch </Directory> ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph <Location /munin-cgi/munin-cgi-graph> # Require local Require all granted Options FollowSymLinks SymLinksIfOwnerMatch <IfModule mod_fcgid.c> SetHandler fcgid-script </IfModule> <IfModule !mod_fcgid.c> SetHandler cgi-script </IfModule> </Location>
重启 Apache:
systemctl restart apache2.service
然后重启 Munin:
systemctl restart munin-node.service
现在等待几分钟,让 Munin 生成其首次输出,然后在浏览器中访问 http://www.example.com/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 <Directory /var/cache/munin/www> # Require local # Require all granted AuthUserFile /etc/munin/munin-htpasswd AuthName "Munin" AuthType Basic Require valid-user Options None </Directory> ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph <Location /munin-cgi/munin-cgi-graph> # Require local # Require all granted AuthUserFile /etc/munin/munin-htpasswd AuthName "Munin" AuthType Basic Require valid-user <IfModule mod_fcgid.c> SetHandler fcgid-script </IfModule> <IfModule !mod_fcgid.c> SetHandler cgi-script </IfModule> </Location>
然后重启 Apache:
systemctl restart apache2.service
4 在 Munin 中启用其他模块
Munin 命令 "munin-node-configure --suggest" 可用于获取可在服务器上启用的其他 Munin 模块的建议。运行:
munin-node-configure --suggest
输出应类似于此:

"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 backports 仓库获取。该仓库默认未启用,因此我们必须先添加它。运行此命令:
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 界面,我想用一个 https web 界面,想用用户名 admin 和密码 example 登录 web 界面,并且希望 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:example
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
# ---------------------------------------------------------------------------------------------
# NOTE: Replace example.pid with the pid name of your server, the name depends on the hostname
# ---------------------------------------------------------------------------------------------
#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 尝试通过 80 端口连接 localhost ,并尝试访问文件 /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
# create RSA certs - Server [ req ] default_bits = 2048 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 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 和 example 登录,你应该会看到 Monit web 界面。它应该看起来像这样:

(主界面)


(SSHd 状态页面)
根据你在 /etc/monit/monitrc 中的配置,monit 将在服务失败时重启它们,并在服务的进程 ID 发生变化等情况下发送通知电子邮件。
要在 shell 上获取 Monit 状态,运行 "monit status" 命令:
monit status
该命令将显示所有被监控服务的状态。

6 链接
- munin: http://munin-monitoring.org/
- monit: http://mmonit.com/monit/