mcpField
P
prometheus

prometheus

github.com/ionfuryクラウド・DevOps2572 回インストール今日更新
リポジトリ

prometheus について

PrometheusとAlertmanagerがOAuth2 Proxyの背後にあるKubernetes環境向けのクエリスキルです。Pod経由のアクセス、ポートフォワード、付属スクリプトによる即時・期間指定PromQLクエリを説明します。

主な機能

  1. 01Pod内からPrometheusをクエリ
  2. 02ポートフォワード経由でAPIにアクセス
  3. 03即時PromQLクエリの実行
  4. 04時間間隔を指定した期間クエリ
  5. 05CPU・メモリ・Podの定番クエリ

活用シーン

  1. 01監視対象が稼働中か確認
  2. 02CPUとメモリの使用状況を調査
  3. 03RunningまたはSucceeded以外のPodを特定
  4. 04ローカルのポートフォワード経由で繰り返しクエリ

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/ionfury/homelab --skill prometheus

インストール後、Claude Code で/prometheusを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

tests.yaml
references/
queries.md
scripts/
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}

コメント