Grafana 圖表保存方案

引言

InfluxDB做爲時序數據庫,通常保存的數據比較有限,爲了控制數據量,通常都須要設置RetentionPolicy來作數據的按期清理。那麼問題來了,咱們如何保存和提取歷史數據?前端

咱們選擇了Granfana做爲前端報表的展示,那麼可否將其中的報表提取出來,做爲PNG來保存呢?下文將詳細介紹方法。c++

Grafana Server端渲染功能

首先在Grafana中配置好咱們想要的圖表,以後選擇Sharegit

clipboard.png

在Link頁中,最下方有一個連接"Direct link renderd image"github

clipboard.png

點擊後,會跳轉到一個渲染好的圖像頁面redis

若是跳出的圖像是黑屏,說明phantomjs渲染存在問題,能夠檢查一下Grafana的日誌sql

tail /var/log/grafana/grafana.log
2016/07/19 15:31:00 [I] Listen: http://0.0.0.0:3000
2016/07/19 15:31:22 [I] PhantomRenderer::renderToPng url   http://localhost:3000/dashboard-solo/db/redis_performance?panelId=1&from=1468891881256&to=1468913481256&var-instance=10.1.100.219%3A7013&width=1000&height=500
2016/07/19 15:31:23 [I] Completed 127.0.0.1 - "GET /query HTTP/1.1" 500 Internal Server Error 0 bytes in 238254us

上面的日誌是我安裝3.0.4版本時發現的問題,升級到最新的3.1.0版本就沒有問題了數據庫

curl -s https://packagecloud.io/install/repositories/grafana/stable/script.rpm.sh | sudo bash
sudo yum install grafana-3.1.0-1468321182.x86_64

若是還有問題,能夠檢查一下phantomjs是否編譯成功json

#/usr/share/grafana/vendor/phantomjs/phantomjs -v
2.1.1

若是編譯不成功,多是一些依賴包沒有安裝,能夠檢查一下瀏覽器

sudo yum -y install gcc gcc-c++ make flex bison gperf \
ruby openssl-devel freetype-devel fontconfig-devel \
libicu-devel sqlite-devel libpng-devel libjpeg-devel

Grafana API Key

admin->API Keys

clipboard.png

隨便輸入一個Key的名字,Role選擇Viewer(只讀),點Add後ruby

clipboard.png

把Key保存下來

clipboard.png

測試一下Key是否可使用

curl -H "Accept: application/json" \
-H "Authorization: Bearer 把key填這裏" \
"http://redis.daily.vdian.net:3000/render/dashboard-solo/db/host_performance?panelId=1&fullscreen&from=1468889495822&to=1468911095822&var-host=%24__all"

若是可否打開則說明驗證經過

提取保存

Grafana文檔中介紹的是Hubot,但hubot使用S3做爲存儲來保存PNG,不適合我天朝的網絡。這裏介紹Grafana-images來實現

安裝編譯

git clone https://github.com/lanyonm/grafana-images.git
git checkout master

該工具是Go寫的,安裝前確保已經安裝Go環境

make

編譯好了以後就能夠運行了。

3個參數

  1. imageHost: 須要在本機上另外運行一個HTTP服務,讓其餘應用可以訪問保存下來的圖片,配置時注意URL指向的目錄與imagePath配置目錄一致

  2. imagePath: 圖片保存到本地的目錄

  3. port: 這裏的port是grafana-images本身的port,與imageHost的URL無關

    #./grafana-images --help
    Usage of ./grafana-images:
      -imageHost string
    host for the saved images (default "http://grafana.example.com/saved-images")
      -imagePath string
    location on disk where images will be saved (default "/opt/saved-images")
      -port int
    grafana-images listening port (default 8080)

    帶入參數運行granfana-images

測試

再開一個終端,測試一下可否經過Grafana-images來獲取圖片

curl -d '{"imageUrl":"http://redis.daily.vdian.net:3000/render/dashboard-solo/db/host_performance?panelId=1&fullscreen&from=1468889495822&to=1468911095822&var-host=%24__all"}' \
-H "Accept: application/json" \
-H "Authorization: Bearer 你的Key" \
http://redis.daily.vdian.net:8080/grafana-images

若是成功,會返回以下返回值

{"pubImg":"http://redis.daily.vdian.net/download/fc9e541e5c1e4a15fb7afbd61ba26f61.png"}

在瀏覽器器中可以打開圖片就算成功了。

總結

經過上述方法,能夠把性能圖片保存爲圖片,建議經過腳本定時保存下來,你可能須要另一套前端頁面來調度訪問這些歷史信息的圖片,這裏就不贅述了。

相關文章
相關標籤/搜索