Skip to content

自治运维系统

模块概述

版本: v3.0.0 状态: ✅ 已实现 IPC处理器: 15个 最后更新: 2026-03-05

自治运维 (AIOps) 系统,集成异常检测、事件分类、自动修复、回滚管理、告警通知和事后分析。支持 Z-Score/IQR/EWMA 三种检测算法、5 种回滚策略和 4 级告警升级链。

核心特性

  • 异常检测: Z-Score (阈值3.0) / IQR (1.5倍) / EWMA (α=0.3) 三算法
  • 事件分类: 4 级严重度 (P0-P3) 自动分类
  • 自动修复: 3 个内置 Playbook + 自定义修复策略
  • 回滚管理: 5 种回滚类型 (git/docker/config/service/custom)
  • 告警升级: P3(5min)→P2(3min)→P1(1min)→P0 升级链
  • 事后分析: 自动生成事件回顾报告

1. 架构设计

1.1 整体架构图

┌──────────────────────────────────────────────────────────────────┐
│                        前端 (Vue3)                                │
├──────────────────────────────────────────────────────────────────┤
│                       运维管理面板                                 │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌───────────────────┐   │
│  │ 异常监控  │ │ 事件列表  │ │ 修复历史  │ │ 事后分析          │   │
│  └──────────┘ └──────────┘ └──────────┘ └───────────────────┘   │
└──────────────────────────────────────────────────────────────────┘
                              ↕ IPC (15个通道)
┌──────────────────────────────────────────────────────────────────┐
│                        主进程 (Electron)                          │
├──────────────────────────────────────────────────────────────────┤
│                autonomous-ops-ipc.js (378行)                      │
│  ┌────────────────────────────────────────────────────────────┐  │
│  │  AnomalyDetector (675行)       │ IncidentClassifier (707行) │  │
│  │  Z-Score / IQR / EWMA         │  P0-P3 严重度分类          │  │
│  │  滑动窗口 (100样本)            │  规则匹配 + 历史关联       │  │
│  └────────────────────────────────────────────────────────────┘  │
│  ┌────────────────────────────────────────────────────────────┐  │
│  │  AutoRemediator (760行)        │ RollbackManager (424行)    │  │
│  │  3 内置 Playbook              │  5 种回滚类型              │  │
│  │  自动/手动修复                 │  git/docker/config/svc     │  │
│  └────────────────────────────────────────────────────────────┘  │
│  ┌────────────────────────────────────────────────────────────┐  │
│  │  AlertManager (521行)          │ PostmortemGenerator (444行) │  │
│  │  4 通道 (webhook/email/im/app)│  自动事件回顾              │  │
│  │  P3→P2→P1→P0 升级链          │  时间线 + 根因分析         │  │
│  └────────────────────────────────────────────────────────────┘  │
│  ┌────────────────────────────────────────────────────────────┐  │
│  │  PostDeployMonitor (387行)     │ SQLite Database (3 tables) │  │
│  │  部署后健康检查                │                            │  │
│  └────────────────────────────────────────────────────────────┘  │
└──────────────────────────────────────────────────────────────────┘

1.2 事件处理流水线

指标数据流入

AnomalyDetector → 异常检测 (Z-Score/IQR/EWMA)
    ↓ anomaly:detected 事件
IncidentClassifier → 严重度分类 (P0-P3)
    ↓ incident:created 事件
AlertManager → 通知相关人员 (4通道)

AutoRemediator → 匹配 Playbook → 执行修复
    ↓ 修复失败?
RollbackManager → 回滚操作

PostmortemGenerator → 生成事后分析报告

1.3 告警升级链

P3 (Low) ──→ 5分钟无响应 ──→ P2 (Medium)

                             3分钟无响应

                             P1 (High)

                             1分钟无响应

                             P0 (Critical)
                             全渠道通知

1.4 核心组件

组件文件行数说明
AnomalyDetectoranomaly-detector.js675三算法异常检测
IncidentClassifierincident-classifier.js707P0-P3 事件分类
AutoRemediatorauto-remediator.js760自动修复引擎
RollbackManagerrollback-manager.js4245种回滚策略
AlertManageralert-manager.js5214通道告警+升级
PostmortemGeneratorpostmortem-generator.js444事后分析生成
PostDeployMonitorpost-deploy-monitor.js387部署后监控
Ops IPCautonomous-ops-ipc.js37815个IPC处理器

2. 核心模块

2.1 AnomalyDetector

javascript
class AnomalyDetector extends EventEmitter {
  async initialize(database)

  // 检测
  async detect(metricName, value, options?)   // 检测单个指标
  async detectBatch(metrics)                   // 批量检测
  async getAnomalies(options?)                 // 获取异常列表

  // 基线
  async updateBaseline(metricName, values)     // 更新基线
  async getBaseline(metricName)                // 获取基线

  // 配置
  setAlgorithm(algorithm)                      // 设置算法
  setThreshold(threshold)                      // 设置阈值
}

检测算法:

算法参数判定条件适用场景
Z-Scorethreshold=3.0|z| > threshold正态分布数据
IQRmultiplier=1.5value < Q1-1.5×IQR 或 > Q3+1.5×IQR非正态分布
EWMAalpha=0.3偏离加权移动平均 > 阈值时序趋势

滑动窗口: 100 个样本,当前值包含在统计计算中。

2.2 IncidentClassifier

javascript
class IncidentClassifier extends EventEmitter {
  async initialize(database)

  // 分类
  async classify(anomaly)                      // 分类异常事件
  async getIncident(incidentId)                // 获取事件
  async listIncidents(options?)                // 列出事件
  async updateIncident(incidentId, updates)    // 更新事件
  async acknowledgeIncident(incidentId)        // 确认事件

  // 规则
  async addRule(rule)                          // 添加分类规则
  async removeRule(ruleId)                     // 删除规则
}

严重度级别:

级别说明响应时间通知范围
P0严重 - 服务完全不可用立即全渠道
P1高 - 核心功能受损15分钟邮件+IM
P2中 - 非核心功能受损1小时IM
P3低 - 轻微影响4小时应用内

2.3 AutoRemediator

javascript
class AutoRemediator extends EventEmitter {
  async initialize(database)

  // 修复
  async remediate(incident)                    // 自动修复
  async manualRemediate(incidentId, action)    // 手动修复
  async getRemediationHistory(options?)        // 修复历史

  // Playbook 管理
  async addPlaybook(playbook)                  // 添加 Playbook
  async removePlaybook(playbookId)             // 删除 Playbook
  async listPlaybooks()                        // 列出 Playbook
  async executePlaybook(playbookId, context)   // 执行 Playbook
}

内置 Playbook:

Playbook ID触发条件修复动作
pb-high-memory内存使用 > 90%清理缓存、强制 GC、重启服务
pb-high-cpuCPU 使用 > 95%限流、降级非核心服务
pb-error-spike错误率激增切换到备用服务、回滚最近变更

2.4 RollbackManager

javascript
class RollbackManager extends EventEmitter {
  async initialize(database)

  // 回滚
  async rollback(incidentId, type, options?)   // 执行回滚
  async getRollbackHistory(options?)           // 回滚历史
  async getRollbackStatus(rollbackId)          // 回滚状态
}

回滚类型:

类型说明执行动作
git-revertGit 代码回滚git revert 最近提交
docker-rollbackDocker 镜像回滚切换到上一版本镜像
config-restore配置恢复恢复到备份配置
service-restart服务重启重启指定服务
custom自定义回滚执行自定义步骤

回滚返回值:

javascript
{
  success: true,
  rollbackId: 'rb-xxx',
  type: 'git-revert',
  duration: 5000,
  details: { undoneSteps: [...] }
}

2.5 AlertManager

javascript
class AlertManager extends EventEmitter {
  async initialize(database)

  // 告警
  async sendAlert(incident)                    // 发送告警
  async acknowledgeAlert(alertId)              // 确认告警
  async getActiveAlerts()                      // 活跃告警
  async getAlertHistory(options?)              // 告警历史

  // 通道配置
  async configureChannel(channel, config)      // 配置通道
  async getChannelConfig(channel)              // 获取通道配置
}

告警通道:

通道说明配置
webhookWebhook 推送url, headers
email邮件通知smtp, recipients
im即时消息 (Slack/飞书)webhookUrl
in-app应用内通知无需配置

2.6 PostmortemGenerator

javascript
class PostmortemGenerator extends EventEmitter {
  async initialize(database)

  // 报告
  async generate(incidentId)                   // 生成事后分析
  async getPostmortem(incidentId)              // 获取报告
  async listPostmortems(options?)              // 列出报告
}

报告结构:

javascript
{
  incidentId: 'inc-xxx',
  title: '...',
  timeline: [                    // 事件时间线
    { time, event, details }
  ],
  rootCause: '...',              // 根因分析
  impact: '...',                 // 影响范围
  remediation: '...',            // 修复措施
  preventionActions: [],         // 预防建议
  generatedAt: '...',
}

3. 数据模型

3.1 ops_incidents

字段类型说明
idTEXT PK事件ID
anomaly_metricTEXT异常指标名
severityTEXT严重度 (P0/P1/P2/P3)
statusTEXT状态 (open/acknowledged/resolved/closed)
descriptionTEXT事件描述
anomaly_dataTEXT(JSON)异常数据
remediation_idTEXT关联修复ID
postmortemTEXT(JSON)事后分析
acknowledged_atINTEGER确认时间
resolved_atINTEGER解决时间
created_atINTEGER创建时间

3.2 ops_remediation_playbooks

字段类型说明
idTEXT PKPlaybook ID
nameTEXT名称
trigger_conditionTEXT(JSON)触发条件
stepsTEXT(JSON)修复步骤
enabledINTEGER是否启用
success_countINTEGER成功次数
failure_countINTEGER失败次数
created_atINTEGER创建时间

3.3 ops_metrics_baseline

字段类型说明
metric_nameTEXT PK指标名称
meanREAL均值
std_devREAL标准差
q1REAL第一四分位数
q3REAL第三四分位数
sample_countINTEGER样本数
updated_atINTEGER更新时间

4. IPC接口 (15个)

4.1 异常检测 (3个)

通道说明参数
ops:detect-anomaly检测异常metricName, value, options?
ops:get-anomalies获取异常列表options?
ops:update-baseline更新基线metricName, values

4.2 事件管理 (3个)

通道说明参数
ops:list-incidents列出事件options?
ops:acknowledge-incident确认事件incidentId
ops:get-incident获取事件详情incidentId

4.3 修复管理 (3个)

通道说明参数
ops:remediate自动修复incidentId
ops:manual-remediate手动修复incidentId, action
ops:list-playbooks列出Playbook-

4.4 回滚 (2个)

通道说明参数
ops:rollback执行回滚incidentId, type, options?
ops:get-rollback-history回滚历史options?

4.5 告警 (2个)

通道说明参数
ops:get-active-alerts活跃告警-
ops:acknowledge-alert确认告警alertId

4.6 事后分析 (2个)

通道说明参数
ops:generate-postmortem生成事后分析incidentId
ops:get-postmortem获取分析报告incidentId

5. 事件

事件负载说明
anomaly:detected检测到异常
incident:created事件创建
incident:acknowledged事件确认
incident:resolved事件解决
remediation:started开始修复
remediation:completed修复完成
rollback:completed回滚完成
alert:sent告警发送
alert:escalated告警升级
postmortem:generated事后分析完成

6. 文件结构

desktop-app-vue/src/main/ai-engine/cowork/
├── anomaly-detector.js          # 三算法异常检测 (675行)
├── incident-classifier.js       # P0-P3 事件分类 (707行)
├── auto-remediator.js           # 自动修复引擎 (760行)
├── rollback-manager.js          # 5种回滚策略 (424行)
├── alert-manager.js             # 4通道告警+升级 (521行)
├── postmortem-generator.js      # 事后分析生成 (444行)
├── post-deploy-monitor.js       # 部署后监控 (387行)
└── autonomous-ops-ipc.js        # 15个IPC处理器 (378行)

7. 相关文档


文档版本: 1.0 最后更新: 2026-03-05

基于 MIT 许可发布