Grafana 操作手冊
一、Grafana 下載 & 登入
官方下載連結: https://grafana.com/grafana/download
- Version: Grafana 版本,本篇是選擇使用 v8.4.4
- Edition: 選擇OSS(Open Source Software)
- 作業系統: 依照自身電腦選擇對應的即可
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
預設 port 為3000,網址: localhost:3000
預設登入帳號/密碼 : admin / admin
二、Grafana 基本操作和設定 (以MySQL為範例)
新增 Dashboard
1.在頁面左邊工具列找到 '+',選擇 Create 中的 Dashboard
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2.畫面會出現一個 Add panel (假如沒有出現可以點右上角紅框處新增),點擊Add a new panel
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3.進入 edit panel 的模式後畫面可分為三個部分
(1)紅框: 圖形預覽
(2)藍框: 資料來源(data source),我在這部分都使用MySQL資料庫,SQL搜尋的語法也是寫在這裡
(3)綠框: 選擇 panel 類型(time series, bar chart, gauge, table等等),以及 panel 的設定(title, style, mapping)都在這邊更改
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這時候會發現還沒將自己的 data source 讀取進來,所以回過頭來新增 data source
新增 Data Source
1.在頁面左邊工具列點選 Configuration 中的 Data Source,並選取右上方的 Add data source
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2.裡面有許多 data source 可以選擇,這邊選擇 MySQL database作為示範
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3.紅框內為必填的部分,其他可以按照自己的需求去做更改,填完之後點擊底下的 Save & test
註: Save & test 可能會出現 connecting error,如何排除就需要去 google 了
- Name: 之後在 panel 選取 data source 時顯示的名稱
- Host: IP address : 資料庫的 port (例 localhost:3306)
- Database: 填入欲當資料來源的資料庫名稱
- User/Password: 資料庫帳號/密碼
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
新增 Time Series Panel
1.回到 dashboard 並且進入edit panel mode,點擊右上角紅框處即可選擇自己想要的panel類型
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
註: 在 Grafana plugin 裡可以增加 panel 的種類,可以搜尋有沒有你需要的 panel 類型
https://grafana.com/grafana/plugins/
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2.左下角先選擇 Edit SQL 來撰寫 SQL 搜尋 (熟悉 SQL 語法後覺得預設的 query builder 不是很好用)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
輸入你想搜尋的 SQL query ( 欄位 dt = 時間, val = 整數 random 1 ~ 100)
$timeGroupAlias(dt,$__interval) && $timeGroup(dt,$__interval)
為何要這樣寫而不直接寫 dt =>
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(點此連結)防止 time zone 錯誤
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3.Title 的設定在頁面右邊,想要註解也可以寫進 Description,其他更細節的設定可以往下拉慢慢嘗試
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
4.設定完成後點擊右上角的 Apply,就可以在 Dashboard 上成功看到美美的圖囉!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
新增 Table Panel
建立 panel 的步驟與上述差異不大,主要是介紹有哪些額外的功能來達成客製化的效果
先建立一個 Table 類型的 panel,並且進行 SQL 搜尋
將 SQL 複製貼上
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
完成後我們進行欄位重新命名,要重新命名有兩種方法,一種是透過 SQL,一種是 Grafana 內建的功能,這邊我們只介紹 Grafana 的做法 Transform
2.點選 Transform -> Add transformation -> Organize fields
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3.輸入名稱以更改舊欄位名稱,改完會馬上顯示在 panel 上方
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
當然,Transform 除了 rename 外還有許多功能可以做使用。接著我們來看看如何使用 Overrides 更改單行(column)的屬性
Overrides
一般來說,我們要對表格的所有欄位寬度進行統一的修改,會從 All -> Column width 來輸入寬度。但是假如我覺得只有"時間"這欄不夠大,要針對單行進行修改,就必須使用到 Overrides
4.點選 Overrides -> Fields with name -> Choose "時間"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
5.Add override property -> Column width -> 輸入數值"200",即可看到"時間"欄位長度單獨變寬
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在 All 所有 panel 的設定,Overrides 基本上都有一樣的設定功能,所以想要進行客製化,Overrides 的使用必不可少
Variable
承接上述 table,當我們想建立下拉式選單來選擇搜尋的內容時,可以使用 Variable 搭配 SQL 來達成
6.點擊右上方的 dashboard setting -> 左邊選擇 Variables -> Add variable
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
7.填入 Variable 資訊
General
- Name: device (在 dashboard 使用時的名字=>$device)
- Type: Query
- Label: 設備序號 (顯示名稱)
Query Options
- Data source: MySQL-3307
- Query: select device_id from tb_feature group by device_id
底下會有預覽結果(preview of values),確認後按下 Update
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
8.回到 dashboard,可以看到上方多了剛剛建立的下拉式選單,但此時還沒與 panel 進行連動
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
9.進到 table panel 的設定頁面,在紅框處加入 where device_id = '$device'
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
10.最後,回到首頁試著操作下拉式選單確認有沒有成功
Extension:
再加一個 variable 'unit'
General
- Name: unit
- Type: Query
- Label: 量測部位
Query Options
- Data source: MySQL-3307
- Query: select unit_id from tb_feature where device_id = '$device' group by unit_id
SQL 加入 where device_id = '$device' and unit_id = '$unit'
三、Grafana 個人技巧分享
Grafana 技巧 - 如何顯示固定時間區間的 Panel
Grafana 技巧 - 防止 Timestamp 與資料庫差8小時
Grafana 技巧 - Float 浮點數對小數點位數做調整
Grafana 技巧 - Value Mapping
Grafana 技巧 - 如何更改登入 Title 與分頁 Title
Grafana 技巧 - 如何更改 Logo
四、Grafana 移植相關文件
如何將 Grafana 監控平台移植到新 Server 上
Linux 下載 Grafana (.deb package版)