插件窝 干货文章 linux monit要怎么安装部署

linux monit要怎么安装部署

Monit sudo 监控 bash 439    来源:    2025-05-14

Linux Monit 安装部署指南

Monit 是一个开源的进程监控工具,可以监控和管理 Unix/Linux 系统上的进程、文件、目录、设备等。以下是详细的安装部署步骤:

一、安装 Monit

1. 基于 Debian/Ubuntu 系统

sudo apt update
sudo apt install monit

2. 基于 RHEL/CentOS 系统

对于 CentOS/RHEL 7/8:

sudo yum install epel-release
sudo yum install monit

或者

sudo dnf install epel-release
sudo dnf install monit

3. 基于源码安装

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

二、配置 Monit

主配置文件通常位于 /etc/monit/monitrc/etc/monitrc

1. 基本配置

编辑配置文件:

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界面用户名密码

2. 监控服务示例

/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

三、启动和测试 Monit

1. 启动 Monit 服务

sudo systemctl start monit
sudo systemctl enable monit

2. 检查 Monit 状态

sudo monit status

3. 测试配置文件

sudo monit -t

如果没有错误,会显示:Control file syntax OK

4. 重新加载配置

sudo monit reload

四、访问 Web 界面

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 监控系统。根据实际需求,您可以添加更多的监控项来监控系统上的各种服务和资源。