KushalParikh/ai-incident-response
GitHub: KushalParikh/ai-incident-response
一个结合 Prometheus 监控、Kafka 事件流和 LLM 大模型的智能事件响应平台,实现从告警检测到 AI 根因分析的自动化运维闭环。
Stars: 1 | Forks: 0
# 🚨 AI 事件响应系统
一个由 AI 驱动的生产事件响应平台,利用 LLM 自动检测、分析系统警报并提供根因分析。
## 🏗️ 架构
```
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Sample App │────▶│ Prometheus │────▶│ Grafana │
│ (FastAPI) │ │ (Metrics) │ │ (Dashboards)│
└──────┬───────┘ └──────┬───────┘ └──────────────┘
│ │
│ logs alerts│
▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Promtail │────▶│ Kafka │────▶│ AI Analyzer │
│ (Log Ship) │ │ (KRaft) │ │ (Gemini/Groq)│
└──────┬───────┘ └──────────────┘ └──────┬───────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Loki │ │ PostgreSQL │
│ (Log Store) │ │ (Incidents) │
└──────────────┘ └──────┬───────┘
│
▼
┌──────────────┐
│ API Server │◀── WebSocket ──▶ Dashboard
│ (FastAPI) │
└──────────────┘
```
## ✨ 关键特性
- **自动警报检测** — Prometheus 监控示例应用并在检测到异常时触发警报
- **AI 根因分析** — Gemini 2.0 Flash 或 Groq (Llama 3.3) 结合指标和日志上下文分析事件
- **实时仪表盘** — 基于 WebSocket 的 UI 实时显示事件
- **混沌工程** — 内置混沌引擎模拟真实的生产事件
- **全栈可观测性** — Prometheus + Grafana + Loki 及预配置的仪表盘
- **事件流** — Kafka (KRaft 模式) 提供可靠的警报管道
- **事件时间线** — 跟踪从警报到解决的所有事件
## 🛠️ 技术栈
| 组件 | 技术 |
|-----------|-----------|
| **Sample App** | Python, FastAPI, Prometheus client |
| **Metrics** | Prometheus |
| **Visualization** | Grafana (预配置) |
| **Log Aggregation** | Loki + Promtail |
| **Event Streaming** | Apache Kafka (KRaft — 无 Zookeeper) |
| **AI Analysis** | Google Gemini 2.0 Flash / Groq (Llama 3.3) |
| **Database** | PostgreSQL |
| **Cache** | Redis |
| **Backend API** | FastAPI + WebSocket |
| **Dashboard** | HTML/CSS/JS + Nginx |
| **Containerization** | Docker + Docker Compose |
| **Orchestration** | Kubernetes (包含 manifests) |
| **CI/CD** | GitHub Actions |
## 🚀 快速开始
### 前置条件
- 已安装 Docker Desktop
- (可选) Gemini API key 或 Groq API key 用于 AI 分析
### 1. 克隆并配置
```
git clone https://github.com/your-username/ai-incident-response.git
cd ai-incident-response
```
### 2. 添加 API Keys (可选)
编辑 `.env` 文件以添加您的免费 AI API key:
```
# 从 https://aistudio.google.com/apikey 免费获取
GEMINI_API_KEY=your-gemini-key
# 或者从 https://console.groq.com/keys 免费获取
GROQ_API_KEY=your-groq-key
```
### 3. 启动技术栈
```
docker-compose up -d --build
```
### 4. 访问服务
| 服务 | URL |
|---------|-----|
| **AI Dashboard** | http://localhost:3001 |
| **Grafana** | http://localhost:3000 (admin/admin) |
| **Prometheus** | http://localhost:9090 |
| **Sample App** | http://localhost:8000 |
| **API Server** | http://localhost:8080 |
### 5. 触发事件
混沌引擎每 2 分钟自动触发一次,您也可以手动触发:
```
# 触发所有 chaos 模式持续 60 秒
curl -X POST http://localhost:8000/api/chaos/start \
-H "Content-Type: application/json" \
-d '{"mode": "all", "duration_seconds": 60, "intensity": 0.7}'
```
或在仪表盘上点击 **“⚡ Trigger Chaos”**。
## 📂 项目结构
```
ai-incident-response/
├── sample-app/ # FastAPI order service (metrics + logs)
│ ├── app.py # Main application
│ ├── chaos.py # Chaos engineering engine
│ └── traffic_generator.py # Continuous traffic generator
├── services/ # Backend services
│ ├── alert_forwarder.py # Prometheus → Kafka alert pipeline
│ ├── ai_analyzer.py # Kafka → AI → PostgreSQL analyzer
│ └── api_server.py # REST API + WebSocket server
├── dashboard/ # Frontend UI
│ ├── index.html
│ ├── styles.css
│ └── app.js
├── config/ # Monitoring configuration
│ ├── prometheus/ # Scrape config + alert rules
│ ├── grafana/ # Datasources + dashboards
│ ├── loki/ # Log storage config
│ └── promtail/ # Log shipping config
├── database/ # PostgreSQL schema
│ └── init.sql
├── k8s/ # Kubernetes manifests
│ └── deployment.yml
├── .github/workflows/ # CI/CD pipeline
│ └── ci.yml
├── docker-compose.yml # Full stack orchestration
└── .env # Environment variables
```
## 🔄 工作原理
1. **Sample App** 提供订单 API,包含 Prometheus 指标和结构化 JSON 日志
2. **Traffic Generator** 持续发送请求以生成真实数据
3. **Chaos Engine** 定期注入故障(延迟、错误、内存峰值)
4. **Prometheus** 抓取指标并评估警报规则
5. **Alert Forwarder** 轮询 Prometheus 获取触发的警报,使用指标和日志上下文丰富数据,并发布到 Kafka
6. **AI Analyzer** 消费 Kafka 数据,构建详细的提示词,调用 Gemini/Groq 进行根因分析
7. **Results** 连同完整的事件时间线存储在 PostgreSQL 中
8. **API Server** 通过 REST + WebSocket 向仪表盘提供数据
9. **Dashboard** 显示实时事件及 AI 分析
## ⚡ 混沌模式
| 模式 | 作用 |
|------|-------------|
| `latency` | 为请求增加 0.5-3s 的随机延迟 |
| `errors` | 导致随机 500 错误 (数据库/超时/下游服务) |
| `memory` | 模拟内存泄漏 (~10MB/s 增长) |
| `all` | 组合所有混沌模式 |
## 📊 Grafana 仪表盘
预配置的仪表盘包括:
- 按状态码分类的请求率
- 按类型分类的错误率
- 延迟百分位数 (p50, p95)
- 已处理的订单总数
- 内存使用量仪表
- 应用日志面板
## 🐳 资源占用
针对基础笔记本电脑优化:
| 服务 | 内存限制 |
|---------|-------------|
| Kafka (KRaft) | 384 MB |
| Prometheus | 256 MB |
| Grafana | 256 MB |
| Loki | 256 MB |
| AI Analyzer | 256 MB |
| Sample App | 256 MB |
| PostgreSQL | 128 MB |
| Promtail | 128 MB |
| Alert Forwarder | 128 MB |
| API Server | 128 MB |
| Traffic Generator | 128 MB |
| Redis | 64 MB |
| Dashboard | 32 MB |
| **Total** | **~2.4 GB** |
## ☸️ Kubernetes 部署
```
# 启动 Minikube
minikube start
# Apply manifests
kubectl apply -f k8s/deployment.yml
# 检查状态
kubectl -n incident-response get pods
```
## 🤝 参与贡献
1. Fork 本仓库
2. 创建一个功能分支 (`git checkout -b feature/improvement`)
3. 提交更改 (`git commit -am 'Add new feature'`)
4. 推送到分支 (`git push origin feature/improvement`)
5. 发起一个 Pull Request
## 📄 许可证
MIT License — 详情见 [LICENSE](LICENSE)。
标签:AIOps, API集成, AV绕过, DLL 劫持, FastAPI, Gemini, Grafana, Kafka, Loki, PE 加载器, PostgreSQL, SonarQube插件, WebSocket, 人工智能, 依赖分析, 可观测性, 大语言模型, 子域名突变, 实时仪表盘, 搜索引擎查询, 故障排查, 根因分析, 测试用例, 用户模式Hook绕过, 自定义请求头, 请求拦截, 软件成分分析, 运维自动化, 逆向工具