关于 prometheus
prometheus 是通过 HTTP API 和随附脚本查询 Prometheus 监控系统的技能,面向需要检查时序数据的用户。它还可用于查看目标、元数据、告警和系统状态。
主要功能
- 01执行即时查询和区间查询
- 02格式化 PromQL 并查找时间序列
- 03获取标签和指标元数据
- 04检查目标、规则和告警
- 05查看配置与运行状态
- 06附带查询和健康检查脚本
适用场景
- 01检查某一时刻的指标值
- 02分析一段时间内的指标变化
- 03查看监控目标和活动告警
- 04检查 Prometheus 健康状况和运行信息
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ julianobarbosa/ claude-code-skills --skill prometheus安装后在 Claude Code 中输入/prometheus确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/api_reference.mdpromql_functions.mdscripts/prom_health.pyprom_metadata.pyprom_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 | 格式化 PromQL | query |
/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/alertmanagers | Alertmanager 发现 |
/api/v1/status/config | 当前配置 YAML |
/api/v1/status/flags | CLI 标志 |
/api/v1/status/runtimeinfo | 运行时信息 |
/api/v1/status/buildinfo | 构建信息 |
/api/v1/status/tsdb | TSDB 基数统计 |
/api/v1/status/walreplay | WAL 重放进度 |
管理端点(需要 --web.enable-admin-api)
| 端点 | 方法 | 用途 |
|---|---|---|
/api/v1/admin/tsdb/snapshot | POST | 创建 TSDB 快照 |
/api/v1/admin/tsdb/delete_series | POST | 删除时间序列(match[]、start、end) |
/api/v1/admin/tsdb/clean_tombstones | POST | 清理已删除数据 |
常见 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