Monit 服务进程监控指南

Monit 服务进程监控指南

一、Monit 简介

Monit 是一款开源的系统和服务监控工具,能够实时监测服务器的各项指标,包括 CPU、内存、磁盘使用情况等,尤其擅长对服务进程进行监控。当被监控的进程出现异常(如进程停止、崩溃、占用资源过高)时,Monit 可以自动采取相应措施,如重启进程、发送警报通知管理员,有效保障服务器的稳定运行。

二、安装 Monit

(一)基于 OS x 系统

Homebrew 是 macOS 上最常用的包管理工具,可以方便地安装 Monit。

  1. 安装 Monit:
csharp 复制代码
sudo brew install monit

# 验证安装
monit -V 

// brew uninstall monit

(二)基于 CentOS 系统

  1. 安装 EPEL 仓库(Extra Packages for Enterprise Linux),Monit 软件包位于此仓库中:
arduino 复制代码
sudo yum install epel - release
  1. 安装 Monit:

    sudo yum install monit

  2. 基础命令

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-devopenssl-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 状态

  1. 查看 Monit 服务运行状态:
lua 复制代码
sudo  monit status
  1. 查看 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 将在系统启动时自动运行,并监控配置的所有服务(如自动启动崩溃的进程)。

相关推荐
间彧27 分钟前
Kubernetes的Pod与Docker Compose中的服务在概念上有何异同?
后端
间彧30 分钟前
从开发到生产,如何将Docker Compose项目平滑迁移到Kubernetes?
后端
间彧36 分钟前
如何结合CI/CD流水线自动选择正确的Docker Compose配置?
后端
间彧37 分钟前
在多环境(开发、测试、生产)下,如何管理不同的Docker Compose配置?
后端
间彧38 分钟前
如何为Docker Compose中的服务配置健康检查,确保服务真正可用?
后端
间彧43 分钟前
Docker Compose和Kubernetes在编排服务时有哪些核心区别?
后端
间彧1 小时前
如何在实际项目中集成Arthas Tunnel Server实现Kubernetes集群的远程诊断?
后端
brzhang1 小时前
读懂 MiniMax Agent 的设计逻辑,然后我复刻了一个MiniMax Agent
前端·后端·架构
草明2 小时前
Go 的 IO 多路复用
开发语言·后端·golang
蓝-萧2 小时前
Plugin ‘mysql_native_password‘ is not loaded`
java·后端