You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Monit中为同一服务配置不同失败周期的差异化告警收件人?

在Monit中按失败周期给不同用户发送告警的正确配置

你遇到的问题源于对Monit语法的误解:

  • 第一种配置的语法错误:then alert user@domain.com不符合Monit规则,alert指令不能直接跟在then后指定收件人;
  • 第二种配置的重复告警:当服务连续3次失败时,for 3 cycles的条件满足,同时普通failed条件也满足,导致两个告警同时触发。

以下是两种可行的解决方案:

方案1:利用Monit内置的Alert条件过滤

通过alert指令的only on和with cycles参数,绑定不同失败周期到指定收件人:

check host "Cluster0 Admin Panel" with address 192.168.5.80
  # 定义失败检测规则
  if failed port 4443 protocol https with timeout 10 seconds then alert
  if failed port 4443 protocol https with timeout 10 seconds for 3 cycles then alert

  # 首次失败(1个周期)时通知userB
  alert userB@mydomain.com only on { connection failed } with 1 cycle
  # 连续3次失败时通知userA
  alert userA@mydomain.com only on { connection failed } with 3 cycles

注意:该语法需Monit 5.28及以上版本支持,若版本较低,建议使用方案2。

方案2:通过Exec执行自定义邮件命令

直接调用系统邮件工具(如sendmail或mail),在满足对应失败周期时发送告警,兼容性更强:

check host "Cluster0 Admin Panel" with address 192.168.5.80
  # 首次失败时通知userB,仅触发一次
  if failed port 4443 protocol https with timeout 10 seconds for 1 cycle then
    exec "/usr/bin/sendmail -s '服务告警:首次检测失败' userB@mydomain.com <<EOF
Cluster0 Admin Panel (192.168.5.80:4443 HTTPS) 首次检测失败,请关注。
EOF"
    not every 10 cycles  # 避免重复发送,可根据需求调整周期数

  # 连续3次失败时通知userA
  if failed port 4443 protocol https with timeout 10 seconds for 3 cycles then
    exec "/usr/bin/sendmail -s '服务告警:连续3次失败' userA@mydomain.com <<EOF
Cluster0 Admin Panel (192.168.5.80:4443 HTTPS) 已连续3次检测失败,请及时处理。
EOF"

配置完成后,执行monit reload重新加载配置,即可按预期触发不同告警。

内容的提问来源于stack exchange,提问作者Mesut GÜLNAZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 06:55:36