2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【第1回】時系列データの入口としての「自分のPC監視」——Windowsを最短でグラフ化する(Grafana Alloy + Prometheus + Grafana)

2
Last updated at Posted at 2025-12-15

対象:自宅PC(Windows 10/11)
ねらい:監視の入口として「自分のPC」を、**可視化(グラフ)**まで一気に到達させる


この記事でやること(ゴール)

  • Windows上で Grafana Alloy を動かして、Windowsメトリクス(CPU/メモリ/ディスクなど)を取得
  • Prometheus に保存
  • Grafana でグラフ化(=「時系列データを触った」体験が完了)

全体構成(ミニマム)

  • Windows(ホスト):Grafana Alloy(サービス起動)
  • Docker:Prometheus / Grafana

Alloy の prometheus.exporter.windowswindows_exporter を内包し、Windowsの各種メトリクスを出します。
enabled_collectors のデフォルトは ["cpu","logical_disk","net","os","service","system"] です(必要に応じて追加します)。


前提

  • Windows に Docker Desktop が入っている
  • (推奨)Alloy も Windows にインストール済み(サービスで動く)

1. Docker で Prometheus と Grafana を起動する

フォルダ例:C:\monitoring\

C:\monitoring\
  docker-compose.yml
  prometheus\
    prometheus.yml
  grafana\
    provisioning\
      datasources\
        datasource.yml

docker-compose.yml

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --web.enable-remote-write-receiver
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
    depends_on:
      - prometheus

volumes:
  prometheus_data:
  grafana_data:

prometheus/prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]

grafana/provisioning/datasources/datasource.yml

apiVersion: 1

datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090
    isDefault: true

起動:

cd C:\monitoring
docker compose up -d

確認:

  • Prometheus: http://localhost:9090
  • Grafana: http://localhost:3000(admin / admin)

2. Alloy(Windowsサービス)で Windows メトリクスを取り、Prometheus に送る

2.1 設定ファイルの場所

Alloy のデフォルト設定ファイルは以下です。

  • %PROGRAMFILES%\GrafanaLabs\Alloy\config.alloy

2.2 config.alloy(最小構成)

Prometheus が remote_write を受けるには --web.enable-remote-write-receiver が必要で、受信エンドポイントは /api/v1/write です。

%PROGRAMFILES%\GrafanaLabs\Alloy\config.alloy を、まずは以下のようにします(既存がある場合は上書きではなく置換・バックアップ推奨)。

// ---- 1) Windows exporter(内蔵) ----
prometheus.exporter.windows "selfpc" {
  // まずは “よく使う”ものを明示(必要に応じて増やす)
  enabled_collectors = [
    "cpu", "logical_disk", "net", "os", "service", "system",
    // おすすめ:ダッシュボードや可視化で効く
    "memory", "cs",
  ]

  // ドライブレターだけに絞る例(任意)
  logical_disk {
    include = "^[A-Z]:$"
    exclude = "^HarddiskVolume.*$"
  }
}

// ---- 2) exporter を scrape ----
prometheus.scrape "selfpc" {
  targets    = prometheus.exporter.windows.selfpc.targets
  forward_to = [prometheus.remote_write.local.receiver]
}

// ---- 3) Prometheus に remote_write ----
prometheus.remote_write "local" {
  endpoint {
    url = "http://localhost:9090/api/v1/write"
  }
}

2.3 Alloy サービス再起動

  • services.msc を開く
  • Alloy サービスを再起動

3. “入った” を確認する(Prometheus → Grafana)

3.1 Prometheus でメトリクスが見えるか

Prometheus(http://localhost:9090)の検索欄で、次がヒットすればOKです。

  • CPU:windows_cpu_time_total
    • image.png
  • メモリ:windows_memory_available_bytes
    • image.png
  • ディスク:windows_logical_disk_free_bytes
    • image.png

3.2 Grafana でグラフ化(最小)

Grafana(http://localhost:3000)→ Explore → データソースに Prometheus を選び、以下をそのまま貼ります。

CPU使用率(%)

100 - (avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[5m])) * 100)

image.png

メモリ空き(bytes)

windows_memory_available_bytes

image.png

Cドライブ使用率(%)

100 * (1 - (windows_logical_disk_free_bytes{volume="C:"} / windows_logical_disk_size_bytes{volume="C:"}))

image.png

補足:logical_disk の size 系は Windows カウンタ由来で更新が遅れることがあります。
(気になる場合は for を長めに取ったアラートにする、などで吸収します)


4. 便利:既製ダッシュボードを一発で入れる(任意)

Grafana には Windows 用ダッシュボードが公開されています。
例:Windows Node All(ID: 19269)(cpu/cs/logical_disk/memory/net/os/system などの collector 前提)

  • Grafana → Dashboards → Import → ID を入れる → Import

5. つまずきポイント(最短で潰す)

  • Prometheus にデータが入らない
    • Prometheus 起動コマンドに --web.enable-remote-write-receiver が入っているか
    • Alloy の remote_write URL が http://localhost:9090/api/v1/write になっているか
  • Alloyが起動しない
    • Windows イベントビューア(Application)に Alloy のログが出ます(原因の当たりを付けるのが早い)
  • メトリクスが少ない
    • enabled_collectorsmemorycs を追加してみる

次回予告(第2回)

第1回で「OS全体のメトリクス」が見えました。次は一歩進めて、

  • **自分が気にしてる“身近なログ・イベント”**を扱う(Windowsイベントログ)
  • プロセス/サービス監視で「困りごとに直結」させる

をやります。


参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?