ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

如何配置 Gatus 将监控失败告警发送到 SMTP 邮件服务器

如何配置 Gatus 将监控失败告警发送到 SMTP 邮件服务器 如何配置 Gatus 将监控失败告警发送到 SMTP 邮件服务器【免费下载链接】gatusAutomated developer-oriented status page with alerting and incident support项目地址: https://gitcode.com/GitHub_Trending/ga/gatusGatus 的健康检查只负责在状态页上标红而 README.md 的 Alerting 部分提供了把告警推送到各渠道的能力其中email类型走标准 SMTP 发送。本文的目标很单一让某个 endpoint 连续检查失败后Gatus 自动向指定的收件人发送告警邮件并在告警解除后可选再发一封 resolved 邮件。你只需要一个可用的 SMTP 服务器地址、端口和凭据以及一份正在运行的 Gatus 配置文件不需要改动 Gatus 本身。准备条件Gatus 已经可以启动。配置文件的默认位置是config/config.yaml如果放在别处用环境变量GATUS_CONFIG_PATH指定路径。SMTP 服务器的连接信息主机例如smtp.gmail.com、端口例如587、发件人地址以及如服务器要求鉴权时SMTP 用户名和密码。要接收告警的收件人邮箱地址。在 alerting.email 中配置 SMTP 服务器alerting.email是全局配置所有type: email的告警都从这里取 SMTP 连接参数。参数含义如下摘自 README 的参数表参数说明默认值alerting.email.from发件邮箱必填alerting.email.usernameSMTP 鉴权用户名留空则使用from的值alerting.email.passwordSMTP 鉴权密码留空则不进行鉴权alerting.email.host邮件服务器主机必填alerting.email.port邮件服务器端口必填alerting.email.to收件人多个地址用逗号分隔必填alerting.email.default-alert该类型告警的默认触发参数见下文N/Aalerting.email.client.insecure是否跳过 TLS 证书校验falsealerting.email.overrides按 endpoint 分组的覆盖配置见下文[]README 给出的示例配置邮箱地址和主机替换成你自己的即可alerting: email: from: fromexample.com username: fromexample.com password: hunter2 host: mail.example.com port: 587 to: recipient1example.com,recipient2example.com client: insecure: false两点需要注意password留空时 Gatus 不做任何鉴权直接以普通 SMTP 客户端连接。如果password为空且你的服务器仍要求鉴权邮件是发不出去的。如果 SMTP 主机只支持内网证书或自签证书把client.insecure设为true跳过 TLS 校验。密码这类敏感值不要写死在配置里。Gatus 支持在配置文件中直接使用环境变量例如password: ${SMTP_PASSWORD}配置内容本身含$时用$$转义具体规则见 README 的 Use environment variables in config files 一节。在 endpoint 上挂 email 告警SMTP 服务器配好只是通道还要在每个需要邮件通知的 endpoint 下声明一条type: email的告警endpoints: - name: website url: https://twin.sh/health interval: 5m conditions: - [STATUS] 200 - [BODY].status UP - [RESPONSE_TIME] 300 alerts: - type: email description: healthcheck failed send-on-resolved: trueendpoint 级告警参数来自 README 的 Alerting 参数表参数说明默认值failure-threshold连续失败多少次后触发告警3success-threshold持续失败中连续成功多少次后判定为 resolved2send-on-resolved告警解除后是否再发一封通知falsedescription告警描述会包含在邮件内容里provider-override覆盖全局的 SMTP 配置{}endpoint 的conditions决定了什么叫失败任一条件不满足该次检查即为 unhealthy计入failure-threshold的连续失败计数。可选用 default-alert 避免逐个 endpoint 重复如果很多 endpoint 都发往同一批收件人、用同样的触发参数可以把触发参数放到alerting.email.default-alert下endpoint 里只写type: emailalerting: email: from: fromexample.com host: mail.example.com port: 587 to: recipient1example.com default-alert: description: health check failed send-on-resolved: true failure-threshold: 5 endpoints: - name: example url: https://example.org conditions: - [STATUS] 200 alerts: - type: emailREADME 明确提醒设置了 default-alert 后endpoint 的alerts里仍必须写明type否则不会生效。可选按分组覆盖收件人overrides可以为特定 endpoint 分组指定不同的收件人列表覆盖全局的toalerting: email: from: fromexample.com host: mail.example.com port: 587 to: recipient1example.com,recipient2example.com # 针对 core 分组的告警改发给另一批收件人 overrides: - group: core to: recipient3example.com,recipient4example.com应用配置并验证Gatus 在运行中会自动检测配置文件更新并热加载所以保存上面的配置后不需要重启容器。验证分两步确认配置被正确加载热加载后查看 Gatus 日志README 要求确保日志中不出现下面这条消息一旦出现说明新配置无效、旧配置仍在用若配置的是无效文件默认行为是应用直接退出除非设置了skip-invalid-config-update: trueThe configuration file was updated, but it is not valid. The old configuration will continue being used.触发一次真实失败并查收邮件把某个 endpoint 指向一个必然失败的地址或临时修改 conditions 让它不满足连续失败达到failure-threshold默认 3 次后收件人邮箱会收到邮件如果设置了send-on-resolved: true连续成功达到success-threshold后还会再收到一封。邮件内容由 alerting/provider/email/email.go 的buildMessageSubjectAndBody生成测试文件 alerting/provider/email/email_test.go 中固化了具体格式可作为对照示例以下为测试中的示例输出实际内容随 endpoint 名和失败次数变化endpoint-name: Alert triggered An alert for endpoint-name has been triggered due to having failed 3 time(s) in a row Alert description: description-1 Condition results: ❌ [CONNECTED] true ❌ [STATUS] 200resolved 邮件的标题是endpoint-name: Alert resolved正文说明在连续成功 N 次后告警解除。如果邮件没收到先回到第 1 步确认配置确实被加载了再核对host、port、password是否与 SMTP 服务器一致。限制与边界README 特别标注如果告警 provider 配置不完整或无效所有配置为该类型的告警都会被忽略而不是只影响单个 endpoint。同一alerts列表中可以对多个 provider 重复type: email并配合不同的failure-threshold实现分级通知这与其它 provider 的用法一致。配置目录挂载时注意如果只 bind 了单个配置文件而不是配置目录热更新可能检测不到变更README 指向了上游 issue #151。README 在邮件一节还留了一句提醒Some mail servers are painfully slow即个别邮件服务器响应很慢属已知现象。【免费下载链接】gatusAutomated developer-oriented status page with alerting and incident support项目地址: https://gitcode.com/GitHub_Trending/ga/gatus创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表