thetechdisciple01/Incident-Response-Toolkit
GitHub: thetechdisciple01/Incident-Response-Toolkit
一套轻量级的 Python 事件响应自动化工具包,通过可配置规则实现故障分类、Slack 告警路由、Runbook 自动执行和事后复盘报告生成的端到端工作流。
Stars: 0 | Forks: 0
# 事件响应自动化工具包
一个轻量级、生产就绪的 Python 工具包,用于自动化 IT 事件响应工作流。简化了跨分布式团队的严重性分类、Slack 告警、Runbook 执行和事后报告生成。
## 概述
跨分布式团队管理事件响应具有挑战性。手动分类耗时数小时,拖慢 MTTR,并导致响应不一致。该工具包自动化了重复部分——分类、路由、告警和基本修复——以便工程师可以专注于实际解决。
**源于运维刚需:** 这源于大规模管理事件响应的真实痛点,其中不一致的分类导致了错失 SLA 和缓慢的确认时间。
## 关键功能
- **严重性分类引擎** - 使用可配置的 ITIL 对齐规则,根据影响、紧迫性和受影响服务自动分类事件
- **Slack 事件告警** - 将格式化的事件通知发送到相应频道,支持基于严重性的路由和用户提及
- **Runbook 触发系统** - 将事件与预定义的 Runbook 匹配,并执行自动化修复步骤
- **事后报告生成器** - 汇总时间线、采取的行动和解决方案,生成结构化报告以进行复盘分析
- **升级管理器** - 当事件在 SLA 时间窗口内未被确认时,处理基于时间的升级
- **可扩展架构** - 易于与 PagerDuty、Datadog、ServiceNow 及其他监控/工单工具集成
## 项目结构
```
incident-response-toolkit/
├── config/
│ ├── severity_rules.yaml # Severity classification rules
│ ├── escalation_paths.yaml # Escalation routing and timing
│ ├── runbooks.yaml # Runbook definitions and triggers
│ ├── slack_config.yaml # Slack workspace configuration
│ └── slack_config.yaml.example # Configuration template
├── src/
│ ├── __init__.py
│ ├── classifier.py # Severity classification engine
│ ├── alerter.py # Slack notification handler
│ ├── runbook_engine.py # Runbook matching and execution
│ ├── escalation.py # Escalation management
│ ├── report_generator.py # Post-incident report builder
│ └── utils.py # Shared utilities and logging
├── tests/
│ ├── test_classifier.py
│ ├── test_alerter.py
│ └── test_escalation.py
├── main.py # CLI entry point
├── requirements.txt
├── .gitignore
└── README.md
```
## 安装
### 前置条件
- Python 3.8+
- pip
### 设置
```
# 克隆 repository
git clone https://github.com/yourusername/incident-response-toolkit.git
cd incident-response-toolkit
# 创建 virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# 安装 dependencies
pip install -r requirements.txt
# 复制并配置 Slack 设置
cp config/slack_config.yaml.example config/slack_config.yaml
# 使用你的 workspace 值编辑 config/slack_config.yaml
```
## 配置
### 1. 严重性规则 (`config/severity_rules.yaml`)
定义如何根据影响、受影响用户和服务层级对事件进行分类:
```
severity_levels:
critical:
conditions:
affected_users: ">1000"
revenue_impact: true
service_tier: "P0"
response_time_minutes: 15
escalation_after_minutes: 10
priority_score: 100
```
### 2. 升级路径 (`config/escalation_paths.yaml`)
配置当事件未被确认时如何升级:
```
escalation_paths:
critical:
initial_contact:
- type: "slack_channel"
target: "incidents-critical"
notify_after_minutes: 0
first_escalation:
- type: "slack_user"
target: "@incident-commander"
notify_after_minutes: 10
```
### 3. Runbooks (`config/runbooks.yaml`)
定义针对特定故障模式的自动化修复步骤:
```
runbooks:
payment_processing_failure:
id: "RB-001"
steps:
- action: "health_check"
target: "payment-api"
- action: "restart_service"
target: "payment-processor"
```
### 4. Slack 集成 (`config/slack_config.yaml`)
设置你的 Slack bot token 和频道映射:
```
export SLACK_BOT_TOKEN="xoxb-your-token-here"
```
## 使用
### 分类并路由事件
```
python main.py classify \
--service "payment-api" \
--impact "high" \
--description "Payment processing failures across US region" \
--affected-users 2500 \
--revenue-impact
```
### 执行 Runbook
```
python main.py runbook \
--incident-id INC-2024-0142 \
--auto-execute
```
### 生成事后报告
```
python main.py report \
--incident-id INC-2024-0142 \
--output ./reports/
```
### 运行完整事件工作流
```
# 此流程从 source(例如 PagerDuty)进行处理、分类、告警和升级
python main.py workflow \
--source pagerduty \
--auto-escalate
```
## 环境变量
```
# Required
export SLACK_BOT_TOKEN="xoxb-your-slack-bot-token"
# Optional - 用于 integrations
export PAGERDUTY_API_KEY="your-pagerduty-api-key"
export SERVICENOW_INSTANCE="yourinstance.service-now.com"
export SERVICENOW_USER="api_user"
export SERVICENOW_PASS="api_pass"
```
## 测试
运行测试套件:
```
# 运行所有 tests
python -m pytest tests/
# 运行特定 test file
python -m pytest tests/test_classifier.py -v
# 运行 with coverage
python -m pytest tests/ --cov=src/
```
## 工作原理
### 事件工作流
1. **事件接入** - 事件来自监控系统(Datadog、PagerDuty、自定义 webhook)
2. **分类** - 严重性引擎分析影响、服务和关键词模式
3. **告警** - Slack 通知发送到相应频道,具有基于严重性的路由
4. **Runbook 匹配** - 系统根据服务和错误模式查找适用的自动化 Runbook
5. **执行** - 执行自动化步骤(健康检查、重启、扩容等)
6. **升级** - 如果在 SLA 内未确认,则升级到下一层级
7. **解决** - 解决后,生成全面的事后报告
### 示例事件流
```
Payment API Down
↓
Classification: CRITICAL (>1000 users, revenue impact)
↓
Slack Alert → incidents-critical
Mention: @incident-commander, @engineering-director
↓
Runbook Match: payment_processing_failure (RB-001)
↓
Execute: health_check → database_check → restart_service
↓
Monitor: Wait for acknowledgment (SLA: 10 min)
↓
Escalation (if needed): Email on-call at 10 min, page director at 20 min
↓
Resolution: Generate report, metrics, action items
```
## 集成示例
### 与 PagerDuty 集成
```
from src.classifier import SeverityClassifier
from src.alerter import SlackAlerter
# 接收来自 PagerDuty 的 webhook
def handle_pagerduty_webhook(event):
classifier = SeverityClassifier()
result = classifier.classify(
service=event['service'],
impact=event['severity'],
description=event['description']
)
alerter = SlackAlerter()
alerter.alert_incident_created(
incident_id=event['incident_id'],
severity=result.severity,
description=event['description']
)
```
### 与自定义监控集成
```
from main import IncidentResponseOrchestrator
orchestrator = IncidentResponseOrchestrator()
# 处理来自 Datadog alert 的 incident
incident_id = orchestrator.process_incident(
service="api-gateway",
impact="high",
description="High latency detected",
auto_execute=True
)
```
## 路线图
- [ ] Microsoft Teams 集成
- [ ] 基于历史事件数据的 ML 严重性预测
- [ ] 自动生成 Confluence 页面用于事后复盘
- [ ] AWS CloudWatch 和 Azure Monitor 告警接入
- [ ] 事件指标仪表盘
- [ ] 来自多个来源的 Webhook 接入
- [ ] 数据库持久化(替换内存存储)
- [ ] 自定义 Webhook 通知
- [ ] On-call 轮值集成
## 贡献
欢迎贡献!请提交 issue 和 pull request。
## 许可证
MIT License - 详情请参阅 LICENSE 文件
## 支持
如有问题、疑问或贡献:
- 在 GitHub 上开启一个 issue
- 查看配置文件中的现有文档
- 查看测试文件以获取使用示例
**专为大规模管理事件响应的团队构建。**
标签:DevOps工具, HTTP工具, ITIL, IT运维, PE 加载器, Python, Runbook自动化, Slack机器人, SLA管理, Socks5代理, SRE, 事件管理, 事后分析, 偏差过滤, 升级策略, 告警路由, 故障分类, 无后门, 网络信息收集, 自动修复, 自动化运维, 逆向工具