Monit 是一款强大的 Linux 进程、文件、目录和系统资源监控工具,支持多种报警通知方式。以下是 Monit 的主要报警方式及配置方法:
通过 SMTP 发送邮件通知,需配置 Monit 的邮件服务参数:
# 在 /etc/monitrc 或 /etc/monit.d/mailserver 中配置
set mailserver smtp.example.com # SMTP服务器地址
port 587 # 端口(可选)
username "user@example.com" # 认证用户名(可选)
password "password" # 认证密码(可选)
using tlsv1 # 加密方式(可选)
set mail-format {
from: monit@example.com
subject: "[Monit Alert] $EVENT: $SERVICE"
message: "Host: $HOST\nEvent: $EVENT\nService: $SERVICE\nDate: $DATE\nAction: $ACTION\nDescription: $DESCRIPTION"
}
set alert admin@example.com # 接收报警的邮箱
通过调用外部脚本(如 Shell/Python)发送通知,支持灵活集成(如短信、钉钉、Slack等):
# 在 monitrc 中配置
set alert sysadmin@example.com but exec "/path/to/alert_script.sh"
脚本示例 (alert_script.sh
):
#!/bin/bash
# 接收Monit传递的环境变量
echo "Monit Alert: $MONIT_SERVICE - $MONIT_DESCRIPTION" | send_sms.py
通过 HTTP POST
请求将报警发送到 Webhook(如企业微信、Prometheus Alertmanager等):
check process nginx with pidfile /var/run/nginx.pid
if failed port 80 then alert
alert http://webhook.example.com/api/alert with {
method="POST",
header="Content-Type: application/json",
content='{"service": "$SERVICE", "event": "$EVENT", "host": "$HOST"}'
}
将报警写入本地日志文件(需配合 syslog
或日志工具分析):
set logfile /var/log/monit.log
set alert sysadmin@example.com not on { instance, action }
通过 but not on
过滤特定事件避免冗余报警:
set alert admin@example.com but not on { pid, ppid } # 忽略PID变化报警
支持为不同事件设置不同的接收人:
set alert dev-team@example.com on { timeout, resource }
set alert ops-team@example.com on { checksum, permission }
exec
触发 Zabbix/Nagios 的告警机制。/etc/monitrc
或 /etc/monit/conf.d/
下的配置文件。monit -t
systemctl restart monit
set daemon 60
控制检查频率(单位:秒)。通过以上方式,Monit 可以灵活适配多种运维场景的报警需求。