Monit 服务进程监控指南
一、Monit 简介
Monit 是一款开源的系统和服务监控工具,能够实时监测服务器的各项指标,包括 CPU、内存、磁盘使用情况等,尤其擅长对服务进程进行监控。当被监控的进程出现异常(如进程停止、崩溃、占用资源过高)时,Monit 可以自动采取相应措施,如重启进程、发送警报通知管理员,有效保障服务器的稳定运行。
二、安装 Monit
(一)基于 OS x 系统
Homebrew 是 macOS 上最常用的包管理工具,可以方便地安装 Monit。
- 安装 Monit:
 
            
            
              csharp
              
              
            
          
          sudo brew install monit
# 验证安装
monit -V 
// brew uninstall monit
        (二)基于 CentOS 系统
- 安装 EPEL 仓库(Extra Packages for Enterprise Linux),Monit 软件包位于此仓库中:
 
            
            
              arduino
              
              
            
          
          sudo yum install epel - release
        - 
安装 Monit:
sudo yum install monit
 - 
基础命令
 
            
            
              css
              
              
            
          
          sudo vim /etc/monitrc
 
# 语法检测
sudo monit -t
-I 选项的作用是让 Monit 以守护进程的方式在后台运行。
sudo monit -I
sudo monit status
sudo monit status all
# 启动 Monit 时使用 -d 选项来指定轮询时间,设置轮询时间为 60 秒
sudo monit -d 60 -I
lsof -i :8081
        方法 2:手动编译安装
下载 Monit 源码 :
访问 Monit 官方网站 下载最新版本的源码包(如 monit-5.32.0.tar.gz)
解压源码包:
            
            
              bash
              
              
            
          
          tar -xzf monit-5.32.0.tar.gz
cd monit-5.32.0
        编译和安装:
            
            
              go
              
              
            
          
          ./configure
make
sudo make install
        验证安装:
monit -V    
        可能的错误:
            
            
              vbnet
              
              
            
          
          ./configure checking for SSL support... enabled checking for SSL include 
directory... Not found Couldn't find your SSL header files. Use --with-ssl-incl-
dir option to fix this problem or disable the SSL support with --without-ssl
        在编译 Monit 时,如果遇到 SSL header files not found 的错误,说明系统缺少 OpenSSL 的开发头文件(openssl-dev 或 openssl-devel)。以下是解决此问题的步骤
验证openssl安装:
brew info openssl
        记下 OpenSSL 的安装路径,例如 /usr/local/opt/openssl。
配置 Monit 使用 OpenSSL
在编译 Monit 时,指定 OpenSSL 的头文件和库文件路径。
1. 使用 --with-ssl-incl-dir 和 --with-ssl-lib-dir
运行 ./configure 时,添加以下选项:
            
            
              javascript
              
              
            
          
          ./configure --with-ssl-incl-dir=/usr/local/opt/openssl/include \
           --with-ssl-lib-dir=/usr/local/opt/openssl/lib
        2. 禁用 SSL 支持(不推荐)
如果你不需要 SSL 支持,可以禁用 SSL
            
            
              bash
              
              
            
          
          ./configure --without-ssl
        三、配置 Monit
(一)主配置文件
Monit 的主配置文件通常位于/etc/monit/monitrc。打开该文件进行全局配置,如设置日志文件路径、监控周期等。
如果配置文件不存在,可以手动创建:
            
            
              bash
              
              
            
          
          sudo touch /etc/monit/monitrc
sudo chmod 600 /etc/monit/monitrc
        例如,修改日志文件路径为/var/log/monit.log:
            
            
              c
              
              
            
          
          set logfile /var/log/monit.log
        设置监控周期为每 120 秒检查一次服务状态:
            
            
              arduino
              
              
            
          
          set daemon 120
        (二)添加服务监控配置
在主配置文件末尾或单独创建一个配置文件(如/etc/monit/conf.d/目录下的文件)来定义要监控的服务。以监控 Nginx 服务为例:
            
            
              ini
              
              
            
          
          check process nginx with pidfile /var/run/nginx.pid
  start program = "/usr/sbin/service nginx start"
  stop program = "/usr/sbin/service nginx stop"
  if failed host 127.0.0.1 port 80 protocol http then restart
  if 5 restarts within 5 cycles then timeout
        上述配置中:
- check process nginx with pidfile /var/run/nginx.pid:定义监控名为nginx的进程,通过/var/run/nginx.pid文件来识别进程 ID。
 
- start program和stop program:指定启动和停止 Nginx 服务的命令。
 
- if failed host 127.0.0.1 port 80 protocol http then restart:如果通过 HTTP 协议访问本地127.0.0.1:80失败,则重启 Nginx 服务。
 
- if 5 restarts within 5 cycles then timeout:如果在 5 个监控周期内重启了 5 次,则视为超时,Monit 将不再尝试重启。
 
四、启动与管理 Monit
(一)启动 Monit 服务
            
            
              sql
              
              
            
          
          sudo monit start
        (二)设置开机自启
            
            
              csharp
              
              
            
          
          sudo chkconfig monit on
        (三)检查 Monit 状态
- 查看 Monit 服务运行状态:
 
            
            
              lua
              
              
            
          
          sudo  monit status
        - 查看 Monit 监控的服务状态:
 
            
            
              css
              
              
            
          
          monit summary
        五、监控结果与警报
(一)查看监控结果
通过monit summary命令可以获取当前监控的所有服务的简要状态信息,包括服务名称、状态(运行、停止、异常等)。也可以使用monit status命令获取更详细的监控信息,如进程资源使用情况等。
(二)设置警报通知
Monit 支持通过邮件发送警报通知。在主配置文件monitrc中进行如下配置:
            
            
              kotlin
              
              
            
          
          set mailserver smtp.example.com port 587 username "your_email@example.com" password "your_password"
set alert your_email@example.com
        上述配置中,设置了 SMTP 邮件服务器地址、端口、用户名和密码,并指定将警报发送到your_email@example.com邮箱。当被监控的服务出现异常并触发警报条件时,管理员将收到包含详细异常信息的邮件通知。
六、Monit设置为自启服务
以下是针对 Linux (systemd) 和 macOS (launchd) 的详细配置脚本,确保 Monit 开机自启并正确管理服务:
1. Linux (systemd) 配置
(1) 创建 systemd 服务文件
            
            
              bash
              
              
            
          
          sudo vim /etc/systemd/system/monit.service
        填入以下内容:
            
            
              ini
              
              
            
          
          [Unit]
Description=Monit - system monitoring tool
Documentation=man:monit(1)
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/monit -c /etc/monitrc
ExecStop=/usr/bin/monit -c /etc/monitrc quit
ExecReload=/usr/bin/monit -c /etc/monitrc reload
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
        (2) 启用并启动 Monit
            
            
              bash
              
              
            
          
          sudo systemctl daemon-reload          # 重新加载配置
sudo systemctl enable monit           # 开机自启
sudo systemctl start monit            # 立即启动
sudo systemctl status monit           # 检查状态
        (3) 验证日志
            
            
              bash
              
              
            
          
          journalctl -u monit -f               # 实时查看日志
        2. macOS (launchd) 配置
(1) 创建 launchd plist 文件
            
            
              bash
              
              
            
          
          sudo vim /Library/LaunchDaemons/com.monit.plist
        填入以下内容(根据实际路径调整):
            
            
              xml
              
              
            
          
          <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.monit</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/monit</string>
        <string>-c</string>
        <string>/usr/local/etc/monitrc</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/var/log/monit.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/monit.log</string>
</dict>
</plist>
        (2) 设置权限并加载
            
            
              bash
              
              
            
          
          sudo chmod 644 /Library/LaunchDaemons/com.monit.plist
sudo launchctl load /Library/LaunchDaemons/com.monit.plist
sudo launchctl start com.monit
        (3) 验证日志
            
            
              bash
              
              
            
          
          tail -f /var/log/monit.log
        3. Monit 配置文件示例
确保 /etc/monitrc 或 /usr/local/etc/monitrc 包含以下内容(以监控 Nginx 为例):
            
            
              bash
              
              
            
          
          # 全局设置
set logfile /var/log/monit.log
set pidfile /var/run/monit.pid
set statefile /var/run/monit.state
# 监控 Nginx
check process nginx with pidfile /var/run/nginx.pid
    start program = "/usr/bin/systemctl start nginx"
    stop program = "/usr/bin/systemctl stop nginx"
    if failed host 127.0.0.1 port 80 protocol http then restart
    if does not exist then start  # 关键!确保服务不存在时自动启动
        4. 关键点总结
| 系统 | 配置文件路径 | 自启命令 | 日志查看 | 
|---|---|---|---|
| Linux | /etc/systemd/system/monit.service | 
systemctl enable monit | 
journalctl -u monit | 
| macOS | /Library/LaunchDaemons/com.monit.plist | 
launchctl load com.monit | 
tail -f /var/log/monit.log | 
5. 常见问题解决
Q1: Monit 启动但服务未自动拉起?
- 检查配置中是否有 
if does not exist then start。 - 确保服务检查块的 
start program命令正确。 
Q2: launchd 报权限错误?
            
            
              bash
              
              
            
          
          sudo chown root:wheel /Library/LaunchDaemons/com.monit.plist
        Q3: systemd 无法启动 Monit?
检查日志:
            
            
              bash
              
              
            
          
          sudo journalctl -u monit --no-pager -n 50
        通过以上配置,Monit 将在系统启动时自动运行,并监控配置的所有服务(如自动启动崩溃的进程)。