mcpField
A
alertmanager-query

alertmanager-query

github.com/victoriametrics云与 DevOps59284 次安装2 周前更新
代码仓库

关于 alertmanager-query

alertmanager-query 是一项通过 curl 查询兼容 Prometheus 的 AlertManager HTTP API v2 的技能,面向运维人员。它涵盖告警检查与静默管理,并提示先检查连通性及在服务不可用时使用 VictoriaMetrics 备用方案。

主要功能

  1. 01列出并筛选告警
  2. 02检查告警状态
  3. 03列出和获取静默
  4. 04创建和删除静默
  5. 05检查连通性及备用方案

适用场景

  1. 01查看活跃、静默或受抑制的告警
  2. 02在维护期间创建或结束静默
  3. 03检查告警路由和抑制状态

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/victoriametrics/skills --skill alertmanager-query

安装后在 Claude Code 中输入/alertmanager-query确认已加载。

附带文件references/api-reference.md

详细介绍

AlertManager 查询(API v2)

通过 curl 直接查询兼容 Prometheus 的 AlertManager HTTP API v2。涵盖带筛选条件的告警列表、静默管理(列出、创建、获取、删除)以及告警状态检查。

可用性警告

AlertManager 作为集群内 Pod 运行,可能无法访问(崩溃循环、DNS 故障、本地未部署)。务必先检查连接状态。AlertManager 宕机时,请改用 VictoriaMetrics 获取告警数据:

# Fallback: firing/pending alerts from VictoriaMetrics (always available)
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_METRICS_URL/api/v1/alerts" | jq '.data.alerts[]'

AlertManager 能提供 VM 告警无法提供的信息:静默、抑制状态和告警路由。

环境

# $VM_ALERTMANAGER_URL - base URL
#   Remote: export VM_ALERTMANAGER_URL="https://alertmanager.example.com"
#   Local:  N/A (AlertManager typically not deployed locally)
# $VM_CURL_CONFIG - curl config file with auth header (set for remote, unset for local)
#   Remote: export VM_CURL_CONFIG="$HOME/.config/victoriametrics/curl.conf"
#   Local:  leave unset (defaults to /dev/null - no auth)

认证模式

所有 curl 命令都会从 curl 配置文件加载认证信息:

curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s "$VM_ALERTMANAGER_URL/api/v2/alerts" | jq .

未设置 VM_CURL_CONFIG 时,curl 会读取 /dev/null,且不会发送认证标头。设置后,该变量必须指向权限为 0600 的 curl 配置文件,其中包含 header = "Authorization: Bearer <token>"。切勿打印其内容。

核心端点

列出告警
# All alerts (active, silenced, inhibited)
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/alerts" | jq .

# Only active (not silenced, not inhibited)
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/alerts?active=true&silenced=false&inhibited=false" | jq .

# Filter by label matcher (URL-encode the matcher)
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s -G \
  --data-urlencode 'filter=alertname="HighMemory"' \
  "$VM_ALERTMANAGER_URL/api/v2/alerts" | jq .

# Filter by receiver
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/alerts?receiver=slack-critical" | jq .

参数:active(布尔值,默认值为 true)、silenced(布尔值,默认值为 true)、inhibited(布尔值,默认值为 true)、unprocessed(布尔值,默认值为 true)、filter(字符串数组,匹配表达式)、receiver(字符串正则表达式)

列出静默
# All silences
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/silences" | jq .

# Filter silences by matcher
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s -G \
  --data-urlencode 'filter=alertname="HighMemory"' \
  "$VM_ALERTMANAGER_URL/api/v2/silences" | jq .

参数:filter(字符串数组,匹配表达式)

按 ID 获取静默
# Note: singular "silence" in path (not "silences")
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/silence/{silenceID}" | jq .

将 {silenceID} 替换为 UUID。

创建静默
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  -X POST -H "Content-Type: application/json" \
  -d '{
    "matchers": [
      {"name": "alertname", "value": "HighMemory", "isRegex": false, "isEqual": true}
    ],
    "startsAt": "2026-03-07T00:00:00Z",
    "endsAt": "2026-03-08T00:00:00Z",
    "createdBy": "claude",
    "comment": "Maintenance window"
  }' \
  "$VM_ALERTMANAGER_URL/api/v2/silences" | jq .

返回 {"silenceID": "uuid-here"}。匹配器字段:name(标签名)、value(匹配值)、isRegex(布尔值)、isEqual(布尔值——否定匹配时为 false)。时间使用 RFC3339 格式。

删除(使其过期)静默
# Note: singular "silence" in path
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  -X DELETE \
  "$VM_ALERTMANAGER_URL/api/v2/silence/{silenceID}"

将 {silenceID} 替换为 UUID。成功时返回空响应正文。

时间戳格式

所有时间戳均使用 RFC3339 格式:2026-03-07T00:00:00Z

响应解析(jq)

# Count alerts by state
... | jq 'group_by(.status.state) | map({state: .[0].status.state, count: length})'

# List alert names and states
... | jq '.[] | {alertname: .labels.alertname, state: .status.state, severity: .labels.severity}'

# Active silences only (not expired)
... | jq '[.[] | select(.status.state == "active")]'

# Silence details (ID, matchers, expiry)
... | jq '.[] | {id: .id, matchers: [.matchers[] | "\(.name)=\(.value)"], endsAt: .endsAt, createdBy: .createdBy}'

常见模式

# Quick connectivity check
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -sf -o /dev/null -w "%{http_code}" \
  "$VM_ALERTMANAGER_URL/api/v2/alerts" && echo " OK" || echo " UNREACHABLE"

# Count firing alerts
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  "$VM_ALERTMANAGER_URL/api/v2/alerts?active=true&silenced=false&inhibited=false" | jq 'length'

# Silence an alert for 2 hours from now
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s \
  -X POST -H "Content-Type: application/json" \
  -d "$(jq -n --arg start "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
    --arg end "$(date -u -d '+2 hours' +%Y-%m-%dT%H:%M:%SZ)" \
    '{matchers: [{name: "alertname", value: $ARGS.named.alert, isRegex: false, isEqual: true}],
      startsAt: $start, endsAt: $end, createdBy: "claude", comment: $ARGS.named.reason}' \
    --arg alert "AlertName" --arg reason "Investigation in progress")" \
  "$VM_ALERTMANAGER_URL/api/v2/silences" | jq .

# Check if a specific alert is silenced
curl -q --config "${VM_CURL_CONFIG:-/dev/null}" -s -G \
  --data-urlencode 'filter=alertname="TargetAlert"' \
  "$VM_ALERTMANAGER_URL/api/v2/alerts?active=false&silenced=true" | jq 'length'

环境切换

# Check current environment
echo "VM_ALERTMANAGER_URL: $VM_ALERTMANAGER_URL"
echo "VM_CURL_CONFIG: ${VM_CURL_CONFIG:-(unset - no auth)}"

重要说明

  • 路径差异:列出/创建使用复数形式 /api/v2/silences,获取/删除使用单数形式 /api/v2/silence/{id}
  • POST 端点需要 Content-Type: application/json
  • DELETE 成功时返回空响应正文(HTTP 200)
  • filter 参数使用 PromQL 风格的匹配器:alertname="Foo"、severity=~"critical|warning"
  • AlertManager 可能宕机——务必准备备用方案,使用 $VM_METRICS_URL/api/v1/alerts
  • 完整的端点详情、参数和响应格式请参阅 references/api-reference.md

评论

支持 Markdown