网关告警通知
API 网关出现异常流量模式或错误,可能意味着发生了故障或恶意攻击。为特定阈值和活动设置告警,便可快速发现并了解可能表明安全事件、滥用或异常使用的模式。
本教程介绍如何创建告警策略,以便在特定事件发生时接收邮件和 Webhook 通知。下面的交互式演示展示了如何统计网关组中的健康网关实例数。
前提条件
- 安装 API7 企业版。
- 在网关组上运行 API。
- 获取通知系统的 Webhook URL。
设置 SMTP 服务器
- 从顶部导航栏中选择组织,然后选择设置。
- 点击SMTP 服务器选项卡。
- 点击启用。
- 在对话框中,执行以下操作:
- SMTP 服务器地址,输入你的 SMTP 服务器的地址。例如,
127.0.0.1。 - 用户名和密码,输入连接到你的 SMTP 服务器的凭据。
- 发件人姓名,输入
API7 Enterprise以在邮件中将此名称显示为发件人。 - 发件人邮箱地址,输入
noreply@api7.ai。这将用作实际的发件人地址。 - 点击启用。
- SMTP 服务器地址,输入你的 SMTP 服务器的地址。例如,
添加联系人
联系人 定义了一组可由多个告警策略复用的邮件地址或 Webhook URL。
添加邮件联系人
- 从顶部导航栏中选择组织,然后选择联系人。
- 点击新增联系人。
- 在对话框中,执行以下操作:
- 名称,输入
Emergency Team Email List。 - 类型,选择
邮件。 - 邮件地址,输入
emergencyteamlist@api7.ai。 - 点击新增。
- 名称,输入
添加 Webhook 联系人
使用 Slack 传入 Webhook 将来自 API7 企业版的消息发布到 Slack。
- 从顶部导航栏中选择组织,然后选择联系人。
- 点击新增联系人。
- 在对话框中,执行以下操作:
- 名称,输入
Slack Notification。 - 类型,选择
Webhook。 - URL,输入
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX。 - 点击新增。
- 名称,输入
添加告警策略
有关更多告警邮件内容示例,请参阅告警变量和模板。告警策略会在创建或更新后立即开始监控,首次检查会在指定的检查间隔后进行。
监控 SSL 证书到期
配置每日任务,检查 SSL 证书的到期日期。
若某张证书将在 30 天内到期,则向应急小组发送邮件告警,并在 Slack 中发布通知。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
SSL Certificate Expired。 -
告警等级,选择
高。 -
检查间隔,输入
1440分钟。 -
触发条件,执行以下操作:
- 条件类型,选择
满足下列所有条件 (AND)。 - 事件,选择
SSL 证书即将过期。 - 触发网关组,选择
全选。 - 规则,填空
SSL 证书将在 30 天内过期。
- 条件类型,选择
-
点击新增通知。
-
在对话框中,执行以下操作:
- 类型,选择
邮件。 - 联系人,选择
Emergency Team Email List。 - 告警邮件主题,输入
[API7 Alert] [{{.Severity}}]SSL Certificate Expiration Warning。 - 告警邮件内容,输入
Alert Time: {{.AlertTime.Format "2006 Jan 02 15:04:05"}}, Detail:{{.AlertDetail}}。 - 点击新增。
- 类型,选择
-
点击新增通知。
-
在对话框中,执行以下操作:
- 类型,选择
Webhook。 - 联系人,选择
Slack Notification。 - 告警消息,输入
"text": "{{.AlertDetail}}.".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设网关组 production group 中的一张 SSL 证书带有一张将于 2024-12-31 到期的控制面证书。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略:SSL Certificate Expired
- 告警等级:高
- 告警时间:5 分钟前
- 触发网关组:production group
- 告警详情:SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.
-
发送的邮件将类似于:
* Subject: [API7 Alert][High]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.
- 你将在 Slack 中收到一条消息:
SSL Certificate: sslcert123 in gateway group: production group will expire in 21 days.
监控控制面到数据面的 mTLS 证书到期
API7 控制面证书和 API7 控制面 CA 证书用于在控制面与数据面之间建立安全的 mTLS 通信。这些证书在部署网关实例时激活,有效期为 13 个月。
配置每日任务,检查网关实例上证书的到期日期。若某个网关实例的证书将在 30 天内到期,则向应急小组发送邮件告警,并在 Slack 中发布通知。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
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}}.".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设网关组 production group 中的网关实例 gateway 123 的控制面证书将于 2024-12-31 到期。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略:Control Plane CA Certificate Expired
- 告警等级:高
- 告警时间:5 分钟前
- 触发网关组:production group
- 告警详情:Certificate of gateway instance: gateway 123 will expire in 21 days.
-
发送的邮件将类似于:
* Subject: [API7 Alert] Gateway Instance Certificate Expiration Warning
* Alert Time: 2024 DEC 31 17:00:00, Detail: Certificate of gateway instance: gateway 123 will expire in 21 days.
- 你将在 Slack 中收到一条消息:
Certificate of gateway instance: gateway 123 will expire in 21 days.
检测网关实例离线
如果网关实例(数据面节点)超过 2 小时未向控制面报告心跳,并且此状态持续 7 天,数据面节点将被自动移除,并标记为 offline。
配置每小时运行一次的检查任务,出现问题时向应急小组发送邮件告警和 Slack 通知。收到告警后,应尝试恢复离线的网关实例。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
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}}".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设网关组 production group 中的网关实例 gateway 123 在 2024-12-31 14:00:00 离线,网关组 test group 中的网关实例 gateway 456 在 2024-12-31 13:00:00 离线。告警策略在 2024-12-31 17:00:00 触发。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略: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.
-
发送的邮件将类似于:
* 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.
- 你将在 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
检测 CPU 核数超出限制
如果所有网关组的 CPU 核数用量连续七天超过许可证允许的上限,新增或修改资源将受限。现有服务和路由会继续运行。
配置每小时告警任务以检测生产环境的所有网关组,并在出现问题时向应急小组发送邮件告警和 Slack 通知。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
CPU cores Exceeding Quota。 -
告警等级,选择
高。 -
检查间隔,输入
60分钟。 -
触发条件,执行以下操作:
- 条件类型,选择
满足下列所有条件 (AND)。 - 事件,选择
CPU 核心数超出 License 限制。
- 条件类型,选择
-
点击新增通知。
-
在对话框中,执行以下操作:
- 类型,选择
邮件。 - 联系人,选择
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}}".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设你的 API7 企业版许可证限制为 100 个 CPU 核,并且从 2024-12-1 开始已连续 21 天超出此限制。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略:CPU Cores Exceeding Quota
- 告警等级:中
- 告警时间:5 分钟前
- 触发网关组:production group
- 告警详情:Total CPU usage for all gateway groups is 110c, exceeded allowed license CPU quota 100c.
-
发送的邮件将类似于:
* 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.
- 你将在 Slack 中收到一条消息:
Total CPU usage for all gateway groups is 110c, exceeded allowed license CPU quota 100c at 2024 DEC 31 17:00:00.
统计网关组中的健康网关实例数
如果网关组中健康网关实例的数量低于临界阈值,则表明可能会出现服务中断并影响流量处理。 这种情况在 Kubernetes 部署中尤其重要,因为网关实例可能会遇到故障或意外缩减。
实施高频告警任务以检测并在出现问题时向应急小组发送邮件告警和 Slack 通知。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
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}}".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设网关组 Production Group 至少需要 50 个健康的网关实例。截至 2024-12-31 17:00:00,只有 40 个实例在运行,可能导致服务降级或中断,需要立即处理。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略:No Enough Healthy Gateway Instances in Production Group
- 告警等级:高
- 告警时间:5 分钟前
- 触发网关组:Production Group
- 告警详情:Number of healthy gateway instances in the gateway group: Production Group is 40.
-
发送的邮件将类似于:
* 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.
- 你将在 Slack 中收到一条消息:
Number of healthy gateway instances in the gateway group: Production Group is 40 at 2024 DEC 31 17:00:00.
监控状态码
如果特定 API 响应状态码的数量超过阈值(例如,过多的 500 错误),则表明可能会出现服务中断并影响流量处理。
实施高频告警任务以检测并在出现问题时向应急小组发送邮件告警和 Slack 通知。
-
从侧边栏选择告警,然后点击策略。
-
点击新增告警策略。
-
在对话框中,执行以下操作:
-
名称,输入
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}}".- 点击新增。
- 类型,选择
-
-
点击新增。
验证
假设网关组 VIP Group 带有标签 envType:production,在 2024-12-31 16:00 至 17:00 之间,1000 个请求中有 150 个返回 500,错误率为 15%。同一时段,带有标签 envType:production 的网关组 US Group 在 500 个请求中有 50 个返回 500,错误率为 10%。
-
从侧边栏选择告警,然后点击历史。
-
你应该会看到一个告警记录。点击详情:
- 告警策略: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.\n 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.\n 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.
-
发送的邮件将类似于:
* 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.
- 你将在 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.