本页内容
- 前置条件
- 在 Debian 11 上安装 Monit
- 启用 Monit Web 界面
- 为 Nginx 配置 Monit
- 为 ProFTPD 配置 Monit
- 访问 Monit Web 界面
- 通过命令行监控服务
- 总结
Monit 是一款免费的开源监控工具,用于在 Linux 系统上监控服务器进程和服务,例如 Apache、Nginx、Rsyslog、FTP 和 SSH。Monit 提供基于 Web 的界面以及命令行客户端来监控各项服务。它具备在进程未运行时自动启动该进程的能力。它还允许你监控远程主机的 TCP 端口、服务协议以及 ping 状态。
本教程将讲解如何在 Debian 11 上安装并使用 Monit 监控工具。
前置条件
- 一台运行 Debian 11 的服务器。
- 服务器上已配置 root 密码。
在 Debian 11 上安装 Monit
默认情况下,Monit 软件包已包含在 Debian 11 的默认仓库中。你可以通过运行以下命令来安装它:
apt-get install monit -y
安装完成后,你可以启动 Monit 服务,并通过以下命令将其设置为开机自启:
systemctl start monit
systemctl enable monit
你也可以通过以下命令检查 Monit 的状态:
systemctl status monit
你将看到如下输出:
? monit.service - LSB: service and resource monitoring daemon
Loaded: loaded (/etc/init.d/monit; generated)
Active: active (running) since Sat 2022-01-01 07:38:44 UTC; 10s ago
Docs: man:systemd-sysv-generator(8)
Process: 1005 ExecStart=/etc/init.d/monit start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 2341)
Memory: 1.7M
CPU: 20ms
CGroup: /system.slice/monit.service
??1010 /usr/bin/monit -c /etc/monit/monitrc
Jan 01 07:38:44 debian11 systemd[1]: Starting LSB: service and resource monitoring daemon...
Jan 01 07:38:44 debian11 monit[1005]: Starting daemon monitor: monit.
Jan 01 07:38:44 debian11 systemd[1]: Started LSB: service and resource monitoring daemon.
完成上述操作后,你可以进入下一步。
启用 Monit Web 界面
Monit 还提供了一个 Web 界面来管理和监控各项服务。默认情况下该功能是关闭的。因此你需要启用它并设置一个管理员密码。你可以通过编辑 Monit 配置文件来完成:
nano /etc/monit/monitrc
取消注释并修改以下行:
set httpd port 2812 and allow admin:monitpassword
保存并关闭文件,然后重启 Monit 服务以使更改生效:
systemctl restart monit
默认情况下,Monit 监听 2812 端口。你可以通过以下命令进行确认:
ss -antpl | grep :2812
你将看到如下输出:
LISTEN 0 1024 0.0.0.0:2812 0.0.0.0:* users:(("monit",pid=1322,fd=6))
LISTEN 0 1024 [::]:2812 [::]:* users:(("monit",pid=1322,fd=7))
为 Nginx 配置 Monit
Monit 允许你监控不同的服务。首先,使用以下命令检查你的操作系统状态:
monit status
你将看到如下输出:
Monit 5.27.2 uptime: 0m System 'debian11' status OK monitoring status Monitored monitoring mode active on reboot start load average [0.14] [0.06] [0.02] cpu 0.0%usr 0.0%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice memory usage 192.2 MB [9.7%] swap usage 0 B [0.0%] uptime 2m boot time Sat, 01 Jan 2022 07:37:55 filedescriptors 512 [0.0% of 9223372036854775807 limit] data collected Sat, 01 Jan 2022 07:39:34
默认情况下,Monit 提供了一个 Nginx 服务配置文件,但默认是禁用的。因此你需要启用它才能对其进行监控。你可以通过运行以下命令来启用:
ln -s /etc/monit/conf-available/nginx /etc/monit/conf-enabled/
接下来,重启 Monit 服务以使更改生效:
systemctl restart monit
为 ProFTPD 配置 Monit
Monit 并没有为 ProFTPD 提供默认的服务配置文件。因此你需要创建一个 ProFTPD 服务配置文件。你可以通过以下命令创建:
nano /etc/monit/conf-available/proftpd
添加以下内容:
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
保存并关闭文件,然后通过以下命令启用 ProFTPD 服务配置文件:
ln -s /etc/monit/conf-available/proftpd /etc/monit/conf-enabled
接下来,使用以下命令检查配置文件中的语法错误:
monit -t
如果一切正常,你将看到如下输出:
Control file syntax OK
最后,重启 Monit 以使更改生效。
systemctl restart monit
访问 Monit Web 界面
现在,打开你的 Web 浏览器,使用 URL http://your-server-ip:2812 访问 Monit Web 界面。你将被重定向到 Monit 登录页面:

输入你的管理员用户名和密码,然后点击“Sign In”按钮。你将在以下界面中看到 Monit 仪表盘:

点击 debian11 以查看你的操作系统状态。你将看到如下界面:

点击 Nginx 以查看 Nginx 服务的状态。你将看到如下界面:

通过命令行监控服务
你也可以使用命令行界面监控任意服务的状态。
要获取所有服务的摘要,请运行以下命令:
monit summary
你将看到如下输出:
Monit 5.27.2 uptime: 0m ???????????????????????????????????????????????????????????????????????????????? ? Service Name ? Status ? Type ? ???????????????????????????????????????????????????????????????????????????????? ? debian11 ? OK ? System ? ???????????????????????????????????????????????????????????????????????????????? ? proftpd ? OK ? Process ? ???????????????????????????????????????????????????????????????????????????????? ? nginx ? OK ? Process ? ???????????????????????????????????????????????????????????????????????????????? ? nginx_bin ? OK ? File ? ???????????????????????????????????????????????????????????????????????????????? ? nginx_rc ? OK ? File ? ????????????????????????????????????????????????????????????????????????????????
要查看所有服务的状态,请运行以下命令:
monit status
你将看到如下输出:
Monit 5.27.2 uptime: 0m Process 'proftpd' status OK monitoring status Monitored monitoring mode active on reboot start pid 2596 parent pid 1 uid 0 effective uid 107 gid 65534 uptime 16m threads 1 children 0 cpu - cpu total - memory 0.1% [2.9 MB] memory total 0.1% [2.9 MB] security attribute unconfined filedescriptors 5 [0.5% of 1024 limit] total filedescriptors 5 read bytes 0 B/s [32 B total] disk read bytes 0 B/s [0 B total] disk read operations 0.0 reads/s [34 reads total] write bytes 0 B/s [178 B total] disk write bytes 0 B/s [4 kB total] disk write operations 0.0 writes/s [3 writes total] port response time 2.495 ms to localhost:21 type TCP/IP protocol FTP data collected Sat, 01 Jan 2022 08:00:43 Process 'nginx' status OK monitoring status Monitored monitoring mode active on reboot start pid 2078 parent pid 1 uid 0 effective uid 0 gid 0 uptime 20m threads 1 children 1 cpu - cpu total - memory 0.6% [11.5 MB] memory total 0.8% [16.6 MB] security attribute unconfined filedescriptors 9 [0.9% of 1024 limit] total filedescriptors 19 read bytes 0 B/s [62.0 kB total] disk read bytes 0 B/s [3.4 MB total] disk read operations 0.0 reads/s [73 reads total] write bytes 0 B/s [81 B total] disk write bytes 0 B/s [4 kB total] disk write operations 0.0 writes/s [2 writes total] data collected Sat, 01 Jan 2022 08:00:43 File 'nginx_bin' status OK monitoring status Monitored monitoring mode active on reboot start permission 755 uid 0 gid 0 size 1.1 MB access timestamp Sat, 01 Jan 2022 07:39:44 change timestamp Sat, 01 Jan 2022 07:39:47 modify timestamp Sat, 29 May 2021 14:21:37 checksum 12a1c0be9962768c055158782d1d564a (MD5) data collected Sat, 01 Jan 2022 08:00:43 File 'nginx_rc' status OK monitoring status Monitored monitoring mode active on reboot start permission 755 uid 0 gid 0 size 4.5 kB access timestamp Sat, 01 Jan 2022 07:39:44 change timestamp Sat, 01 Jan 2022 07:39:47 modify timestamp Sat, 29 May 2021 14:21:37 checksum 290f6f12a12bc8e882bc5af46c1bfe7c (MD5) data collected Sat, 01 Jan 2022 08:00:43 System 'debian11' status OK monitoring status Monitored monitoring mode active on reboot start load average [0.00] [0.00] [0.00] cpu 0.0%usr 0.0%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice memory usage 199.5 MB [10.1%] swap usage 0 B [0.0%] uptime 23m boot time Sat, 01 Jan 2022 07:37:55 filedescriptors 640 [0.0% of 9223372036854775807 limit] data collected Sat, 01 Jan 2022 08:00:43
要查看 ProFTPD 服务的状态,请运行以下命令:
monit status proftpd
你将看到如下输出:
Monit 5.27.2 uptime: 0m Process 'proftpd' status OK monitoring status Monitored monitoring mode active on reboot start pid 2596 parent pid 1 uid 0 effective uid 107 gid 65534 uptime 16m threads 1 children 0 cpu - cpu total - memory 0.1% [2.9 MB] memory total 0.1% [2.9 MB] security attribute unconfined filedescriptors 5 [0.5% of 1024 limit] total filedescriptors 5 read bytes 0 B/s [32 B total] disk read bytes 0 B/s [0 B total] disk read operations 0.0 reads/s [34 reads total] write bytes 0 B/s [178 B total] disk write bytes 0 B/s [4 kB total] disk write operations 0.0 writes/s [3 writes total] port response time 2.495 ms to localhost:21 type TCP/IP protocol FTP data collected Sat, 01 Jan 2022 08:00:43
总结
恭喜!你已成功在 Debian 11 上安装了 Monit。现在你可以开始添加远程主机,并通过 Web 浏览器对它们进行监控。如果你有任何疑问,随时向我提问。