跳到主要内容
版本:3.6.x

触发网关告警

API 网关使用过程中的异常流量模式或错误可能表明存在问题或恶意攻击。为特定阈值和活动设置告警,可以快速发现并了解可能反映安全漏洞、滥用或异常使用的模式。

本教程介绍如何创建告警策略,为特定事件接收邮件和 Webhook 通知。下面的交互式演示介绍统计网关组中健康网关实例数的场景。

前提条件​

  1. 安装 API7 企业版。
  2. 网关组中有一个运行中的 API。
  3. 获取通知系统的 Webhook URL。

设置 SMTP 服务器​

  1. 从顶部导航栏选择组织,然后选择设置。
  2. 点击 SMTP 服务器选项卡。
  3. 点击启用。
  4. 在对话框中,执行以下操作:
  • 在 SMTP 服务器地址字段中输入 SMTP 服务器地址,例如 127.0.0.1。
  • 在用户名和密码字段中输入连接 SMTP 服务器的凭证。
  • 在发件人名称字段中输入 API7 Enterprise,将此名称显示为邮件发件人。
  • 在发件人邮箱地址字段中输入 noreply@api7.ai,将其用作实际发件地址。
  • 点击启用。

添加联系人​

_联系人_定义了一组邮箱地址或 Webhook URL,可供多个告警策略使用。

添加邮件联系人​

  1. 从顶部导航栏选择组织,然后选择联系人。
  2. 点击新增联系人。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 Emergency Team Email List。
  • 在类型字段中选择 邮件。
  • 在邮箱地址字段中输入收件人的邮箱地址,例如 emergencyteamlist@api7.ai。
  • 点击新增。

添加 Webhook 联系人​

使用 Slack Incoming Webhook,将 API7 企业版的消息发布到 Slack。

  1. 从顶部导航栏选择组织,然后选择联系人。
  2. 点击新增联系人。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 Slack Notification。
  • 在类型字段中选择 Webhook。
  • 在 URL 字段中输入 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX,并替换为你的 ID。
  • 点击新增。

添加基本告警策略​

强烈建议配置以下告警策略,它们对大多数用户都很重要。

监控控制面到数据面的 mTLS 证书到期​

API7 控制面证书和控制面 CA 证书用于保障控制面与数据面之间的 mTLS 通信安全,在部署网关实例时启用。这些证书的有效期为13个月。

配置每日任务检查证书到期日期,主动监控网关实例上即将到期的证书并发出告警。如果网关实例的证书将在30天内到期,向应急团队发送邮件告警,并在 Slack 中发布通知。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 Gateway Instance Certificate Expired。

  • 在告警等级字段中选择 高。

  • 在检查间隔字段中输入 1440 分钟。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(AND)。
    • 在事件字段中选择 控制面和数据面之间的 mTLS 证书即将到期。
    • 在触发网关组字段中选择 全选。
    • 在规则字段中,将内容填写为 数据面和控制面之间的 mTLS 证书将在30天内到期。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] Gateway Instance Certificate Expiration Warning。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}.".
    • 点击新增。
  1. 点击新增。

验证​

假设控制面证书将于2024-12-31到期。在2024-12-10,告警策略将触发以下通知:

  1. 一封内容如下的邮件:
* Subject: [API7 Alert] Gateway Instance Certificate Expiration Warning
* Alert Time: 2024 DEC 10 17:00:00, Detail: Certificate of gateway instance: gateway 123 expires in 21 days.
  1. 一条 Slack 消息:
Certificate of gateway instance: gateway 123 expires in 21 days.
  1. 从侧边导航栏选择告警,然后点击历史。
  2. 页面会显示该事件对应的告警记录。点击详情:
  • 告警策略:Gateway Instance Certificate Expired
  • 告警等级:高
  • 告警时间:5分钟前
  • 触发网关组:production group
  • 告警详情:Certificate of gateway instance: gateway 123 expires in 21 days.

检测网关实例离线​

如果网关实例(数据面节点)超过2小时未向控制面上报心跳,且此状态持续7天,数据面节点将被自动移除,并标记为 offline。

配置每小时运行的检测任务,在出现问题时向应急团队发送邮件告警和 Slack 通知,然后由相关人员尝试恢复离线的网关实例。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 Gateway Instance Offline。

  • 在告警等级字段中选择 高。

  • 在检查间隔字段中输入 60 分钟。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(AND)。
    • 在事件字段中选择 网关实例离线。
    • 在触发网关组字段中选择 全选。
    • 在规则字段中,将内容填写为 网关组中的任一网关实例离线超过1小时。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] Gateway Instance Offline Warning。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}".
    • 点击新增。
  1. 点击新增。

验证​

假设两个网关实例分别在2024-12-31 14:00:00和2024-12-31 13:00:00离线。 在2024-12-31 17:00:00,告警策略将触发以下通知:

  1. 一封内容如下的邮件:
* Subject: [API7 Alert] Gateway Instance Offline Warning
* Alert Time: 2024 DEC 31 17:00:00, Detail: Gateway instance: gateway 123 in the gateway group: production group offline for 3 hours.\ Gateway instance: gateway 456 in the gateway group: test group offline for 4 hours.
  1. 一条 Slack 消息:
Gateway instance: gateway 123 in the gateway group: production group offline for 3 hours
Gateway instance: gateway 456 in the gateway group: test group offline for 4 hours
  1. 一条告警历史记录。从侧边导航栏选择告警,然后点击历史查看该记录。
  2. 记录详情。点击记录的详情,可以看到:
  • 告警策略:Gateway Instance Offline
  • 告警等级:高
  • 告警时间:5分钟前
  • 触发网关组:production group
  • 告警详情:Gateway instance: gateway 123 in the gateway group: production group offline for 3 hours.\ Gateway instance: gateway 456 in the gateway group: test group offline for 4 hours.

检测 CPU 核数超出限额​

如果所有网关组的 CPU 核使用量连续7天超过许可证允许的 CPU 核数限额,新增或修改资源将受到限制,但现有服务和路由会继续运行。

配置每小时运行的任务,检测生产环境的所有网关组,并在出现问题时向应急团队发送邮件告警和 Slack 通知。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 CPU cores Exceeding Quota。

  • 在告警等级字段中选择 高。

  • 在检查间隔字段中输入 60 分钟。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(AND)。
    • 在事件字段中选择 超出许可证允许的 CPU 核数限额。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] CPU Cores Exceeding Quota。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}".
    • 点击新增。
  1. 点击新增。

验证​

假设 API7 企业版许可证的限额为100个 CPU 核。在2024-12-31 17:00:00,告警策略将触发以下通知:

  1. 一封内容如下的邮件:
* Subject: [API7 Alert] CPU Cores Exceeding Quota
* Alert Time: 2024 DEC 31 17:00:00, Detail: Total CPU usage for all gateway groups is 110c, exceeded allowed license CPU quota 100c.
  1. 一条 Slack 消息:
Total CPU usage for all gateway groups is 110c, exceeded allowed license CPU quota 100c.
  1. 一条告警历史记录。从侧边导航栏选择告警,然后点击历史查看该记录。
  2. 记录详情。点击记录的详情,可以看到:
  • 告警策略:CPU Cores Exceeding Quota
  • 告警等级:高
  • 告警时间:5分钟前
  • 告警详情:Total CPU usage for all gateway groups is 110c, exceeded allowed license CPU quota 100c.

更多告警策略示例​

监控 SSL 证书到期​

配置每日任务检查证书到期日期,主动监控即将到期的 SSL 证书并发出告警。

如果证书将在30天内到期,向应急团队发送邮件告警,并在 Slack 中发布通知。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 SSL Certificate Expired。

  • 在告警等级字段中选择 中。

  • 在检查间隔字段中输入 1440 分钟。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(AND)。
    • 在事件字段中选择 SSL 证书即将到期。
    • 在触发网关组字段中选择 全选。
    • 在规则字段中,将内容填写为 SSL 证书将在30天内到期。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] SSL Certificate Expiration Warning。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}.".
    • 点击新增。
  1. 点击新增。

验证​

假设 SSL 证书将于2024-12-31到期。在2024-12-10,告警策略将触发以下通知:

  1. 一封内容如下的邮件:
* Subject: [API7 Alert]SSL Certificate Expiration Warning
* Alert Time: 2024 DEC 10 17:00:00, Detail: SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.
  1. 一条 Slack 消息:
SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.
  1. 一条告警历史记录。从侧边导航栏选择告警,然后点击历史查看该记录。
  2. 记录详情。点击记录的详情,可以看到:
  • 告警策略:SSL Certificate Expired
  • 告警等级:中
  • 告警时间:5分钟前
  • 触发网关组:production group
  • 告警详情:SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.

统计网关组中的健康网关实例数​

如果网关组中的健康网关实例数低于临界阈值,可能表明存在服务中断或流量处理受到影响。 此场景尤其适用于 Kubernetes 部署,其中的网关实例可能发生故障或意外缩容。

配置高频检测任务,在出现问题时向应急团队发送邮件告警和 Slack 通知。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 No Enough Healthy Gateway Instances in Production Group。

  • 在告警等级字段中选择 中。

  • 在检查间隔字段中输入 30 分钟。

  • 在触发网关组字段中选择 Production Group。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(AND)。
    • 在事件字段中选择 健康网关实例数。
    • 在规则字段中,将内容填写为 网关组中的网关实例数少于50。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] No Enough Healthy Gateway Instances in Production Group。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}".
    • 点击新增。
  1. 点击新增。

验证​

假设网关组至少需要50个健康网关实例,但截至2024-12-31,只有40个实例正常运行。 这一明显缺口可能导致服务降级或中断,需要立即关注并解决。

  1. 一封内容如下的邮件:
* Subject: [API7 Alert] No Enough Healthy Gateway Instances in Production Group
* Alert Time: 2024 DEC 31 17:00:00, Detail: Number of healthy gateway instances in the gateway group: Production Group is 40.
  1. 一条 Slack 消息:
Number of healthy gateway instances in the gateway group: Production Group is 40.
  1. 一条告警历史记录。从侧边导航栏选择告警,然后点击历史查看该记录。
  2. 记录详情。点击记录的详情,可以看到:
  • 告警策略:No Enough Healthy Gateway Instances in Production Group
  • 告警等级:中
  • 告警时间:5分钟前
  • 触发网关组:Production Group
  • 告警详情:Number of healthy gateway instances in the gateway group: Production Group is 40.

监控状态码​

如果特定 API 响应状态码的数量超过阈值,例如出现过多500错误,可能表明存在服务中断或流量处理受到影响。

配置高频检测任务,在出现问题时向应急团队发送邮件告警和 Slack 通知。

  1. 从侧边导航栏选择告警,然后点击策略。
  2. 点击新增告警策略。
  3. 在对话框中,执行以下操作:
  • 在名称字段中输入 Too many 500 status code in production gateway groups。

  • 在告警等级字段中选择 中。

  • 在检查间隔字段中输入 30 分钟。

  • 在触发网关组字段中选择标签匹配,然后输入键值对 envType: production。

  • 在触发条件字段中,执行以下操作:

    • 在条件类型字段中选择 满足下列所有条件(OR)。
    • 在事件字段中选择 状态码500的数量。
    • 在规则字段中,将内容填写为 任一网关组内所有已发布服务在过去60分钟内收到的状态码为500的请求数达到或超过100次。
  • 点击新增条件。

  • 在触发条件字段中,执行以下操作:

    • 在事件字段中选择 状态码500的比例。
    • 在规则字段中,将内容填写为 任一网关组内所有已发布服务在过去60分钟内收到的请求中,状态码为500的请求比例达到或超过10%。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 邮件。
    • 在联系人字段中选择 Emergency Team Email List。
    • 在告警邮件主题字段中输入 [API7 Alert] Too many 500 status code in {{.TriggerGatewayGroup}}。
    • 在告警邮件内容字段中输入 Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。
    • 点击新增。
  • 点击新增通知。

  • 在对话框中,执行以下操作:

    • 在类型字段中选择 Webhook。
    • 在联系人字段中选择 Slack Notification。
    • 在告警消息字段中输入以下内容:
    "text": "{{.AlertDetail}}".
    • 点击新增。
  1. 点击新增。

验证​

假设网关组 VIP Group 带有标签 envType:production,在2024年12月31日16:00至17:00之间的错误率为15%;1000个请求中有150个返回500错误。 网关组 US Group 同样带有标签 envType:production,在2024年12月31日16:00至17:00之间的错误率为10%;500个请求中有50个返回500错误。

  1. 一封内容如下的邮件:
* Subject: [API7 Alert] [API7 Alert] Too many 500 status code in VIP Group,US Group
* Alert Time: 2024 DEC 31 17:00:00, Detail:Number of requests with status code 500 received by all published services of the gateway group: VIP Group is 150 times in the last 60 minutes.
Ratio of requests with status code 500 received by all published services of the gateway group: VIP Group is 15% in the last 60 minutes.
Ratio of requests with status code 500 received by all published services of the gateway group: US Group is 10% in the last 60 minutes.
  1. 一条 Slack 消息:
Number of requests with status code 500 received by all published services of the gateway group: VIP Group is 150 times in the last 60 minutes.
Ratio of requests with status code 500 received by all published services of the gateway group: VIP Group is 15% in the last 60 minutes.
Ratio of requests with status code 500 received by all published services of the gateway group: US Group is 10% in the last 60 minutes.
  1. 一条告警历史记录。从侧边导航栏选择告警,然后点击历史查看该记录。
  2. 记录详情。点击记录的详情,可以看到:
  • 告警策略:Too many 500 in production gateway groups
  • 告警等级:中
  • 告警时间:5分钟前
  • 触发网关组:VIP Group
  • 告警详情:Number of requests with status code 500 received by all published services of the gateway group: VIP Group is 150 times in the last 60 minutes. Ratio of requests with status code 500 received by all published services of the gateway group: VIP Group is 15% in the last 60 minutes. Ratio of requests with status code 500 received by all published services of the gateway group: US Group is 10% in the last 60 minutes.

相关阅读​