prometheus について
PrometheusとAlertmanagerがOAuth2 Proxyの背後にあるKubernetes環境向けのクエリスキルです。Pod経由のアクセス、ポートフォワード、付属スクリプトによる即時・期間指定PromQLクエリを説明します。
主な機能
- 01Pod内からPrometheusをクエリ
- 02ポートフォワード経由でAPIにアクセス
- 03即時PromQLクエリの実行
- 04時間間隔を指定した期間クエリ
- 05CPU・メモリ・Podの定番クエリ
活用シーン
- 01監視対象が稼働中か確認
- 02CPUとメモリの使用状況を調査
- 03RunningまたはSucceeded以外のPodを特定
- 04ローカルのポートフォワード経由で繰り返しクエリ
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ ionfury/ homelab --skill prometheusインストール後、Claude Code で/prometheusを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
tests.yamlreferences/queries.mdscripts/promql.sh詳細
Prometheus のクエリ
セットアップ
Prometheus と Alertmanager は、内部ゲートウェイ上の OAuth2 Proxy の背後にあります。DNS URL(https://prometheus.internal.tomnowak.work)は OAuth ログインページにリダイレクトされるため、curl による API クエリには使用できません。
アクセス方法
オプション 1:kubectl exec(手軽で、セットアップ不要)
# Query Prometheus API directly inside the pod
kubectl --context <cluster> exec -n monitoring prometheus-kube-prometheus-stack-0 -c prometheus -- \
wget -qO- 'http://localhost:9090/api/v1/query?query=up' | jq '.data.result'
# Query Alertmanager API
kubectl --context <cluster> exec -n monitoring alertmanager-kube-prometheus-stack-0 -c alertmanager -- \
wget -qO- 'http://localhost:9093/api/v2/alerts' | jq .
オプション 2:ポートフォワード(スクリプトや繰り返しのクエリに使用)
kubectl --context <cluster> port-forward -n monitoring svc/prometheus-operated 9090:9090 &
export PROMETHEUS_URL=http://localhost:9090
promql.sh スクリプトはデフォルトで http://localhost:9090 を使用するため、ポートフォワードですぐに使えます。
クイッククエリ
付属のスクリプト .claude/skills/prometheus/scripts/promql.sh を使用します。
# Start port-forward first (script defaults to http://localhost:9090)
kubectl --context <cluster> port-forward -n monitoring svc/prometheus-operated 9090:9090 &
# Instant query
./scripts/promql.sh query 'up'
# Range query (last hour, 15s resolution)
./scripts/promql.sh range 'node_cpu_seconds_total' --start 1h --step 15s
# Firing alerts only
./scripts/promql.sh alerts --firing
# All alert rules
./scripts/promql.sh rules
# Find metrics by label
./scripts/promql.sh series '{job="node-exporter"}'
# Health check
./scripts/promql.sh health
よく使うクエリ
# CPU usage %
./scripts/promql.sh query 'avg(1 - rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100'
# Memory usage %
./scripts/promql.sh query '(1 - sum(node_memory_MemAvailable_bytes) / sum(node_memory_MemTotal_bytes)) * 100'
# Problem pods
./scripts/promql.sh query 'kube_pod_status_phase{phase!~"Running|Succeeded"} == 1'
# Container restarts (last hour)
./scripts/promql.sh query 'increase(kube_pod_container_status_restarts_total[1h]) > 0'
# Firing alerts
./scripts/promql.sh alerts --firing
# Full alert details
./scripts/promql.sh alerts --verbose
モニタリングリソースの作成
このスキルでは、Prometheus へのクエリを扱います。新しいモニタリングリソース (PrometheusRules、ServiceMonitors、PodMonitors、AlertmanagerConfig、レコーディングルール)の作成については、 monitoring-authoring スキルを参照してください。
Prometheus の詳細
| プロパティ | 値 |
|---|---|
| 名前空間 | monitoring |
| サービス | prometheus-operated:9090 |
| 保持期間 | 14 日間 / 50GB |
| 外部ラベル | prometheus_source: ${cluster_name} |