大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
Monit
这一课你将能做到
- 安装配置 Monit 监控进程
- 设置告警规则(CPU、内存、磁盘)
- 邮件/Slack 告警通知
为什么要学
应用上线后可能崩溃、内存泄漏、磁盘满了。Monit 是轻量级进程监控工具,自动检测问题并重启服务。
人话:Monit 是「值班保安」,24 小时盯着你的服务,出问题了自动处理。
跟着做
步骤 1:安装
# Ubuntu
sudo apt install monit
# macOS
brew install monit
步骤 2:配置监控
sudo vim /etc/monit/monitrc
# 检查周期(每 30 秒)
set daemon 30
# Web 界面
set httpd port 2812
allow admin:monit
# 邮件告警
set mailserver smtp.gmail.com port 587
username "your-email@gmail.com" password "your-password"
using tlsv1
set alert your-email@gmail.com
# 监控 Node.js 应用
check process todo-api with pidfile /var/run/todo-api.pid
start program = "/usr/bin/pm2 start /var/www/todo/server/app.js --name todo-api"
stop program = "/usr/bin/pm2 stop todo-api"
if cpu > 80% for 2 cycles then alert
if memory > 500 MB for 5 cycles then restart
if failed port 3000 protocol http
request /health
with timeout 10 seconds
then restart
# 监控系统资源
check system $HOST
if cpu usage > 90% for 5 cycles then alert
if memory usage > 80% for 5 cycles then alert
if swap usage > 50% for 5 cycles then alert
# 监控磁盘
check filesystem root with path /
if space usage > 80% then alert
步骤 3:启动
sudo systemctl enable monit
sudo systemctl start monit
# 查看状态
sudo monit status
sudo monit summary
# 手动控制
sudo monit start todo-api
sudo monit stop todo-api
sudo monit restart todo-api
步骤 4:Web 界面
# 访问 http://服务器IP:2812
# 用户名: admin, 密码: monit
面试常问
-
Monit 和 PM2 区别? PM2 是 Node.js 进程管理器;Monit 是通用系统监控工具,可监控任何进程。
-
Monit 和 Prometheus 区别? Monit 轻量、配置简单;Prometheus 功能强大,适合大规模监控。
自测三问
- Monit 的检查周期怎么配置?
- 如何监控 HTTP 端口?
- 磁盘满 80% 怎么告警?
小结
Monit 轻量易用,监控进程、系统资源、磁盘,自动重启和告警。