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

触发网关告警

异常的流量模式或 API 网关使用中的错误可能预示着问题或恶意攻击。通过为特定阈值和活动设置告警,你可以快速检测并深入了解可能表明安全漏洞、滥用或异常使用的模式。

本教程将指导你创建告警策略,以便接收特定事件的电子邮件和 Webhook 通知。以下是一个交互式演示,提供了在网关组场景中统计健康网关实例的实践介绍。

前置条件

  1. 安装 API7 企业版
  2. 在网关组上运行 API
  3. 获取你的通知系统的 Webhook URL。

设置 SMTP 服务器

  1. 从顶部导航栏中选择 Organization(组织),然后选择 Settings(设置)。
  2. 点击 SMTP Server(SMTP 服务器)选项卡。
  3. 点击 Enable(启用)。
  4. 在对话框中,执行以下操作:
  • SMTP Server Address(SMTP 服务器地址)字段中,输入你的 SMTP 服务器地址。例如,127.0.0.1
  • Username(用户名)和 Password(密码)字段中,输入连接到你的 SMTP 服务器的凭据。
  • From Name(发件人名称)字段中,输入 API7 Enterprise,以在电子邮件中将此名称显示为发件人。
  • From Email Address(发件人电子邮件地址)字段中,输入 noreply@api7.ai。这将被用作实际的发件人地址。
  • 点击 Enable(启用)。

添加联系点

联系点(Contact Point)定义了一组可供多个告警策略使用的电子邮件地址或 Webhook URL。

添加电子邮件联系点

  1. 从顶部导航栏中选择 Organization(组织),然后选择 Contact Points(联系点)。
  2. 点击 Add Contact Point(添加联系点)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 Emergency Team Email List
  • Type(类型)字段中,选择 Email(电子邮件)。
  • Email Addresses(电子邮件地址)字段中,输入收件人的电子邮件地址,例如 emergencyteamlist@api7.ai
  • 点击 Add(添加)。

添加 Webhook 联系点

使用 Slack incoming webhook 将消息从 API7 企业版发送到 Slack。

  1. 从顶部导航栏中选择 Organization(组织),然后选择 Contact Points(联系点)。
  2. 点击 Add Contact Point(添加联系点)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 Slack Notification
  • Type(类型)字段中,选择 Webhook
  • URL 字段中,输入 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX。请替换为你自己的 ID。
  • 点击 Add(添加)。

添加基本告警策略

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

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

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

为了主动监控并对网关实例上即将过期的证书发出告警,请实施一项每日任务以检查证书的过期日期。如果网关实例的证书即将过期(在 30 天内),请向紧急响应团队发送电子邮件告警,并在 Slack 中发布通知。

  1. 从侧边导航栏中选择 Alert(告警),然后点击 Policies(策略)。
  2. 点击 Add Alert Policy(添加告警策略)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 Gateway Instance Certificate Expiration

  • Severity(严重程度)字段中,选择 High(高)。

  • Check Interval(检查间隔)字段中,输入 1440 分钟。

  • Conditions(条件)字段中,执行以下操作:

    • Operator(运算符)字段中,选择 Meet all of the following conditions(AND)(满足以下所有条件(AND))。
    • Event(事件)字段中,选择 mTLS certificate between control plane and data plane will expire(控制面和数据面之间的 mTLS 证书将过期)。
    • Trigger Gateway Group(触发网关组)字段中,选择 Select all(全选)。
    • Rule(规则)字段中,填写距离过期还有 30 天的规则。
  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入

      [API7 Alert] Gateway Instance Certificate Expiration Warning.
    • Alert Email Content(告警邮件内容)字段中,输入:

      Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入

      "text": "{{.AlertDetail}}.".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设一个控制面证书将在 2024-12-31 过期。在 2024-12-10 时,告警策略触发:

  1. 收到以下内容的电子邮件:
* Subject: [API7 Alert] Gateway Instance Certificate Expiration Warning
* Alert Time: 2024 DEC 10 17:00:00, Detail: The certificate for gateway instance: gateway 123 will expire in 21 days.
  1. Slack 中收到一条消息:
The certificate for gateway instance: gateway 123 will expire in 21 days.
  1. 从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)。
  2. 将显示与该事件对应的告警记录。点击 Detail(详情):
  • Alert Policy: Gateway Instance Certificate Expiration
  • Severity: High
  • Alert Time: 5 minutes ago
  • Trigger Gateway Group: production group
  • Alert Detail: The certificate for gateway instance: gateway 123 will expire in 21 days.

检测网关实例离线

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

实施一项每小时执行的任务,以检测网关实例的状态,在出现问题时向紧急响应团队发送电子邮件告警以及 Slack 通知。然后相关人员应该尝试恢复离线的网关实例。

  1. 从侧边导航栏中选择 Alert(告警),然后点击 Policies(策略)。
  2. 点击 Add Alert Policy(添加告警策略)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 Gateway Instance Offline
  • Severity(严重程度)字段中,选择 High(高)。
  • Check Interval(检查间隔)字段中,输入 60 分钟。
  • Conditions(条件)字段中,执行以下操作:
    • Operator(运算符)字段中,选择 Meet all of the following conditions(AND)(满足以下所有条件(AND))。
    • Event(事件)字段中,选择 Gateway instance offline(网关实例离线)。
    • Trigger Gateway Group(触发网关组)字段中,选择 Select all(全选)。
    • Rule(规则)字段中,填写为 Any gateway instance in the gateway group offline for more than 1 hour(网关组中任意网关实例离线超过 1 小时)。
  • 点击 Add Notification(添加通知)。
  • 在对话框中,执行以下操作:
    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入:

      [API7 Alert] Gateway Instance Offline Warning.
    • Alert Email Content(告警邮件内容)字段中,输入:

      Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。
  • 在对话框中,执行以下操作:
    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入:

       "text": "{{.AlertDetail}}".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设有两个网关实例分别在 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 has been offline for 3 hours.
Gateway instance: gateway 456 in the gateway group: test group has been offline for 4 hours.
  1. Slack 中收到一条消息:
Gateway instance: gateway 123 in the gateway group: production group has been offline for 3 hours. 
Gateway instance: gateway 456 in the gateway group: test group has been offline for 4 hours.
  1. 在告警历史记录中新增一条记录。从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)以查看该记录。
  2. 记录详情。点击进入记录 Detail(详情),你应该看到:
  • Alert Policy: Gateway Instance Offline
  • Severity: High
  • Alert Time: 5 minutes ago
  • Trigger Gateway Group: production group
  • Alert Detail: Gateway instance: gateway 123 in the gateway group: production group has been offline for 3 hours. Gateway instance: gateway 456 in the gateway group: test group has been offline for 4 hours.

检测 CPU 核心数超出配额

如果所有网关组的 CPU 核心使用量连续七天超过许可的 CPU 核心限制,则资源添加或修改将受到限制。但是,现有的服务和路由将继续运作。

实施一项每小时执行的任务,以检测生产环境的所有网关组,在出现问题时向紧急响应团队发送电子邮件告警以及 Slack 通知。

  1. 从侧边导航栏中选择 Alert(告警),然后点击 Policies(策略)。
  2. 点击 Add Alert Policy(添加告警策略)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 CPU Cores Exceeding Quota
  • Severity(严重程度)字段中,选择 High(高)。
  • Check Interval(检查间隔)字段中,输入 60 分钟。
  • Conditions(条件)字段中,执行以下操作:
    • Operator(运算符)字段中,选择 Meet all of the following conditions(AND)(满足以下所有条件(AND))。
    • Event(事件)字段中,选择 Allowed License CPU Quota Exceeded(超出允许的许可 CPU 配额)。
  • 点击 Add Notification(添加通知)。
  • 在对话框中,执行以下操作:
    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入:

      [API7 Alert] CPU Cores Exceeding Quota.
    • Alert Email Content(告警邮件内容)字段中,输入:

      Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。
  • 在对话框中,执行以下操作:
    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入

      "text": "{{.AlertDetail}}".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设你的 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 110c has exceeded the allowed license CPU quota 100c.
  1. Slack 中收到一条消息:
Total CPU usage 110c has exceeded the allowed license CPU quota 100c.
  1. 在告警历史记录中新增一条记录。从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)以查看该记录。
  2. 记录详情。点击进入记录 Detail(详情),你应该看到:
  • Alert Policy: CPU Cores Exceeding Quota
  • Severity: High
  • Alert Time: 5 minutes ago
  • Alert Detail: Total CPU usage 110c has exceeded the allowed license CPU quota 100c.

更多告警策略示例

监控 SSL 证书过期

为了主动监控并对即将过期的 SSL 证书发出告警,请实施一项每日任务以检查证书的过期日期。

如果证书即将过期(在 30 天内),请向紧急响应团队发送电子邮件告警,并在 Slack 中发布通知。

  1. 从侧边导航栏中选择 Alert(告警),然后点击 Policies(策略)。
  2. 点击 Add Alert Policy(添加告警策略)。
  3. 在对话框中,执行以下操作:
  • Name(名称)字段中,输入 SSL Certificate Expiration

  • Severity(严重程度)字段中,选择 Medium(中)。

  • Check Interval(检查间隔)字段中,输入 1440 分钟。

  • Conditions(条件)字段中,执行以下操作:

    • Operator(运算符)字段中,选择 Meet all of the following conditions(AND)(满足以下所有条件(AND))。
    • Event(事件)字段中,选择 SSL Certitificate will expire(SSL 证书将过期)。
    • Trigger Gateway Group(触发网关组)字段中,选择 Select all(全选)。
    • Rule(规则)字段中,填写为 SSL certificate will expire in 30 days(SSL 证书将在 30 天内过期)。
  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入:

      [API7 Alert] SSL Certificate Expiration Warning.
    • Alert Email Content(告警邮件内容)字段中,输入:

      Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入:

      "text": "{{.AlertDetail}}.".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设有一个 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 expires in 21 days.
  1. Slack 中收到一条消息:
SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.
  1. 在告警历史记录中新增一条记录。从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)以查看该记录。
  2. 记录详情。点击进入记录 Detail(详情),你应该看到:
  • Alert Policy: SSL Certificate Expiration
  • Severity: Medium
  • Alert Time: 5 minutes ago
  • Trigger Gateway Group: production group
  • Alert Detail: SSL Certificate: sslcert123 in gateway group: production group expires in 21 days.

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

如果网关组中健康的网关实例数量降至临界阈值以下,这表明可能发生服务中断,并影响流量处理。 此场景在 Kubernetes 部署中尤为相关,在此类部署中,网关实例可能会遇到故障或意外缩容。

实施一项高频任务,在出现问题时检测并向紧急响应团队发送电子邮件告警以及 Slack 通知。

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

  • Severity(严重程度)字段中,选择 Medium(中)。

  • Check Interval(检查间隔)字段中,输入 30 分钟。

  • Trigger Gateway Group(触发网关组)字段中,选择 Production Group

  • Conditions(条件)字段中,执行以下操作:

    • Operator(运算符)字段中,选择 Meet all of the following conditions(AND)(满足以下所有条件(AND))。
    • Event(事件)字段中,选择 Number of healthy gateway instances(健康网关实例数)。
    • Rule(规则)字段中,填写为 Number of gateway instances in the gateway group is less than 50(网关组中的网关实例数量少于 50)。
  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入:

      [API7 Alert] No Enough Healthy Gateway Instances in Production Group`.
      * In the **Alert Email Content** field, enter `Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入:

      "text": "{{.AlertDetail}}".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设你的网关组至少需要 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: The number of healthy gateway instances 40 in gateway group: Production Group is less than the minimum requirement of 50.
  1. Slack 中收到一条消息:
The number of healthy gateway instances 40 in gateway group: Production Group is less than the minimum requirement of 50.
  1. 在告警历史记录中新增一条记录。从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)以查看该记录。
  2. 记录详情。点击进入记录 Detail(详情),你应该看到:
  • Alert Policy: No Enough Healthy Gateway Instances in Production Group
  • Severity: Medium
  • Alert Time: 5 minutes ago
  • Trigger Gateway Group: Production Group
  • Alert Detail: The number of healthy gateway instances 40 in gateway group: Production Group is less than the minimum requirement of 50.

监控状态码

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

实施一项高频任务,在出现问题时检测并向紧急响应团队发送电子邮件告警以及 Slack 通知。

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

  • Severity(严重程度)字段中,选择 Medium(中)。

  • Check Interval(检查间隔)字段中,输入 30 分钟。

  • Trigger Gateway Group(触发网关组)字段中,选择 Match Label(匹配标签),然后输入键值对 envType: production

  • Conditions(条件)字段中,执行以下操作:

    • Operator(运算符)字段中,选择 Meet all of the following conditions(OR)(满足以下所有条件(OR))。
    • Event(事件)字段中,选择 Number of status code 500(500 状态码数量)。
    • Rule(规则)字段中,填写为 Number of requests with status code 500 received by all published services of any one of the gateway groups has reached or exceeded 100 times in the last 60 minutes(在过去 60 分钟内,任意一个网关组的所有已发布服务接收到的状态码为 500 的请求数量达到或超过 100 次)。
  • 点击 Add Condition(添加条件)。

  • Conditions(条件)字段中,执行以下操作:

    • Event(事件)字段中,选择 Ratio of status code 500(500 状态码比例)。
    • Rule(规则)字段中,填写为 Ratio of requests with status code 500 received by all published services of any one of the gateway groups has reached or exceeded 10% in the last 60 minutes(在过去 60 分钟内,任意一个网关组的所有已发布服务接收到的状态码为 500 的请求比例达到或超过 10%)。
  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Email(电子邮件)。

    • Contact Points(联系点)字段中,选择 Emergency Team Email List

    • Alert Email Subject(告警邮件主题)字段中,输入:

      [API7 Alert] Too many 500 status code in {{.TriggerGatewayGroup}}.
    • Alert Email Content(告警邮件内容)字段中,输入:

      Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}.
    • 点击 Add(添加)。

  • 点击 Add Notification(添加通知)。

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

    • Type(类型)字段中,选择 Webhook

    • Contact Points(联系点)字段中,选择 Slack Notification

    • Alert Message(告警消息)字段中,输入:

       "text": "{{.AlertDetail}}".
    • 点击 Add(添加)。

  1. 点击 Add(添加)。

验证

假设你的网关组 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: The number of 500 status code requests received by all published services in gateway group: VIP Group exceeded the threshold of 100 with a count of 150 in the last 60 minutes. Details: 100 requests for get-ip-route within httpbin-service, 40 requests for get-address-route within httpbin-service, and 10 unmatched requests.
500 status code request ratio for gateway group: VIP Group was 15% in the last 60 minutes (Total requests: 1000). Details: 100 requests (10%) for get-ip-route in httpbin-service, 40 requests (4%) for get-address-route in httpbin-service, and 10 (1%) for unmatched requests.
500 status code request ratio for gateway group: US Group was 10% in the last 60 minutes (Total requests: 500). Details: 100 requests (10%) for get-ip-route in httpbin-service.
  1. Slack 中收到一条消息:
The number of 500 status code requests received by all published services in gateway group: VIP Group exceeded the threshold of 100 with a count of 150 in the last 60 minutes. Details: 100 requests for get-ip-route within httpbin-service, 40 requests for get-address-route within httpbin-service, and 10 unmatched requests.
500 status code request ratio for gateway group: VIP Group was 15% in the last 60 minutes (Total requests: 1000). Details: 100 requests (10%) for get-ip-route in httpbin-service, 40 requests (4%) for get-address-route in httpbin-service, and 10 (1%) for unmatched requests.
500 status code request ratio for gateway group: US Group was 10% in the last 60 minutes (Total requests: 500). Details: 100 requests (10%) for get-ip-route in httpbin-service.
  1. 在告警历史记录中新增一条记录。从侧边导航栏中选择 Alert(告警),然后点击 History(历史记录)以查看该记录。
  2. 记录详情。点击进入记录 Detail(详情),你应该看到:
  • Alert Policy: Too many 500 status code in production gateway groups
  • Severity: Medium
  • Alert Time: 5 minutes ago
  • Trigger Gateway Group: VIP Group
  • Alert Detail: The number of 500 status code requests received by all published services in gateway group: VIP Group exceeded the threshold of 100 with a count of 150 in the last 60 minutes. Details: 100 requests for get-ip-route within httpbin-service, 40 requests for get-address-route within httpbin-service, and 10 unmatched requests. 500 status code request ratio for gateway group: VIP Group was 15% in the last 60 minutes (Total requests: 1000). Details: 100 requests (10%) for get-ip-route in httpbin-service, 40 requests (4%) for get-address-route in httpbin-service, and 10 (1%) for unmatched requests. 500 status code request ratio for gateway group: US Group was 10% in the last 60 minutes (Total requests: 500). Details: 100 requests (10%) for get-ip-route in httpbin-service.

附加资源