Prometheus Monitoring Query
Query metrics, alerts, and cluster status from Prometheus.
Two Access Modes
1. Direct URL Access (Recommended)
Use when Prometheus is accessible via network (cloud service, remote server, etc.):
CODEBLOCK0
2. Kubernetes Port-Forward
Use when Prometheus is only accessible via kubectl:
CODEBLOCK1
Query Script
Use scripts/query_prometheus.py to query metrics:
CODEBLOCK2
Default: If --prom-url is not specified, uses http://localhost:9090.
Common Query Types
Alerts
CODEBLOCK3
Cluster Status
CODEBLOCK4
Nginx Metrics
CODEBLOCK5
Custom Metrics
Replace <metric_name> with your actual metric:
CODEBLOCK6
Parameters
- -
prom-url: Prometheus URL (default: http://localhost:9090) - INLINECODE5 : PromQL query string
- INLINECODE6 : Flag to query all alerts with states
- INLINECODE7 : Evaluation timestamp (ISO 8601)
- INLINECODE8 : Start time for range query
- INLINECODE9 : End time for range query
- INLINECODE10 : Query resolution step (for range queries)
- INLINECODE11 : Query timeout in seconds (default: 30)
Output
Returns formatted metric results. For instant queries, returns current values. For alerts, returns alert name, state (firing/pending), and labels.
Examples
Check cluster health
CODEBLOCK7
Nginx load
CODEBLOCK8
Custom metric
CODEBLOCK9
Prometheus 监控查询
从 Prometheus 查询指标、告警和集群状态。
两种访问模式
1. 直接 URL 访问(推荐)
当 Prometheus 可通过网络访问时使用(云服务、远程服务器等):
bash
使用远程 Prometheus URL
python scripts/query_prometheus.py \
--prom-url https://prometheus.example.com \
--query up
查询告警
python scripts/query_prometheus.py \
--prom-url https://prometheus.example.com \
--alerts
2. Kubernetes 端口转发
当 Prometheus 仅可通过 kubectl 访问时使用:
bash
终端 1:端口转发 Prometheus
kubectl port-forward -n prometheus svc/prometheus 9090:9090
终端 2:查询指标
python scripts/query_prometheus.py \
--prom-url http://localhost:9090 \
--query up
查询脚本
使用 scripts/query_prometheus.py 查询指标:
bash
查询特定指标(默认:http://localhost:9090)
python scripts/query_prometheus.py --query up
显式指定 URL
python scripts/query_prometheus.py \
--prom-url http://localhost:9090 \
--query rate(http
requeststotal[5m])
查询告警
python scripts/query_prometheus.py --alerts
默认值: 如果未指定 --prom-url,则使用 http://localhost:9090。
常见查询类型
告警
promql
所有触发中的告警
ALERTS{alertstate=firing}
待处理的告警
ALERTS{alertstate=pending}
集群状态
promql
节点状态
kube
nodestatus_condition{condition=Ready}
Pod 状态
kube
podstatus_phase{phase=Running}
kube
podstatus_phase{phase=Failed}
命名空间 Pod 数量
count by (namespace) (kube
podinfo)
Nginx 指标
promql
请求速率
rate(nginx
httprequests_total[5m])
连接统计
nginx
connectionsactive
nginx
connectionsreading
nginx
connectionswriting
nginx
connectionswaiting
请求持续时间(p99)
histogram
quantile(0.99, rate(nginxhttp
requestduration
secondsbucket[5m]))
自定义指标
将 替换为实际指标名称:
promql
当前值
5 分钟速率
rate([5m])
1 小时平均值
avg([1h])
参数
- - prom-url:Prometheus URL(默认:http://localhost:9090)
- query:PromQL 查询字符串
- alerts:查询所有告警及其状态的标志
- time:评估时间戳(ISO 8601)
- start:范围查询的开始时间
- end:范围查询的结束时间
- step:查询分辨率步长(用于范围查询)
- timeout:查询超时时间(秒,默认:30)
输出
返回格式化后的指标结果。对于即时查询,返回当前值。对于告警,返回告警名称、状态(触发中/待处理)和标签。
示例
检查集群健康状态
bash
所有触发中的告警
python scripts/query_prometheus.py --alerts
节点状态
python scripts/queryprometheus.py --query kubenodestatuscondition{condition=Ready}
Nginx 负载
bash
请求速率
python scripts/queryprometheus.py --query rate(nginxhttprequeststotal[5m])
活跃连接数
python scripts/queryprometheus.py --query nginxconnections_active
自定义指标
bash
python scripts/queryprometheus.py --query mycustom_metric
python scripts/queryprometheus.py --query rate(mycustom_metric[5m])