告警机制

AlertManager 告警通知与响应。

Pigsty 使用 VictoriaMetrics vmalert 进行告警规则评估,AlertManager 进行告警管理和通知分发。


告警架构

┌─────────────────────────────────────────────────────────────┐
│                     通知渠道                                  │
│  ┌───────┐  ┌───────┐  ┌───────┐  ┌───────┐  ┌───────┐     │
│  │ Email │  │ Slack │  │ 钉钉  │  │飞书    │  │Webhook│     │
│  └───────┘  └───────┘  └───────┘  └───────┘  └───────┘     │
└───────────────────────────┬─────────────────────────────────┘
                            │ 通知
┌───────────────────────────┴─────────────────────────────────┐
│                      AlertManager                            │
│                    (告警分组、去重、路由)                      │
└───────────────────────────┬─────────────────────────────────┘
                            │ 告警
┌───────────────────────────┴─────────────────────────────────┐
│                        vmalert                               │
│                     (规则评估引擎)                            │
└───────────────────────────┬─────────────────────────────────┘
                            │ 查询
┌───────────────────────────┴─────────────────────────────────┐
│                    VictoriaMetrics                           │
│                      (指标存储)                              │
└─────────────────────────────────────────────────────────────┘

预置告警规则

PostgreSQL 告警

规则名称 级别 说明
PG_DOWN P0 PostgreSQL 实例宕机
PG_REPLICATION_LAG P1 复制延迟过高
PG_CONNECTION_EXHAUSTED P1 连接数接近上限
PG_DEADLOCK P2 检测到死锁
PG_LONG_TRANSACTION P2 长事务未提交
PG_BACKUP_FAILED P1 备份失败

主机告警

规则名称 级别 说明
NODE_DOWN P0 节点不可达
NODE_CPU_HIGH P2 CPU 使用率过高
NODE_MEMORY_HIGH P2 内存使用率过高
NODE_DISK_FULL P1 磁盘空间不足

高可用告警

规则名称 级别 说明
PATRONI_FAILOVER P1 发生故障切换
PATRONI_NO_LEADER P0 集群无主库
ETCD_DOWN P0 ETCD 节点宕机

告警级别

级别 含义 响应要求
P0 紧急 立即响应,服务中断
P1 严重 尽快处理,可能影响服务
P2 警告 计划处理,潜在问题
P3 信息 需要关注,但不紧急

通知配置

邮件通知

# /etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'alertmanager@example.com'
  smtp_auth_username: 'alertmanager@example.com'
  smtp_auth_password: 'password'

receivers:
  - name: 'email'
    email_configs:
      - to: 'dba@example.com'

Slack 通知

receivers:
  - name: 'slack'
    slack_configs:
      - api_url: 'https://hooks.slack.com/services/xxx/xxx/xxx'
        channel: '#alerts'

钉钉通知

receivers:
  - name: 'dingtalk'
    webhook_configs:
      - url: 'https://oapi.dingtalk.com/robot/send?access_token=xxx'

告警路由

根据告警标签路由到不同接收者:

route:
  group_by: ['alertname', 'cls']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  receiver: 'default'
  routes:
    # P0 告警立即通知
    - match:
        severity: P0
      receiver: 'oncall'
      repeat_interval: 15m

    # PostgreSQL 告警发给 DBA
    - match:
        job: pgsql
      receiver: 'dba-team'

    # 特定集群告警发给对应团队
    - match:
        cls: pg-order
      receiver: 'order-team'

告警静默

临时静默告警(如计划维护期间):

通过 Web UI

  1. 访问 AlertManager(http://<ip>:9093
  2. 点击 “Silences” -> “New Silence”
  3. 设置匹配条件和静默时间

通过命令行

# 创建静默
amtool silence add alertname="PG_DOWN" instance="pg-test-1" --duration=2h

# 查看静默
amtool silence query

# 取消静默
amtool silence expire <silence-id>

自定义告警规则

添加告警规则

# /etc/prometheus/rules/custom.yml
groups:
  - name: custom
    rules:
      - alert: HighQueryLatency
        expr: pg_stat_statements_mean_exec_time > 1000
        for: 5m
        labels:
          severity: P2
        annotations:
          summary: "High query latency on {{ $labels.ins }}"
          description: "Average query execution time is {{ $value }}ms"

规则格式

- alert: <告警名称>
  expr: <PromQL 表达式>
  for: <持续时间>
  labels:
    severity: <级别>
  annotations:
    summary: <摘要>
    description: <详细描述>

告警最佳实践

  1. 避免告警疲劳

    • 只告警需要人工处理的问题
    • 合理设置阈值和持续时间
    • 使用告警分组减少通知数量
  2. 分级处理

    • P0 告警必须立即响应
    • P1 告警在工作时间内处理
    • P2/P3 告警可以批量处理
  3. 维护期间静默

    • 计划维护前创建静默
    • 维护完成后验证并取消静默
  4. 持续优化

    • 定期回顾告警历史
    • 调整误报较多的规则
    • 补充遗漏的告警场景