性能測試——監控工具Grafana的介紹與使用


1.簡介

Grafana是一款用Go語言開發的.源數據可視化工具,可以做數據監控和數據統計,帶有告警功能。目前使用grafana的公司有很多,如paypal、ebay、intel等。

1.1 七大特點

①可視化:快速和靈活的客戶端圖形具有多種選項。面板插件為許多不同的方式可視化指標和日志。
②報警:可視化地為最重要的指標定義警報規則。Grafana將持續評估它們,並發送通知。
③通知:警報更改狀態時,它會發出通知。接收電子郵件通知。
④動態儀表盤:使用模板變量創建動態和可重用的儀表板,這些模板變量作為下拉菜單出現在儀表板頂部。
⑤混合數據源:在同一個圖中混合不同的數據源!可以根據每個查詢指定數據源。這甚至適用於自定義數據源。
⑥注釋:注釋來自不同數據源圖表。將鼠標懸停在事件上可以顯示完整的事件元數據和標記。
⑦過濾器:過濾器允許您動態創建新的鍵/值過濾器,這些過濾器將自動應用於使用該數據源的所有查詢。

2 安裝

詳細安裝教程可參考:http://docs.grafana.org/installation/debian/

2.1 下載安裝包

wget <u>https://dl.grafana.com/oss/release/grafana-6.0.1-1.x86_64.rpm</u>

2.2 安裝環境依賴

yum install initscripts fontconfig  
yum install freetype
yum install urw-fonts

2.3 安裝Grafana服務

rpm -Uvh grafana-6.0.1-1.x86_64.rpm

2.4 插件安裝

使用grafana-cli工具安裝 #獲取可用插件列表 grafana-cli plugins list-remote 修改圖形為餅狀 grafana-cli plugins install grafana-piechart-panel 安裝其他圖形插件 grafana-cli plugins install grafana-clock-panel #鍾表形展示 grafana-cli plugins install briangann-gauge-panel #字符型展示 grafana-cli plugins install natel-discrete-panel #服務器狀態 grafana-cli plugins install vonage-status-panel 

2.5 插件卸載

例:grafana-cli plugins uninstall vonage-status-panel
安裝和卸載后需要重啟grafana才能夠生效

2.6 啟動、重啟、關閉

啟動:service grafana-server start 停止:service grafana-server stop 重啟:service grafana-server restart 加入開機自啟動: chkconfig --add grafana-server on 

2.7 啟動測試

默認用戶密碼:admin/admin, 訪問地址: 服務地址 : [http://grafana服務地址:3000](http://localhost:3000/) 如果出現登錄界面,代表安裝啟動成功 

3 創建Dashboard

3.1 數據源配置

Dashboard的建立都是基於某一個數據源的,所以要先加一個數據源。

 
增加MySQL數據源

3.2 可視化方式

可視化方式有很多種,不過Graph、Table、Pie chart 這三種基本就已經滿足數據展現要求了。

 
可視化方式

3.2.1 Graph

注意:只有Graph才能配置告警

新建時間間隔變量

 
新建時間間隔變量

Graph折線圖

 
Graph折線圖
SELECT $__timeGroupAlias(create_time,$__interval), country_name as metric, COUNT(user_id) as '用戶數量' FROM `user` WHERE $__timeFilter(create_time) GROUP BY 1,2 ORDER BY $__timeGroup(create_time,$__interval); 
 
展現方式默認是lines

Graph柱狀圖

把這個Graph折線圖Copy一份,改一下展現方式即可。

 
 
 
 
 
改Lines為Bars

3.2.2 Table

創建篩選查詢變量

 
新建一個下拉列表做篩選

用戶列表

 
用戶列表
select user_id,user_name,mobile,id_number,country_name,money,create_time from user WHERE $__timeFilter(create_time) and country_name in ($countryName)

注意:默認添加完table后,如果有數字,會以K為單位,比如將300000展示位30k。
數字展示方式修改,Add column style:

 
修改數字樣式

3.2.3 Pie

 
餅圖
select country_name, create_time as time, count(*) as c from user where $__timeFilter(create_time) group by country_name order by c asc; 

3.3 權限管理

3.3.1 用戶管理

 
用戶管理

3.3.2 團隊管理

 
新建團隊

 
團隊加成員

3.3.3 文件夾權限設置

 
 

3.3.4 dashboarad權限設置

 
 

如果user在該org下role=admin,就擁有了對該dashboard的admin權利,愛干啥干啥,比如配置這個dashboard,看dashboard,配置dashboard的權限。
如果user在該org下role=editor,就擁有了對該dashbaord的edit操作權限,可以編輯dashboard,當然也可以看。
如果user在該org下role=viewer,就擁有對該dashbaord的view操作權限,就是可以看這個dashboard,但不能編輯。
就如上圖設置,如果登錄用戶是admin,就可以進行編輯;如果登錄用戶是wade,僅有查看權限。

3.4 模板變量

當表格中出現數據后,需要通過篩選條件進行篩選,grafana提供了模板變量用於自定義篩選字段。
Type:定義變量類型
Query:這個變量類型允許您編寫一個數據源查詢,該查詢通常返回一個 metric names, tag values or keys。例如,返回erver names, sensor ids or data centers列表的查詢。
interval:interval值。這個變量可以代表時間跨度。不要按時間或日期直方圖間隔硬編碼一個組,使用這種類型的變量。

遺留問題:當選擇1d(單位是d)時,會報解析錯誤:error parsing interval 1d,暫未解決;

Datasource:此類型允許您快速更改整個儀表板的數據源。如果在不同環境中有多個數據源實例,則非常有用。
Custom:使用逗號分隔列表手動定義變量選項。
Constant:定義一個隱藏常數。有用的metric路徑前綴的dashboards,你想分享。在dashboard export,期間,常量變量將作為一個重要的選項。
Ad hoc filters:非常特殊類型的變量,只對某些數據源,InfluxDB及Elasticsearch目前。它允許您添加將自動添加到使用指定數據源的所有metric查詢的key/value 過濾器。

上面的Table和Graph分別使用了interval和query來定義變量進行篩選,不再重復。

Text box

 
新建輸入框

 
 

Custom

 
新建Custom變量

 
下拉選擇

3.5 版本控制

 
 

4 告警通知

4.1 開啟告警

grafana只有graph支持告警通知。
grafana的告警通知渠道有很多種,像Email、Teams、釘釘等都有支持。
在grafana.ini中開啟告警:

#################################### Alerting ############################
[alerting]
# Disable alerting engine & UI features
enabled = true   #開啟
# Makes it possible to turn off alert rule execution but alerting UI is visible
execute_alerts = true  #開啟
# Default setting for new alert rules. Defaults to categorize error and timeouts as alerting. (alerting, keep_state)
;error_or_timeout = alerting
# Default setting for how Grafana handles nodata or null values in alerting. (alerting, no_data, keep_state, ok)
;nodata_or_nullvalues = no_data
# Alert notifications can include images, but rendering many images at the same time can overload the server
# This limit will protect the server from render overloading and make sure notifications are sent out quickly
;concurrent_render_limit = 5

4.2 郵件通知

4.2.1 STMP服務器配置

要能發送郵件通知,首先需要在配置文件grafana.ini中配置郵件服務器等信息:

#################################### SMTP / Emailing ########################## [smtp] enabled = true #是否允許開啟 host = #發送服務器地址,可以再郵箱的配置教程中找到: user = 你的郵箱 # If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;""" password = 這個密碼是你開啟smtp服務生成的密碼 ;cert_file = ;key_file = skip_verify = true from_address = 你的郵箱 from_name = Grafana # EHLO identity in SMTP dialog (defaults to instance_name) ;ehlo_identity = dashboard.example.com [emails] ;welcome_email_on_sign_up = false 

修改完配置,記得重啟Grafana服務

4.2.2 郵件發送

配置郵件通知渠道

 
配置郵件通知渠道

發送測試

 
發送測試

設置告警條件

 
 

5 小結

Grafana是個功能強大、展現層很漂亮的數據可視化監控工具,本篇主要介紹了Grafana基於MySQL數據源的安裝及常用姿勢,也支持其他數據源如ElasticSearch、InfluxDB等。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM