安装
基于 CentOS 系统
- 安装 EPEL 仓库(Extra Packages for Enterprise Linux),Monit 软件包位于此仓库中:
sudo yum install epel - release
2. 安装 Monit:
sudo yum install monit
3. 基础命令
# 查看版本
sudo monit -V
# 语法检测
sudo monit -t
# -I 选项的作用是让 Monit 以守护进程的方式在后台运行。
sudo monit -I
# 启动monit
sudo monit start all
# 查看状态
sudo monit status
sudo monit status all
# 重新加载监视器
sudo monit reload
# 启动 Monit 时使用 -d 选项来指定轮询时间,设置轮询时间为 60 秒
sudo monit -d 60 -I
# 手动启动
sudo monit restart my-service
# 查看所有服务
sudo monit summary
sudo lsof -i :8081
# 查看monit日志
tail -f /var/log/monit.log
配置
配置 Monit
(一)主配置文件
Monit 的主配置文件通常位于/etc/monitrc。打开该文件进行全局配置,如设置日志文件路径、监控周期等。
如果配置文件不存在,可以手动创建:
sudo touch /etc/monitrc
sudo chmod 600 /etc/monitrc
例如,修改日志文件路径为/var/log/monit.log:
set logfile /var/log/monit.log
设置监控周期为每 120 秒检查一次服务状态:
set daemon 120
(二)添加服务监控配置
在主配置文件末尾或单独创建一个配置文件(如/etc/monit/conf.d/目录下的文件)来定义要监控的服务。以监控 Nginx 服务为例:
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 summary命令可以获取当前监控的所有服务的简要状态信息,包括服务名称、状态(运行、停止、异常等)。也可以使用monit status命令获取更详细的监控信息,如进程资源使用情况等。
(二)设置警报通知
Monit 支持通过邮件发送警报通知。在主配置文件monitrc中进行如下配置:
# 设置 Monit 的邮件通知
set mailserver smtp.qq.com port 587
username "1498851306@qq.com" password "xxxpbdicksqphb3kanjaic"
using tls
with timeout 30 seconds
注意:password 是各类邮箱授权码,需要自己去相关的邮箱服务器生成。在第三方客户端登录时,密码框请输入授权码。
上述配置中,设置了 SMTP 邮件服务器地址、端口、用户名和密码,并指定将警报发送到your_email@example.com邮箱。当被监控的服务出现异常并触发警报条件时,管理员将收到包含详细异常信息的邮件通知。
完整配置
# 设置 Monit 的日志文件
set logfile /var/log/monit.log
# 设置 Monit 的 PID 文件
set pidfile /var/run/monit.pid
# 设置 Monit 的状态文件
set statefile /var/run/monit.state
#设置轮询时间
set daemon 20
set httpd port 2812 and
# 设置 Monit 的邮件通知
set mailserver smtp.qq.com port 587
username "1498851306@qq.com" password "pbdiwckfqhbkanjaic"
using tls
with timeout 30 seconds
set alert 1498851306@qq.com
# 设置邮件格式,确保 from 字段和 username 一致
set mail-format {
from: 1498851306@qq.com
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
Yours sincerely,
monit
}
# 监控Java进程
check process cdyz-admin with pidfile /var/run/cdyz-one-admin.pid
# 关联文件检查(关键!)
depends on cdyz_ready
stop program = "/home/bid/bidding-cdyz/one.sh stop"
#start delay 60 # 延迟60秒后开始检查服务状态
if does not exist then exec "/usr/bin/logger -t monit 'cdyz_ready missing' && /bin/echo 'Alert: cdyz_ready missing' >&2"
group java
# 监控Java进程
check process cdyz-admin with pidfile /var/run/cdyz-one-admin.pid
# 关联文件检查(关键!)
depends on cdyz_ready
start program = "/home/bid/bidding-cdyz/one.sh maven" with timeout 300 seconds
stop program = "/home/bid/bidding-cdyz/one.sh stop"
#start delay 60 # 延迟60秒后开始检查服务状态
if failed host cdyz.51zhaocai.cn port 9530 protocol http
request "/login.html"
with timeout 5 seconds
then restart
if 5 restarts within 5 cycles then timeout
if does not exist then restart
group java
msmtp 测试邮箱
// 1. 安装
sudo yum install msmtp # CentOS/RHEL
# 2. 配置 /etc/msmtprc
# vim /etc/msmtprc
account default
host smtp.qq.com
port 587
from 1498851306@qq.com
auth on
user 1498851306@qq.com
password pbdiwckfqhbkanjaic
tls on
tls_starttls on
# tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
# 3. 测试
swaks --to 1498851306@qq.com --from 1498851306@qq.com \
--server smtp.qq.com:587 \
--auth LOGIN \
--auth-user 1498851306@qq.com \
--auth-password dgemeqijofjabaga \
--tls \
--helo qq.com
# 4. 结果 (证明我们的配置没有问题)
... ...
... ...
~> Date: Thu, 27 Mar 2025 11:57:43 +0800
~> To: 1498851306@qq.com
~> From: 1498851306@qq.com
~> Subject: test Thu, 27 Mar 2025 11:57:43 +0800
~> Message-Id: <20250327115743.2762346@>
~> X-Mailer: swaks v20240103.0 jetmore.org/john/code/swaks/
~>
~> This is a test mailing
~>
~>
~> .
<~ 250 OK: queued as.
~> QUIT
<~*
<~* 221 Bye.
*** Remote host closed connection unexpectedly