sezgiozrn/Logmortem
GitHub: sezgiozrn/Logmortem
这是一款利用 AI 自动拉取 AWS CloudWatch 日志和 GitHub 部署记录,快速生成结构化故障复盘初稿的运维自动化工具。
Stars: 0 | Forks: 0
# Logmortem
在经历了 2 小时的故障之后手动编写 RCA(根本原因分析)——翻阅日志、重建时间线、关联部署——过程缓慢且乏味,而且这往往发生在你已经精疲力竭的时候。logmortem 可以完成初稿,这样工程师就可以专注于验证和改进它,而不是在凌晨 3 点从头开始构建。
只需提供 CloudWatch 日志组、时间窗口和触发的警报,它就会拉取日志,关联最近的 GitHub Actions 部署,并在一分钟内输出结构化的复盘报告。
## 示例输出
```
# ECS 服务崩溃 — 连接池耗尽
| Field | Value |
|---|---|
| Severity | P1 |
| Start | 2026-04-08T02:00:00 |
| Duration | 45 minutes |
| Alert | ECS TaskCount dropped below threshold |
## 根本原因
DB connection pool limit (max_connections=20) was not increased when the deploy
doubled service instances from 4 to 8, exhausting available connections.
## Deploy 关联
Commit abc12345 deployed 30 minutes before incident attempted to fix pool size
but used the wrong config key.
## 行动项
| Priority | Action | Owner |
|---|---|---|
| HIGH | Add pre-deploy check for DB connection pool headroom | platform-team |
| HIGH | Align staging DB config with production | infra |
```
## 工作原理
1. 获取故障时间窗口的 CloudWatch 日志事件(以及故障前 10 分钟的日志作为背景参考)
2. 拉取故障发生前 24 小时内的 GitHub Actions 工作流运行记录
3. 自动过滤健康检查产生的噪音
4. 将所有信息连同结构化的 RCA prompt 发送给 Claude
5. 输出一份 Markdown 格式的复盘报告,其中包含时间线、根本原因、部署关联、促成因素和待办事项
## 局限性与改进设想
- **关联是基于时间的,而非因果关系的。** 故障发生前 30 分钟的部署会被标记;由 Claude 决定其是否相关。这只是供人工验证的草稿,而非最终结论——偶尔也会出现盲目自信的错误判断。
- **仅支持日志。** 无法获取 CloudWatch Metrics 或 traces。如果根本原因隐藏在延迟图表中而非日志行中,则会被遗漏。
- **仅支持 GitHub Actions** 的部署历史。其他的 CD 系统无法识别。
- **庞大的故障时间窗口可能会超出 context budget。** 长时间窗口内嘈杂的日志组会被截断,而不是被总结。
- 如果我重新构建它:我会采用可插拔的日志源、带有预总结的分块摄取(而不是直接截断),以及一个能根据已知原因的故障对 RCA 草稿进行评分的 eval 评估工具,而不是仅凭直觉。
## 自动触发
logmortem 可以在部署失败时自动运行。将 `.github/workflows/auto-rca.yml` 添加到你的 repo 并设置以下 secrets:
| Secret | Required | Description |
|--------|----------|-------------|
| `ANTHROPIC_API_KEY` | Yes | Claude API key |
| `AWS_ACCESS_KEY_ID` | Yes | AWS credentials |
| `AWS_SECRET_ACCESS_KEY` | Yes | AWS credentials |
| `AWS_DEFAULT_REGION` | No | Defaults to us-east-1 |
| `LOG_GROUP` | No | CloudWatch log group to query |
当任何工作流失败时,logmortem 会自动生成 RCA 并将其发布到 GitHub Actions 作业摘要中——可以直接在失败的运行记录中查看。
## 使用方法
```
# 基础 — 仅日志
python src/main.py \
--log-group /aws/ecs/payment-service \
--start-time 2026-04-08T02:00:00 \
--end-time 2026-04-08T03:00:00 \
--alert "ECS TaskCount dropped below threshold"
# 带有 Deploy 关联
python src/main.py \
--log-group /aws/ecs/payment-service \
--start-time 2026-04-08T02:00:00 \
--end-time 2026-04-08T03:00:00 \
--alert "ECS TaskCount dropped below threshold" \
--repo your-org/your-app
# Dry run — 查看已收集的数据,而无需调用 Claude
python src/main.py \
--log-group /aws/ecs/payment-service \
--start-time 2026-04-08T02:00:00 \
--end-time 2026-04-08T03:00:00 \
--alert "ECS TaskCount dropped below threshold" \
--repo your-org/your-app \
--dry-run
# 自定义输出文件
python src/main.py \
--log-group /aws/ecs/payment-service \
--start-time 2026-04-08T02:00:00 \
--end-time 2026-04-08T03:00:00 \
--alert "ECS TaskCount dropped below threshold" \
--output incidents/2026-04-08-payment-outage.md
```
## 设置
```
git clone https://github.com/sezgiozrn/Logmortem.git
cd Logmortem
pip install -r requirements.txt
```
```
export ANTHROPIC_API_KEY=your_key_here
export GITHUB_TOKEN=your_github_token # optional, for deploy correlation
```
通过标准的 boto3 链获取 AWS 凭证(`~/.aws/credentials`、环境变量或实例配置文件)。
## 运行测试
```
pip install pytest pytest-cov
pytest tests/ -v --cov=src --cov-report=term-missing
```
## 技术栈
- **Python** — CLI 和数据摄取
- **boto3** — CloudWatch Logs
- **GitHub REST API** — Actions 工作流历史记录
- **Claude API** — RCA 合成
## 相关内容
为该工具提供参考的运行手册和复盘模板位于 [Platform-Runbooks](https://github.com/sezgiozrn/Platform-Runbooks) 中——包含 AWS/ECS 故障的严重程度级别、上报路径和分类处理步骤。
标签:AIOps, AWS, DLL 劫持, DPI, 事故复盘, 大语言模型, 根因分析, 运维自动化, 逆向工具