0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GrafanaダッシュボードをPNG形式で保存する

Last updated at Posted at 2025-07-10

はじめに

K3s上に構築されたGrafanaに,サイドカーとしてGrafana Image Rendererを設定し,GrafanaのダッシュボードをPNG形式の画像として保存する方法について説明します.

Grafana Image Rendererとは

Grafana上にあるダッシュボードの画像をPNG形式でレンダリングする,バックエンドプラグインです.
https://grafana.com/grafana/plugins/grafana-image-renderer/

環境構成

  • Ubuntu Server : 24.04.2
  • K3s : v1.30.6+k3s1
  • Grafana : 3.12.9

マニフェストファイルの変更点

GrafanaをK3sクラスタにデプロイする際に使用したマニフェストファイル内のspec.template.spec.containers.env配下に下記を追記します.

deployment.yaml(抜粋)
  env:
    - name: GF_RENDERING_SERVER_URL
      value: "http://renderer:8081/render" # rendererコンテナのURL
    - name: GF_RENDERING_CALLBACK_URL
      value: "http://grafana:3000/" # GrafanaのURL
    - name: GF_LOG_FILTERS
      value: rendering:debug

またspec.template.spec.containersgrafana-image-rendererイメージとその他のPodの設定を追記します.

deployment.yaml(抜粋)
- name: renderer
  image: grafana/grafana-image-renderer:latest # Image Rendererコンテナ
  ports:
    - containerPort: 8081  # デフォルトのポート
  resources:
    limits:
      memory: 2Gi
      cpu: 2000m
  env:
    - name: XDG_CONFIG_HOME
      value: "/tmp/.chromium"
    - name: XDG_CACHE_HOME
      value: "/tmp/.chromium"

クラスタ外部からGrafana Image Rendererにアクセスするため,NodePortを設定します.

service.yaml(抜粋)
spec:
  selector:
    app: grafana
  ports:
    - name: renderer
      protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30227
  type: NodePort

クラスタに変更した設定を適用します.

$ kubectl apply -f deployment.yaml
deployment.apps/grafana configured
$ kubectl apply -f service.yaml
service/grafana unchanged
service/renderer created
$

画像の保存方法

下記のコマンドに変数を設定して実行します.

$ curl -s "${GRAFANA_URL}/render/d-solo/${dashboard_id}?"\
    "orgId=1&panelId=${panel_id}"\
    "&width=800&height=500"\
    "&from=${EXPORT_DATE}T${START_TIME_UTC}.000Z"\
    "&to=${EXPORT_DATE}T${END_TIME_UTC}.000Z&timezone=UTC"\
    -u "${GRAFANA_AUTH}" > "${output_file}"

設定する変数

  • GRAFANA_URL : GrafanaのフロントページのURL
  • dashboard_id : ダッシュボードID
    • ダッシュボードIDの取得方法
      ダッシュボードのページを開き,取得したいパネルの右上3点マークから「View」→「Export」→「Export as JSON」を選択する.表示されたJSONファイルの下から3行目のuidで確認できる.
  • panel_id : パネルID
    • パネルIDは.ダッシュボードIDと同様にJSONファイルを表示し,上から21行目のidで確認できる.
  • IMAGE_WIDTH, IMAGE_HEIGHT : 出力する画像サイズの縦と横の長さ(px)
  • EXPORT_DATE : 画像を出力する日付
  • START_TIME_UTC, END_TIME_UTC: グラフの開始と終了時間 (UTC(世界標準時))
  • GRAFANA_AUTH : Grafanaログイン時に入力する「username」と「Password」を{username}:{password}の形式で入力する,
  • output_file : 画像の出力先パス

実行例

変数の設定と実行の例を記載します.

  • GRAFANA_URL: your-grafana-domain.com
  • dashboard_id: aefowcgoe7apsd
  • panel_id: 1
  • IMAGE_WIDTH, IMAGE_HEIGHT: 800px(幅)、500px(高さ)
  • START_TIME_UTC, END_TIME_UTC: 2025/07/14 00:00:00 UTC から 2025/07/14 00:00:00 UTC
  • GRAFANA_AUTH: your_username:your_password
  • output_file: ~/image.png

実際のコマンド実行例

$ curl -s "http://your-grafana-domain.com/render/d-solo/aefowcgoe7apsd?"\
    "orgId=1&panelId=1"\
    "&width=800&height=500"\
    "&from=2025-07-14T00:00:00.000Z"\
    "&to=2025-07-14T00:00:00.000Z&timezone=UTC"\
    -u "your_username:your_password" > "~/image.png"
$

指定した出力先パス(上記の例の場合は~/image.png)に,指定したダッシュボードとパネルの画像が保存されます.

VM内のUbuntu Serverで上記のコマンドを実行して取得した画像を,ローカルPCに画像を転送したい場合,ローカルPCにインストールされているターミナルやコマンドプロンプトでscp {ユーザ名}@{VMのホスト名またはIPアドレス}:{画像が保存されているパス} {ローカルPC上の保存先パス}を実行します.

実際のコマンド実行例

$ scp monitoring@monitoring-master-ml:/home/cdsl/monitoring-report-assistant/grafana-panel-images/image.png ~/image.png
image.png 100%   39KB   1.3MB/s   00:00 

ローカルPCに転送して取得した画像の例

image.png

おわりに

この記事では、K3s上に構築されたGrafana環境で、Grafana Image Rendererをサイドカーとして設定し、GrafanaのダッシュボードをPNG形式の画像として保存する方法を解説しました。
記事で紹介したcurlコマンドは、for文を使用して変数を変更することで、複数のダッシュボードやパネルの画像を取得することも可能です。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?