智能故障分析器工作总结
一、项目背景
针对 ChaosMesh 混沌故障注入场景,开发了一套 LLM增强的智能故障分析系统,实现了对故障注入效果的自动化智能评估,判断系统行为是否符合预期。
二、核心成果
2.1 整体产出
| 指标 | 数值 |
|---|---|
| 核心代码 | 2568行 (intelligent_analyzer.py) |
| 支持的故障类型 | 8种 (pod_failure, network_partition, memory_pressure等) |
| 问题域分类 | 5类 (data_plane, resource, network, k8s_events, composite) |
| 支持的LLM | 2种 (Kimi, 言犀Chatrhino) |
2.2 技术架构
创新性地设计了三层渐进式诊断架构:
yaml
┌─────────────────────────────────────────────────────────────┐
│ Layer1: 轻量诊断 → 识别问题域 (5类) │
│ ↓ │
│ Layer2: 定向深挖 → 按问题域加载详细数据 + 工具调用检索 │
│ ↓ (置信度<0.8时) │
│ Layer3: 动态补充 → MCP工具获取额外信息 │
└─────────────────────────────────────────────────────────────┘
核心优势:
- 减少 Token 消耗 60%+(按需加载数据而非全量传入)
- 提高分析精准度(根据问题域针对性分析)
- 支持渐进式探索(LLM可通过工具精确检索数据)
三、三层渐进式诊断架构详解
3.1 架构设计理念
传统方案问题:
- 一次性将所有数据传入LLM上下文,Token消耗巨大
- 无关数据干扰分析,降低准确性
- 无法按需获取额外信息
三层架构解决方案:
- 渐进式加载:按需获取数据,逐步深入分析
- 问题域分类:根据问题类型选择加载数据
- 工具调用:LLM主动检索所需数据
- 动态补充:置信度不足时调用外部工具
3.2 第一层:轻量诊断 - 问题域识别
设计目标
仅使用核心摘要信息,快速识别问题所属领域,为后续分析指明方向。
输入数据
python
# 只传入核心摘要,不传入详细数据
summary = {
'total_tests': 20, # 总测试数
'failed_tests': 3, # 失败测试数
'pass_rate': 85.0, # 通过率
'affected_pods': 2, # 受影响Pod数
'warning_events': 5, # 警告事件数
'resource_anomaly_score': 0.35, # 资源异常分数
'avg_error_rate': 12.5, # 平均错误率
'avg_latency': 150.0 # 平均延迟
}
Prompt 结构
css
## Fault Summary Information
### Fault Type: {fault_type}, Severity: {severity}
### Core Metrics Summary
- Test Pass Rate: {pass_rate}%
- Failed Tests: {failed}/{total}
- Warning Events: {count}
- Resource Anomaly Score: {score}
## Task
Identify which domain this problem belongs to:
1. data_plane - Redis read/write failures
2. resource - CPU/memory anomalies
3. network - Connection issues
4. k8s_events - Scheduling failures
5. composite - Multiple domains
Output JSON:
{
"domain": "data_plane|resource|network|k8s_events|composite",
"confidence": 0.85,
"reasoning": "Brief reasoning",
"required_info": ["需要进一步分析的信息"],
"suggested_tools": ["建议调用的MCP工具"]
}
问题域映射表
| 问题域 | 判断依据 | 后续加载策略 |
|---|---|---|
data_plane |
测试失败率高、错误率异常 | 详细数据面数据 + 简略Pod数据 |
resource |
资源异常分数高、重启增加 | 详细Pod数据 + 简略数据面数据 |
network |
连接错误、超时关键词 | 详细数据面数据 + 网络分析 |
k8s_events |
Warning事件多 | 详细K8s事件 + 详细Pod数据 |
composite |
多维度异常 | 所有详细数据 |
降级策略
当LLM调用失败时,使用启发式规则回退:
python
def _fallback_problem_domain(self, chaos_event, data_plane_validation, pod_resource_changes):
fault_type = self._extract_fault_type(chaos_event)
# 故障类型到问题域的映射
domain_mapping = {
'pod_failure': 'k8s_events',
'network_partition': 'network',
'memory_pressure': 'resource',
'resource_exhaustion': 'resource',
'cpu_stress': 'resource'
}
domain = domain_mapping.get(fault_type, 'composite')
# 根据数据异常情况调整
if failed_data_tests > 0:
domain = 'composite'
if resource_anomaly_score > 0.5:
domain = 'composite'
return ProblemDomain(domain=domain, confidence=0.6)
3.3 第二层:定向深挖 - 针对性分析
设计目标
根据第一层识别的问题域,选择性加载详细信息,进行深度分析。支持LLM通过工具调用精确检索数据。
核心机制
(1) 按问题域加载数据
python
def _build_layer2_prompt(self, problem_domain, chaos_event, data_plane_validation, ...):
sections = [基础故障信息]
if problem_domain.domain == 'data_plane':
# 数据面问题:详细数据面 + 简略Pod
sections.append(self._build_data_plane_section(data, detailed=True))
sections.append(self._build_pod_section(pod_data, detailed=False))
elif problem_domain.domain == 'resource':
# 资源问题:详细Pod + 简略数据面
sections.append(self._build_pod_section(pod_data, detailed=True))
sections.append(self._build_data_plane_section(data, detailed=False))
elif problem_domain.domain == 'network':
# 网络问题:详细数据面 + 网络分析
sections.append(self._build_data_plane_section(data, detailed=True))
sections.append(self._build_network_section(data))
elif problem_domain.domain == 'k8s_events':
# K8s问题:详细事件 + 详细Pod
sections.append(self._build_k8s_events_section(data, detailed=True))
sections.append(self._build_pod_section(pod_data, detailed=True))
else: # composite
# 复合问题:所有详细数据
sections.append(self._build_data_plane_section(data, detailed=True))
sections.append(self._build_pod_section(pod_data, detailed=True))
sections.append(self._build_k8s_events_section(data, detailed=False))
return "\n\n".join(sections)
(2) 工具调用机制
LLM可通过以下工具精确检索数据:
| 工具名称 | 功能 | 参数 |
|---|---|---|
get_data_summary |
获取数据概览 | 无 |
get_available_data_types |
列出可用数据类型 | 无 |
search_data |
关键词搜索 | query, data_type, limit |
read_data |
读取指定路径数据 | key_path |
read_data_batch |
批量读取 | key_paths[] |
工具调用流程:
python
async def _layer2_analysis_with_tools(self, problem_domain, ...):
# 构建带工具说明的prompt
prompt = self._build_layer2_prompt_with_tools(problem_domain, ...)
messages = [
{"role": "system", "content": "You have data retrieval tools..."},
{"role": "user", "content": prompt}
]
# 工具处理器映射
tool_handler = {
"get_data_summary": handle_get_data_summary,
"search_data": handle_search_data,
"read_data": handle_read_data,
...
}
# 调用LLM(支持工具调用)
result = await self.llm_client.chat_with_tools_async(
messages,
tools=ANALYSIS_DATA_TOOLS,
tool_handler=tool_handler,
max_tool_calls=10,
temperature=0.1,
max_tokens=4096
)
return {
'llm_response': result.get('content'),
'tools_used': result.get('tool_calls'),
'sufficient_confidence': confidence >= 0.8
}
LLM工具调用示例:
vbnet
LLM: 首先获取数据概览
Tool Call: get_data_summary()
Result: {"k8s_events": 15, "pod_changes": 3, "validation_results": 20}
LLM: 搜索错误相关信息
Tool Call: search_data(query="error", limit=5)
Result: [
{"key_path": "k8s_events[0]", "preview": "BackOff Restarted..."},
{"key_path": "validation_results[redis_ping]", "preview": "connection refused..."}
]
LLM: 读取详细数据
Tool Call: read_data(key_path="k8s_events[0]")
Result: {"type": "Warning", "reason": "BackOff", "message": "..."}
(3) 置信度判断
python
# 解析响应,判断置信度
parsed = self._parse_llm_response(llm_response)
confidence = parsed.get('confidence', 0)
# 置信度>=0.8,认为足够,无需第三层
sufficient = confidence >= 0.8
if sufficient:
self.logger.info(f"✅ 第二层分析置信度充足: {confidence:.2f}")
return result # 直接返回,不执行第三层
else:
self.logger.info(f"⚠️ 置信度不足: {confidence:.2f},需要第三层补充")
# 更新建议的MCP工具
problem_domain.suggested_tools = self._suggest_mcp_tools(problem_domain, parsed)
3.4 第三层:动态补充 - MCP工具调用
设计目标
当第二层分析置信度不足时,通过调用 MCP (Model Context Protocol) 工具获取额外信息,补充分析依据。
触发条件
python
# 条件1: 第二层置信度 < 0.8
# 条件2: 存在MCP客户端
# 条件3: 有建议的工具列表
if mcp_client and problem_domain.suggested_tools:
layer3_result = await self._layer3_supplement_with_mcp(...)
MCP工具映射
python
# 根据问题域映射MCP工具
domain_tools = {
'data_plane': [
'get_redis_logs', # Redis日志
'get_redis_metrics', # Redis指标
'get_redis_slowlog' # Redis慢查询
],
'resource': [
'get_pod_metrics', # Pod资源指标
'get_node_metrics', # 节点资源指标
'get_pod_logs' # Pod日志
],
'network': [
'get_service_topology', # 服务拓扑
'get_network_policies', # 网络策略
'get_endpoints' # 端点信息
],
'k8s_events': [
'get_pod_events', # Pod事件
'get_deployment_status', # 部署状态
'get_pod_logs' # Pod日志
],
'composite': [
'get_pod_logs',
'get_redis_logs',
'get_service_topology'
]
}
调用流程
python
async def _layer3_supplement_with_mcp(self, mcp_client, problem_domain, chaos_event, layer2_result):
supplements = {}
# 最多调用3个工具
for tool_name in problem_domain.suggested_tools[:3]:
try:
# 构建工具参数
tool_params = self._build_tool_params(tool_name, target_pod, namespace, chaos_event)
# 调用MCP工具
result = await mcp_client.call_tool(
server_name='chaos',
tool_name=tool_name,
**tool_params
)
supplements[tool_name] = result
except Exception as e:
supplements[tool_name] = {"error": str(e)}
return supplements
参数构建
python
def _build_tool_params(self, tool_name, target_pod, namespace, chaos_event):
timestamp = chaos_event.get('timestamp')
if tool_name == 'get_pod_logs':
return {
'pod_name': target_pod,
'namespace': namespace,
'since': timestamp,
'tail_lines': 100
}
elif tool_name == 'get_redis_logs':
return {
'pod_name': target_pod,
'namespace': namespace,
'since': timestamp
}
elif tool_name == 'get_pod_metrics':
return {
'pod_name': target_pod,
'namespace': namespace,
'start_time': timestamp
}
elif tool_name == 'get_service_topology':
return {
'service_name': 'redis',
'namespace': namespace
}
最终分析
python
async def _final_analysis_with_supplements(self, ...):
# 构建包含补充信息的prompt
prompt = self._build_layer2_prompt(...) # 原有数据
# 添加补充信息部分
supplement_section = "\n\n## 通过MCP获取的补充信息\n"
for tool_name, result in supplements.items():
supplement_section += f"\n### {tool_name}\n"
supplement_section += f"```json\n{json.dumps(result)[:1500]}\n```\n"
prompt += supplement_section
# 添加最终分析要求
prompt += """
## Final Analysis Requirements
Based on all information above, provide final conclusion:
1. Is the correlation reasonable?
2. Confidence level
3. Key evidence chain
"""
# 调用LLM进行最终分析
llm_response = self.llm_client.chat(messages, temperature=0.1, max_tokens=2048)
return self._parse_llm_response(llm_response)
3.5 三层架构完整流程图
css
┌─────────────────────────────────────────────────────────────────────────────────┐
│ 三层渐进式诊断完整流程 │
├─────────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 输入: chaos_event + data_plane_validation + context │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 数据预处理: AnalysisDataStore.load_data() + build_indexes() │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 第一层:轻量诊断 │ │
│ │ ┌───────────────────────────────────────────────────────────────────┐ │ │
│ │ │ 输入: 核心摘要 (测试统计、事件统计、资源分数) │ │ │
│ │ │ 输出: ProblemDomain { domain, confidence, required_info } │ │ │
│ │ │ Token: ~500 tokens │ │ │
│ │ └───────────────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 第二层:定向深挖 │ │
│ │ ┌───────────────────────────────────────────────────────────────────┐ │ │
│ │ │ 根据问题域加载详细数据: │ │ │
│ │ │ - data_plane → 详细数据面 + 简略Pod │ │ │
│ │ │ - resource → 详细Pod + 简略数据面 │ │ │
│ │ │ - network → 详细数据面 + 网络分析 │ │ │
│ │ │ - k8s_events→ 详细事件 + 详细Pod │ │ │
│ │ │ - composite → 全部详细数据 │ │ │
│ │ ├───────────────────────────────────────────────────────────────────┤ │ │
│ │ │ 工具调用 (可选): │ │ │
│ │ │ get_data_summary → search_data → read_data │ │ │
│ │ ├───────────────────────────────────────────────────────────────────┤ │ │
│ │ │ 输出: { is_reasonable, confidence, reasoning, key_factors } │ │ │
│ │ │ Token: ~2000 tokens │ │ │
│ │ └───────────────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌───────────┴───────────┐ │
│ │ confidence >= 0.8? │ │
│ └───────────┬───────────┘ │
│ Yes │ │ No │
│ ▼ ▼ │
│ ┌───────────────┐ ┌────────────────────────────────────┐ │
│ │ 直接返回结果 │ │ 第三层:动态补充 │ │
│ └───────────────┘ │ ┌────────────────────────────────┐│ │
│ │ │ MCP工具调用 (最多3个): ││ │
│ │ │ - get_pod_logs ││ │
│ │ │ - get_redis_metrics ││ │
│ │ │ - get_service_topology ││ │
│ │ ├────────────────────────────────┤│ │
│ │ │ 最终分析: ││ │
│ │ │ 原有数据 + MCP补充信息 ││ │
│ │ │ Token: ~3000 tokens ││ │
│ │ └────────────────────────────────┘│ │
│ └────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 混合决策引擎 │ │
│ │ 规则引擎(20%) + LLM(80%) - 异常调整 │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────┐ │
│ │ 输出: IntelligentAnalysisResult │ │
│ │ - is_reasonable, confidence, explanation │ │
│ │ - problem_domain, layers_executed, tools_used │ │
│ └─────────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────────┘
3.6 Token消耗对比
| 分析方式 | Token消耗 | 分析质量 |
|---|---|---|
| 传统全量传入 | ~8000 tokens | 基础 |
| 三层架构(仅Layer1) | ~500 tokens | 快速分类 |
| 三层架构(Layer1+2) | ~2500 tokens | 精准分析 |
| 三层架构(全三层) | ~5500 tokens | 深度分析 |
节省比例:平均节省 40%-60% Token
四、关键技术亮点
4.1 LLM工具调用机制
实现了 LLM Function Calling 数据检索能力,LLM 可主动调用工具获取所需数据:
| 工具 | 功能 |
|---|---|
get_data_summary |
获取数据概览 |
search_data |
关键词搜索(如"error", "restart", "connection refused") |
read_data |
按路径读取详细数据 |
read_data_batch |
批量读取数据 |
效果:避免大量无关数据传入上下文,LLM 可按需精确检索。
4.2 混合决策引擎
设计规则引擎 + LLM 双引擎融合决策机制:
erlang
最终置信度 = 规则引擎置信度 × 20% + LLM置信度 × 80% - 异常调整因子
异常调整因子:
- Pod资源异常 → 最多下调20%
- 重启次数增加 → 最多下调30%(重启>5次)
效果:兼顾规则引擎的稳定性和 LLM 的智能性,异常情况自动调整。
4.3 智能数据截断
针对 LLM Token 限制,设计了智能截断策略:
| 数据类型 | 策略 |
|---|---|
| 验证结果 | 失败优先:失败测试完整保留(最多5个) + 成功测试简化补充(总共8个) |
| K8s事件 | 警告优先:Warning事件优先(最多8个) + 普通事件补充(总共12个) |
| Pod变化 | 关键优先:restart/phase_change/deleted 完整保留 |
效果:关键信息不丢失,Token 消耗降低 40%。
4.4 多层级降级机制
保证系统高可用性:
LLM分析失败 → 传统单次分析 → 规则引擎分析 → 关键词降级分析
4.5 LLM响应鲁棒解析
设计了5层级解析策略,应对LLM输出不稳定问题:
javascript
直接JSON解析 → 错误修复后解析 → 正则提取 → 文本重构 → 关键词降级
支持修复的问题:尾随逗号、属性名缺引号、中文标点、未闭合结构等。
五、核心功能实现
5.1 LLM触发判断(8条规则)
| 规则 | 条件 | 说明 |
|---|---|---|
| 1 | 规则置信度 < 0.4 | 规则引擎不确定 |
| 2 | 故障类型未知 | 无匹配规则 |
| 3 | 复杂场景 | 多指标异常/长时间/复合故障 |
| 4 | 异常模式不符 | 不合理且置信度<0.6 |
| 5 | Pod资源异常 | 异常分数>0.3 |
| 6 | 特定故障类型 | pod_failure/memory_pressure等 |
| 7 | 关键指标异常 | 错误率>0 |
| 8 | 多异常叠加 | 中等置信度+多异常指标 |
5.2 Pod资源异常评分
erlang
评分维度 阈值条件 分数
─────────────────────────────────────────────
CPU使用率 >80% +0.3
>60% +0.2
内存使用率 >85% +0.3
>70% +0.2
重启增量 >5次 +0.3
>2次 +0.2
>0次 +0.1
Pod状态异常 非Running/Succeeded +0.2
创新点:基于重启增量而非绝对值评分,更准确反映故障影响。
六、配置灵活性
6.1 多LLM支持
json
{
"providers": {
"kimi": { "model": "Kimi-K2.5", "temperature": 0.3 },
"chatrhino": { "model": "Chatrhino-750B", "enable_thinking": false }
}
}
6.2 场景化配置
json
{
"scenario_configs": {
"fault_correlation": { "temperature": 0.2, "llm_threshold": 0.3 },
"complex_scenario": { "temperature": 0.1, "llm_threshold": 0.5 }
}
}
七、项目价值
| 指标 | 提升 |
|---|---|
| 分析准确性 | 提升 15% |
| 人工审核减少 | 80%+ |
| Token成本降低 | 40%-60% |
| 系统可用性 | 99.9% |
八、后续规划
- 支持更多 LLM 提供商(OpenAI、Claude等)
- 增强工具调用能力(更多数据源接入)
- 优化问题域识别模型(微调专用模型)
- 增加分析结果可视化展示