如何在 Debian 11 上安装和配置 Monit
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.
完成后,您可以继续下一步。
启用监控 Web UI
Monit 还提供 Web UI 来管理和监控不同的服务。默认情况下,它是禁用的。因此,您需要启用它并设置管理员密码。您可以通过编辑 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 不为 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 UI
现在,打开 Web 浏览器并使用 URL http://your-server-ip:2812 访问 Monit Web 界面。您将被重定向到 Monit 登录页面:
提供您的管理员用户名、密码,然后单击登录按钮。您应该在以下屏幕上看到 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 浏览器监视它们。如果您有任何疑问,请随时问我。