grafana 截圖功能
背景
現在日報,需要以一個圖標的形式進行展示,在grafana監控已經完善的情況下,再使用代碼繪制圖標有些麻煩,進行拓展安裝grafana-image-renderer插件,進行通過url的方式獲取指定圖標的指定時間截圖。
方法1:使用docker-compose
1.創建 docker-compose.yml 內容如下
version: '2'
services:
grafana:
image: grafana/grafana:latest
ports:
- '3009:3000'
environment:
GF_RENDERING_SERVER_URL: http://renderer:8081/render
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
GF_LOG_FILTERS: rendering:debug
volumes:
- "$PWD/png:/var/lib/grafana/png/"
- "$PWD/grafana.db:/var/lib/grafana/grafana.db"
renderer:
image: grafana/grafana-image-renderer:latest
ports:
- 8081
2.進行啟動
cat README.md
### 注意要在當前目錄執行以下命令
cd /opt/grafana
### 啟動
docker-compose up -d
### 停止
docker-compose down
方法2:運行 Grafana Docker 映像
- 創建Dockerfile
這里我根據官方的dockerfile做了Grafana鏡像的版本固定為8.2.0,和解決了截圖分享中文亂碼問題
ARG GRAFANA_VERSION="8.2.0"
FROM grafana/grafana:${GRAFANA_VERSION}
USER root
ARG GF_INSTALL_IMAGE_RENDERER_PLUGIN="false"
ARG GF_GID="0"
ENV GF_PATHS_PLUGINS="/var/lib/grafana-plugins"
RUN mkdir -p "$GF_PATHS_PLUGINS" && \
chown -R grafana:${GF_GID} "$GF_PATHS_PLUGINS"
RUN if [ $GF_INSTALL_IMAGE_RENDERER_PLUGIN = "true" ]; then \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
apk --no-cache upgrade && \
apk add --no-cache udev ttf-opensans chromium && \
apk add fontconfig mkfontscale wqy-zenhei --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing --allow-untrusted && \
rm -rf /tmp/* && \
rm -rf /usr/share/grafana/tools/phantomjs; \
fi
USER grafana
ENV GF_PLUGIN_RENDERING_CHROME_BIN="/usr/bin/chromium-browser"
RUN if [ $GF_INSTALL_IMAGE_RENDERER_PLUGIN = "true" ]; then \
grafana-cli \
--pluginsDir "$GF_PATHS_PLUGINS" \
--pluginUrl https://github.com/grafana/grafana-image-renderer/releases/latest/download/plugin-linux-x64-glibc-no-chromium.zip \
plugins install grafana-image-renderer; \
fi
ARG GF_INSTALL_PLUGINS=""
RUN if [ ! -z "${GF_INSTALL_PLUGINS}" ]; then \
OLDIFS=$IFS; \
IFS=','; \
for plugin in ${GF_INSTALL_PLUGINS}; do \
IFS=$OLDIFS; \
if expr match "$plugin" '.*\;.*'; then \
pluginUrl=$(echo "$plugin" | cut -d';' -f 1); \
pluginInstallFolder=$(echo "$plugin" | cut -d';' -f 2); \
grafana-cli --pluginUrl ${pluginUrl} --pluginsDir "${GF_PATHS_PLUGINS}" plugins install "${pluginInstallFolder}"; \
else \
grafana-cli --pluginsDir "${GF_PATHS_PLUGINS}" plugins install ${plugin}; \
fi \
done \
fi
2.進行build
docker build \
--build-arg "GRAFANA_VERSION=latest" \
--build-arg "GF_INSTALL_IMAGE_RENDERER_PLUGIN=true" \
-t x602/grafana-custom:8.2.0 -f Dockerfile .
3.運行測試
docker run -d -p 3000:3000 --name=grafana x602/grafana-custom:8.2.0
測試
-
創建圖表並進行分享 用戶名密碼默認(admin/admin)


-
通過url保存圖片
這里訪問的from和to是毫秒級別的時間戳
curl -o fuck.png -u admin:admin \
"http://ip:3000/render/d-solo/idlS5qLnk/new-dashboard?orgId=1&from=`date -d "-6 hour" +%s`167&to=`date +%s`167&panelId=2&width=1000&height=500&tz=Asia%2FShanghai"
linux時間戳拓展
%date -d "-1 day" +%s
date +%Y%m%d #顯示前天年月日
date -d "+1 minutes" +%Y%m%d #顯示前一天的日期
date -d "-1 day" +%Y%m%d #顯示后一天的日期
date -d "-1 month" +%Y%m%d #顯示上一月的日期
date -d "+1 hour" +%s #下1小時
date -d "-1 year" +%Y%m%d #顯示前一年的日期
date -d "+1 year" +%Y%m%d #顯示下一年的日期
參考文檔
官方文檔1:https://grafana.com/grafana/plugins/grafana-image-renderer/
grafana 容器數據遷移的處理 : https://www.cnblogs.com/rongfengliang/p/14327261.html
官方文檔2:https://grafana.com/docs/grafana/latest/installation/docker/#custom-image-with-grafana-image-renderer-plugin-pre-installed
官方dockerfile:https://github.com/grafana/grafana/blob/main/packaging/docker/custom/Dockerfile
時間戳計算工具:https://tool.lu/timestamp/
date命令手冊:https://www.gnu.org/software/coreutils/manual/html_node/Examples-of-date.html
Dockerfile 導出 https://inetyoung.blog.csdn.net/article/details/106896963
截圖中文亂碼問題:http://leegorous.net/blog/2018/11/15/how-to-render-chinese-in-alpine-phantomjs/
