Monit 是一个开源的进程监控工具,可以监控和管理 Unix/Linux 系统上的进程、文件、目录、设备等。以下是详细的安装部署步骤:
sudo apt update
sudo apt install monit
对于 CentOS/RHEL 7/8:
sudo yum install epel-release
sudo yum install monit
或者
sudo dnf install epel-release
sudo dnf install monit
wget https://mmonit.com/monit/dist/monit-5.32.0.tar.gz
tar -xzvf monit-5.32.0.tar.gz
cd monit-5.32.0
./configure
make
sudo make install
主配置文件通常位于 /etc/monit/monitrc
或 /etc/monitrc
编辑配置文件:
sudo nano /etc/monit/monitrc
示例基本配置:
set daemon 30 # 检查间隔30秒
set logfile /var/log/monit.log
set mailserver smtp.example.com port 587
username "user@example.com" password "password"
using tlsv1
with timeout 30 seconds
set alert admin@example.com # 设置接收警报的邮箱
set httpd port 2812 and
use address 0.0.0.0 # 监听所有IP
allow 0.0.0.0/0 # 允许所有IP访问web界面
allow admin:monit # 设置web界面用户名密码
在 /etc/monit/conf.d/
目录下添加监控配置:
监控 Nginx 服务
sudo nano /etc/monit/conf.d/nginx
内容:
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed host 127.0.0.1 port 80 then restart
if cpu > 80% for 5 cycles then alert
if cpu > 90% for 5 cycles then restart
if 5 restarts within 5 cycles then timeout
监控 MySQL 服务
sudo nano /etc/monit/conf.d/mysql
内容:
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if cpu > 80% for 5 cycles then alert
if cpu > 90% for 5 cycles then restart
sudo systemctl start monit
sudo systemctl enable monit
sudo monit status
sudo monit -t
如果没有错误,会显示:Control file syntax OK
sudo monit reload
Monit 提供了一个 Web 界面(默认端口2812),可以通过浏览器访问:
http://your-server-ip:2812
使用配置文件中设置的用户名密码登录。
sudo monit start all
- 启动所有监控服务sudo monit stop all
- 停止所有监控服务sudo monit restart all
- 重启所有监控服务sudo monit summary
- 查看监控摘要sudo monit start nginx
- 启动特定监控项sudo monit stop nginx
- 停止特定监控项Monit 日志通常位于 /var/log/monit.log
,可以使用以下命令查看:
sudo tail -f /var/log/monit.log
通过以上步骤,您应该已经成功安装并配置了 Monit 监控系统。根据实际需求,您可以添加更多的监控项来监控系统上的各种服务和资源。