mcpField
P
prometheus

prometheus

github.com/julianobarbosa云与 DevOps1464 次安装3 天前更新
代码仓库

关于 prometheus

prometheus 是通过 HTTP API 和随附脚本查询 Prometheus 监控系统的技能,面向需要检查时序数据的用户。它还可用于查看目标、元数据、告警和系统状态。

主要功能

  1. 01执行即时查询和区间查询
  2. 02格式化 PromQL 并查找时间序列
  3. 03获取标签和指标元数据
  4. 04检查目标、规则和告警
  5. 05查看配置与运行状态
  6. 06附带查询和健康检查脚本

适用场景

  1. 01检查某一时刻的指标值
  2. 02分析一段时间内的指标变化
  3. 03查看监控目标和活动告警
  4. 04检查 Prometheus 健康状况和运行信息

安装

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

$npx skills add https://github.com/julianobarbosa/claude-code-skills --skill prometheus

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

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
api_reference.md
promql_functions.md
scripts/
prom_health.py
prom_metadata.py
prom_query.py

详细介绍

Prometheus API 技能

通过 /api/v1 上的 HTTP API 查询 Prometheus 监控系统。

快速参考

即时查询
curl 'http://<prometheus>:9090/api/v1/query?query=<promql>&time=<timestamp>'
范围查询
curl 'http://<prometheus>:9090/api/v1/query_range?query=<promql>&start=<ts>&end=<ts>&step=<duration>'

响应格式

所有响应均返回 JSON:

{
  "status": "success" | "error",
  "data": <result>,
  "errorType": "<string>",
  "error": "<string>",
  "warnings": ["<string>"]
}

HTTP 状态码:400(参数错误)、422(表达式错误)、503(超时)。

查询端点

端点用途关键参数
/api/v1/query即时查询query、time、timeout、limit
/api/v1/query_range范围查询query、start、end、step、timeout、limit
/api/v1/format_query格式化 PromQLquery
/api/v1/series根据标签查找时间序列match[]、start、end、limit
/api/v1/labels列出标签名称start、end、match[]、limit
/api/v1/label/<name>/values标签值start、end、match[]、limit
/api/v1/query_exemplars查询示例query、start、end

元数据与状态端点

端点用途
/api/v1/targets目标发现状态(state=active|dropped|any)
/api/v1/targets/metadata来自目标的指标元数据
/api/v1/metadata所有指标元数据
/api/v1/rules告警规则/记录规则
/api/v1/alerts活跃告警
/api/v1/alertmanagersAlertmanager 发现
/api/v1/status/config当前配置 YAML
/api/v1/status/flagsCLI 标志
/api/v1/status/runtimeinfo运行时信息
/api/v1/status/buildinfo构建信息
/api/v1/status/tsdbTSDB 基数统计
/api/v1/status/walreplayWAL 重放进度

管理端点(需要 --web.enable-admin-api)

端点方法用途
/api/v1/admin/tsdb/snapshotPOST创建 TSDB 快照
/api/v1/admin/tsdb/delete_seriesPOST删除时间序列(match[]、start、end)
/api/v1/admin/tsdb/clean_tombstonesPOST清理已删除数据

常见 PromQL 模式

# Rate of counter over 5m
rate(http_requests_total[5m])

# Sum by label
sum by (job) (rate(http_requests_total[5m]))

# Percentile from histogram
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))

# Filter by label
up{job="prometheus", instance=~".*:9090"}

# Increase over time
increase(http_requests_total[1h])

# Average over time range
avg_over_time(process_cpu_seconds_total[5m])

结果类型

  • vector:[{"metric": {...}, "value": [timestamp, "value"]}]
  • matrix:[{"metric": {...}, "values": [[ts, "val"], ...]}]
  • scalar:[timestamp, "value"]
  • string:[timestamp, "string"]

脚本

查询脚本:scripts/prom_query.py

# Instant query
python scripts/prom_query.py http://localhost:9090 'up'

# Range query
python scripts/prom_query.py http://localhost:9090 'rate(http_requests_total[5m])' \
  --start '2024-01-01T00:00:00Z' --end '2024-01-01T01:00:00Z' --step '1m'

# Output: table, json, csv
python scripts/prom_query.py http://localhost:9090 'up' --format table

运行状况检查:scripts/prom_health.py

python scripts/prom_health.py http://localhost:9090

详细参考

完整 API 文档:references/api_reference.md

PromQL 函数:references/promql_functions.md


注意事项

  • 对频繁重置的计数器使用 rate():计算正确但毫无意义——当计数器无法在两次抓取之间保持连续时,使用 increase() 并显式除以间隔。
  • up{} 是针对每个目标的量表:不稳定的目标会显示 up=0,但只有满足 for 条件时才会触发告警。为存活检测设置较短的 for,为减少噪声设置较长的 for。
  • 记录规则按固定间隔评估;错过的评估不会补算——因此事件期间记录序列中会出现间断。
  • 联邦查询的 match[] 参数要求所有匹配器都匹配——空匹配器不会返回任何时间序列,看起来像是查询正常但没有数据。
  • 陈旧标记语义:某个时间序列停止被抓取 → 默认在 5 分钟后出现陈旧标记 → 查询会看到“无数据”,而不是“0”。这会影响基于 absent() 的告警。
  • 服务发现 + relabel_config:keep 操作中的错误正则表达式会静默丢弃所有目标——每次更改配置后,都要通过 /api/v1/targets 验证。

评论

支持 Markdown