abhijha8287/Autonomous-Incident-Commander
GitHub: abhijha8287/Autonomous-Incident-Commander
AI 驱动的事件自主响应平台,将 Splunk 告警自动转化为根因分析、事件时间线和管理层 PDF 报告,全程无需人工干预。
Stars: 0 | Forks: 0
# Autonomous Incident Commander
[](./LICENSE)
[](https://www.python.org/)
[](https://nodejs.org/)
AI 驱动的事件响应平台,能够**自主调查、分析并生成报告** —— 全程无需人工干预。
当告警触发时,平台会查询 Splunk MCP,构建事件时间线,通过置信度分数识别根本原因,并生成一份适合管理层阅读的 PDF 报告 —— 整个过程无需人工输入任何查询指令。
## 问题所在
现代 SRE 团队在每个事件上需要花费 **30–90 分钟** 来处理这些机械性的任务:
- 跨服务搜索日志
- 关联时间戳以构建时间线
- 识别最初失败的原因及其背后的原因
- 为管理层撰写事件报告
现有的 AI 工具(如 Splunk AI Assistant、Datadog Bits AI、Dynatrace Davis)都是**副驾驶(Copilot)** —— 它们仅在被提问时给出答案。它们不会主动执行。
**Autonomous Incident Commander 则会主动执行。**
## 架构
请参阅 [`architecture_diagram.md`](./architecture_diagram.md) 获取完整的图表(系统架构、数据流序列、Agent 数据契约)。
```
Browser (React + Vite)
│
│ POST /api/investigate/{id}/start
│ GET /api/investigate/{id}/stream ← EventSource (SSE)
▼
FastAPI Backend (Python + asyncio)
│
│ asyncio.Queue → BackgroundTask
▼
┌─────────────────────────────────────────────────────────┐
│ 4-Agent Pipeline │
│ │
│ [1] Investigation Agent │
│ └─ Queries Splunk MCP (or log simulator) │
│ → InvestigationOutput │
│ ↓ │
│ [2] Timeline Agent │
│ └─ Chronological event ordering │
│ → TimelineOutput │
│ ↓ │
│ [3] Root Cause Agent ⭐ │
│ └─ LLM streaming reasoning (claude-sonnet-4-6) │
│ → RCAOutput {root_cause, confidence_pct, ...} │
│ ↓ │
│ [4] Report Agent │
│ └─ Executive PDF generation (ReportLab) │
│ → ReportOutput + PDF file │
└─────────────────────────────────────────────────────────┘
│
├─ Splunk MCP Server (stdio) → Splunk Enterprise
└─ Anthropic API (claude-sonnet-4-6, streaming)
```
## 功能
- **实时事件大屏** —— 单屏仪表盘,日志、AI 推理过程和时间线全部实时同步流式展示
- **Splunk MCP 集成** —— 调查 Agent 通过 MCP 协议将 Splunk 作为工具调用;可透明回退至内置的日志模拟器
- **带置信度评分的根本原因分析** —— 真实的 LLM 输出,而非硬编码;在 UI 中逐 token 流式传输(打字机效果)
- **自动生成事件时间线** —— 对所有受影响服务的事件按时间顺序排列,并进行严重性分类
- **管理层 PDF 报告** —— 包含事件摘要、时间线表格、根本原因、证据、优先级修复建议;在调查结束时即可下载
- **预设演示场景** —— 数据库连接池耗尽级联故障(包含 24 条真实的日志事件,涉及 5 个服务,事件窗口为 8 分钟)
## 要求
### 系统
| 依赖 | 版本 | 备注 |
|---|---|---|
| Python | 3.11+ | 后端 runtime |
| Node.js | 18+ | 前端构建 |
| npm | 9+ | 前端包管理器 |
### Python 包(通过 `requirements.txt` 自动安装)
| 包 | 用途 |
|---|---|
| `fastapi` | REST API + SSE 流式传输 |
| `uvicorn[standard]` | ASGI 服务器 |
| `sqlalchemy` | ORM (SQLite) |
| `anthropic` | LLM API 客户端 (claude-sonnet-4-6) |
| `mcp` | Splunk MCP Server 客户端 |
| `reportlab` | PDF 生成 |
| `python-dotenv` | 环境变量加载 |
| `pydantic` | Agent 数据契约 / schema 校验 |
| `aiofiles` | 异步文件 I/O |
### API 密钥
| 密钥 | 是否必需 | 获取途径 |
|---|---|---|
| `ANTHROPIC_API_KEY` | **是** | [console.anthropic.com](https://console.anthropic.com) |
| Splunk 凭证 | 可选 | 仅在 `SPLUNK_MODE=live` 时需要 |
## 安装与运行
### 1. 克隆代码库
```
git clone https://github.com/abhijha8287/Autonomous-Incident-Commander.git
cd Autonomous-Incident-Commander
```
### 2. 配置环境
```
cp .env.example .env
```
打开 `.env` 并设置您的 Anthropic API 密钥:
```
ANTHROPIC_API_KEY=sk-ant-api03-...
```
在演示时保持 `SPLUNK_MODE=simulator` 即可 —— 无需安装 Splunk Enterprise。
### 3. 安装后端依赖
```
# 创建并激活虚拟环境
python -m venv venv
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate
# 安装 packages
pip install -r requirements.txt
```
### 4. 安装前端依赖
```
cd frontend
npm install
cd ..
```
### 5. 运行
打开**两个终端**:
**终端 1 —— 后端:**
```
# 从项目根目录开始,venv 处于激活状态
python -m uvicorn backend.main:app --reload --port 8000
```
**终端 2 —— 前端:**
```
cd frontend
npm run dev
```
打开 **http://localhost:5173**
## 配置示例
### `.env` —— 开发环境(模拟器模式,无需 Splunk)
```
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
SPLUNK_HOST=localhost
SPLUNK_PORT=8089
SPLUNK_USERNAME=admin
SPLUNK_PASSWORD=
SPLUNK_MODE=simulator
```
### `.env` —— 生产环境(连接真实的 Splunk Enterprise)
```
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
SPLUNK_HOST=your-splunk-host.company.com
SPLUNK_PORT=8089
SPLUNK_USERNAME=admin
SPLUNK_PASSWORD=your-splunk-password
SPLUNK_MODE=live
```
### 环境变量参考
| 变量 | 默认值 | 是否必需 | 描述 |
|---|---|---|---|
| `ANTHROPIC_API_KEY` | — | **是** | 用于 claude-sonnet-4-6 的 Anthropic API 密钥 |
| `SPLUNK_MODE` | `simulator` | 否 | `simulator` = 内置日志 \| `live` = 真实 Splunk |
| `SPLUNK_HOST` | `localhost` | 若为 live | Splunk Enterprise 主机名 |
| `SPLUNK_PORT` | `8089` | 若为 live | Splunk 管理端口 |
| `SPLUNK_USERNAME` | `admin` | 若为 live | Splunk 用户名 |
| `SPLUNK_PASSWORD` | — | 若为 live | Splunk 密码 |
## 连接到 Splunk Enterprise
### 1. 安装 Splunk MCP Server
请按照 [Splunk 的 MCP Server 设置指南](https://github.com/splunk/splunk-mcp) 安装并配置服务器。
### 2. 更新 `.env`
```
SPLUNK_MODE=live
SPLUNK_HOST=your-splunk-instance
SPLUNK_PORT=8089
SPLUNK_USERNAME=admin
SPLUNK_PASSWORD=your-password
```
### 3. 确认 MCP 工具名称
Splunk MCP Server 暴露的确切工具名称因版本而异。在演示前请运行一次此命令:
```
# quick_check.py — 从项目根目录运行,且 venv 处于激活状态
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def check():
params = StdioServerParameters(
command="splunk-mcp",
args=["--host", "your-host", "--port", "8089",
"--username", "admin", "--password", "your-pass"]
)
async with stdio_client(params) as (r, w):
async with ClientSession(r, w) as session:
await session.initialize()
tools = await session.list_tools()
print([t.name for t in tools.tools])
asyncio.run(check())
```
如果工具名称与 `"search"` 不同,请更新 `backend/splunk/mcp_client.py` 中的 `call_tool(...)` 行。
## 运行演示场景
平台在启动时会自动预设一个 **数据库连接池耗尽** 事件。有关完整的 4 分钟演示脚本,请参阅 [`demo.md`](./demo.md)。
**快速演示:**
1. 打开 http://localhost:5173 —— 有一个待处理的活动事件
2. 点击该事件 → 打开调查界面
3. 点击 **Start AI Investigation**
4. 观察过程:日志流式传输、构建时间线、AI 推理、生成 PDF
5. 点击 **Download PDF Report**
要在任何时候触发新的事件:
```
curl -X POST http://localhost:8000/api/incidents/trigger \
-H "Content-Type: application/json" \
-d '{"service": "checkout-service", "title": "Critical Error Rate — checkout-service"}'
```
## 项目结构
```
Autonomous-Incident-Commander/
├── backend/
│ ├── main.py # FastAPI app, all API endpoints, SSE stream
│ ├── config.py # Environment variable loading
│ ├── database.py # SQLite connection + schema init
│ ├── models.py # SQLAlchemy ORM models
│ ├── agents/
│ │ ├── schemas.py # Pydantic models — shared agent data contracts
│ │ ├── orchestrator.py # Sequential pipeline, asyncio.Queue handoff
│ │ ├── investigation.py # Agent 1: Splunk MCP log collection
│ │ ├── timeline.py # Agent 2: chronological event ordering
│ │ ├── root_cause.py # Agent 3: LLM root cause analysis (streaming)
│ │ └── report.py # Agent 4: synthesis + PDF trigger
│ ├── splunk/
│ │ └── mcp_client.py # Splunk MCP wrapper + simulator fallback
│ ├── simulator/
│ │ └── log_generator.py # 24-event DB connection pool exhaustion scenario
│ └── reports/
│ └── pdf_generator.py # ReportLab PDF: summary, timeline, RCA, remediation
├── frontend/
│ └── src/
│ ├── App.tsx # Router
│ ├── pages/
│ │ ├── Dashboard.tsx # Incident list + trigger button
│ │ └── Investigation.tsx # Live Incident Theater (the wow screen)
│ ├── components/
│ │ ├── LiveLogPanel.tsx # Left: streaming log events
│ │ ├── AIPanel.tsx # Center: AI reasoning + root cause card
│ │ ├── TimelinePanel.tsx # Right: building incident timeline
│ │ └── ReportPanel.tsx # Bottom bar: PDF download
│ └── hooks/
│ └── useIncidentStream.ts # EventSource SSE state management
├── architecture_diagram.md # System architecture + sequence diagrams
├── demo.md # 4-minute demo script for judges
├── requirements.txt # Python dependencies
├── .env.example # Environment variable template
├── Makefile # Dev shortcuts
├── LICENSE # MIT License
└── README.md
```
## API 参考
| 方法 | Endpoint | 描述 |
|---|---|---|
| `GET` | `/api/incidents` | 列出所有事件 |
| `GET` | `/api/incidents/{id}` | 获取事件及调查历史 |
| `POST` | `/api/incidents/trigger` | 触发新的演示事件 |
| `POST` | `/api/investigate/{id}/start` | 启动 Agent pipeline,返回 `stream_url` |
| `GET` | `/api/investigate/{id}/stream` | Agent 进度的 SSE 流 |
| `GET` | `/api/investigate/{id}/results` | 最终结构化结果 (JSON) |
| `GET` | `/api/reports/{id}/pdf` | 下载 PDF 报告 |
| `GET` | `/docs` | 交互式 API 文档 (Swagger UI) |
## 演示场景 —— DB 连接池耗尽
内置模拟器会生成一个真实的 8 分钟级联故障:
```
10:00:00 INFO database Connection pool initialized: 0/50
10:01:00 INFO database Connection pool: 22/50 — normal load
10:01:30 WARNING database Connection pool: 42/50 — 84% utilization
10:01:50 WARNING checkout-service DB query latency elevated: 620ms
10:02:00 CRITICAL database Connection pool EXHAUSTED: 50/50
10:02:05 ERROR checkout-service Database connection timeout after 30s
10:02:25 ERROR payment-service Checkout dependency unavailable — halted
10:02:45 ERROR api-gateway 5xx rate: 18% on /api/checkout
10:03:05 CRITICAL checkout-service Circuit breaker OPEN
10:04:00 CRITICAL payment-service 47 transactions failed in last 60s
10:08:00 ALERT alerting INCIDENT: checkout-service critical error rate
```
AI 会分析这 24 个事件,并正确识别出**数据库连接池耗尽**是根本原因,同时提供证据引用和优先级修复建议。
## 许可证
MIT —— 详见 [LICENSE](./LICENSE)
为 **Splunk Agentic Ops Hackathon** 而构建 · FastAPI · SQLite · Anthropic claude-sonnet-4-6 · Splunk MCP · React · Vite · TailwindCSS · ReportLab
标签:AV绕过, FastAPI, SRE运维, 人工智能, 用户模式Hook绕过, 自动化事件响应, 计算机取证, 逆向工具