britishchip/incident-bot
GitHub: britishchip/incident-bot
一个轻量级的 Flask Webhook 服务,用于接收 Grafana 告警、自动分级严重性并向 Slack 发送带颜色标识的事件通知。
Stars: 0 | Forks: 0
# incident-bot
一个用于监听 Grafana 告警的 webhook 接收器,能够分配严重性级别,将其记录为结构化事件,并向 Slack 发送带有颜色标识的通知。
旨在与 [sre-observability](https://github.com/britishchip/sre-observability) 配合使用。当 Grafana 告警触发时,它会请求此 bot 而不是通用的 webhook。
```
Grafana alert --> /webhook --> severity classification --> Slack #incidents
|
/incidents (audit log)
```
## 工作原理
当告警触发时,Grafana 会向 `/webhook` 发送 POST 请求。该 bot 会读取告警的 payload,根据告警名称分配严重性级别,生成唯一的事件 ID,并向 Slack 频道发送带有颜色标识的消息。每个事件也会存储在内存中,并可通过 `/incidents` 访问。
严重性由告警名称中的关键字决定:
| 级别 | 颜色 | 触发条件 |
|-------|--------|---------|
| P1 | 红色 | 告警名称包含 "critical" |
| P2 | 橙色 | 告警名称包含 "error" 或 "slo" |
| P3 | 绿色 | 其他所有情况 |
## 端点
| 端点 | 方法 | 描述 |
|----------|--------|-------------|
| `/webhook` | POST | 接收 Grafana 告警 payload |
| `/incidents` | GET | 以 JSON 格式返回所有已记录的事件 |
| `/health` | GET | 健康检查 |
## 本地运行
复制示例 env 文件并添加你的 Slack webhook URL:
```
cp .env.example .env
```
安装依赖并启动 bot:
```
pip install -r requirements.txt
python bot.py
```
该 bot 运行在端口 6000 上。使用示例告警 payload 对其进行测试:
```
curl -X POST http://localhost:6000/webhook \
-H "Content-Type: application/json" \
-d '{
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "SLO Breach - Success Rate"
},
"annotations": {
"summary": "Success rate dropped below 99%"
}
}
]
}'
```
检查事件是否已被记录:
```
curl http://localhost:6000/incidents
```
你应该会在 Slack 频道中看到一条消息,以及包含事件 ID、时间戳和严重性级别的 JSON 响应。
## 部署到 Kubernetes
假设 k3s 正在运行,并且 bot 镜像已导入到 containerd 中。
构建并导入镜像:
```
sudo docker build -t incident-bot:latest .
sudo docker save incident-bot:latest | sudo k3s ctr images import -
```
部署:
```
sudo kubectl apply -f deployment.yaml
```
获取集群 IP:
```
sudo kubectl get svc incident-bot -n monitoring
```
将 Grafana 指向该地址:
1. 前往 Alerting > Contact points > Add contact point
2. Type(类型)选择:Webhook
3. URL:`http://:6000/webhook`
4. 前往 Alerting > Notification policies,并将其设置为默认的 contact point
## 环境变量
| 变量 | 描述 |
|----------|-------------|
| `SLACK_WEBHOOK_URL` | 来自 api.slack.com 的 Slack incoming webhook URL |
## 项目结构
```
incident-bot/
bot.py Flask app, webhook handler, incident logger
requirements.txt
Dockerfile
deployment.yaml Kubernetes Deployment and Service
.env.example Environment variable template
```
标签:API集成, ETW劫持, Grafana告警, PB级数据处理, Python, Slack通知, SRE运维, Webhook接收器, 严重性分级, 事件响应机器人, 可观测性, 告警分类, 团队协作, 子域名突变, 安全运维, 无后门, 日志记录, 消息推送, 监控告警, 结构化事件, 自动化应急响应, 请求拦截, 逆向工具