第十四章·Kibana深入-Timelion畫圖實現系統監控


什么是Timelion?

Timelion使你可以輕松獲得以下問題的答案:

1)隨着時間的推移,每個唯一的用戶會查看多少個頁面?
2)這個星期五和上周五之間的交通量有什么不同?
3)今天有多少日本人口來到我的網站?
4)標准普爾500指數的10日均線是多少?
5)過去兩年收到的所有搜索請求的累計總和是多少?

  Timelion是Kibana時間序列的可視化工具。時間序列可視化是可視化的,以時間順序分析數據。Timelion可用於繪制二維圖形,時間繪制在x軸上。

  與使用簡單的條形圖或線條可視化相比有什么優勢?Timelion采取不同的方法。使用Timelion特定語法,您通過將功能鏈接在一起來定義圖形,而不是使用可視化編輯器創建圖表。該語法啟用了經典點系列圖不提供的一些功能,如將不同索引或數據源的數據繪制到一個圖形中。

但是在使用Timelion之前,我們需要下載並安裝Metricbeat

Metricbeat介紹及部署

Metricbeat介紹

Metricbeat可以定期收集操作系統和服務器的運行指標(CPU,內存,硬盤,IO,讀寫速度,進程等等),Metricbeat可以將收集到的指標和數據發送到你指定的輸出,比如:elasticsearch,logstash,redis等等,最終達成監視服務器的目標。


Metricbeat部署及配置

因為我們使用的ES和Kibana是5版本的,所以我們需要下載5版本的Metricbeat

#RPM包下載 [root@elkstack04 ~]# wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-5.3.3-x86_64.rpm #源碼包下載 [root@elkstack04 ~]# wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-5.3.3-linux-x86_64.tar.gz #安裝Metricbeat [root@elkstack04 ~]# yum localinstall -y metricbeat-5.3.3-x86_64.rpm #修改配置文件 [root@elkstack04 ~]# vim /etc/metricbeat/metricbeat.yml #========================== Modules configuration ============================ metricbeat.modules: #------------------------------- System Module ------------------------------- - module: system metricsets: # CPU stats - cpu # System Load stats - load # Per CPU core stats #- core # IO stats #- diskio # Per filesystem stats - filesystem # File system summary stats - fsstat # Memory stats - memory # Network stats - network # Per process stats - process # Sockets (linux only) #- socket enabled: true period: 1m processes: ['.*'] #================================ Outputs ===================================== # Configure what outputs to use when sending the data collected by the beat. # Multiple outputs may be used. #-------------------------- Elasticsearch output ------------------------------ output.elasticsearch: # Array of hosts to connect to. hosts: ["10.0.0.51:9200"] # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" #要加載儀表板,可以在metricbeat設置中啟用儀表板加載。當儀表板加載被啟用時,Metricbeat使用Kibana API來加載樣本儀表板。只有當Metricbeat啟動時,才會嘗試儀表板加載。 # 設置kibana服務地址 setup.kibana.host: "10.0.0.54:5601" # 加載默認的儀表盤樣式 setup.dashboards.enabled: true # 設置如果存在模板,則不覆蓋原有模板 setup.template.overwrite: false #啟動Metricbeat(CentOS6) [root@elkstack04 ~]# /etc/init.d/metricbeat start #啟動Metricbeat(CentOS7) [root@elkstack04 ~]# systemctl start metricbeat #檢查metricbeat是否正常運行(返回索引對應內容) [root@elkstack04 ~]# curl -XGET 'http://10.0.0.51:9200/metricbeat-*/_search?pretty' 

結果如下:

打開瀏覽器,訪問:http://10.0.0.51:9100/

查看Metricbeat索引

打開瀏覽器,訪問:http://10.0.0.54:5601/

添加metricbeat-*索引

創建后,即可在Discover中看到Metricbeat信息

Timelion使用Metricbeat

創建時間序列可視化

使用Metricbeat的時間序列數據帶你瀏覽Timelion提供的一些函數。

創建第一個可視化將比較在用戶空間中花費的CPU時間與一小時的結果偏移量的實時百分比,為了創建這個可視化,我們需要創建兩個Timelion表達式,一個是system.cpu.user.pct的實時平均數,另一個是1小時的平均偏移量。

首先,需要在第一個表達式中定義indextimefieldmetric,並在Timelion查詢欄中輸入以下表達式。

.es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct') 

現在需要添加另一個具有前一小時數據的系列,以便進行比較,為此,你必須向.es()函數添加一個offset參數,offset將用日期表達式偏移序列檢索。對於本例,你希望將數據偏移一小時,並使用日期表達式-1h,使用逗號分隔這兩個系列,在Timelion查詢欄中輸入以下表達式:

.es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct'), .es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct') 

很難區分這兩個系列,自定義標簽以便於區分它們,你總是可以將.label()函數附加到任何表達式以添加自定義標簽,在Timelion查詢欄中輸入以下表達式來定制標簽:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour'), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour') 

保存完整的Timelion工作表作為Metricbeat示例,作為一種最佳實踐,你應該在完成本教程的過程中保存對本工作表所做的任何重要更改。


定制和格式化可視化

Timelion有很多定制選項,你幾乎可以使用可用的函數對圖表的每個方面進行個性化設置,執行以下修改。

1)添加一個標題
2)更改系列類型
3)改變一個系列的顏色和不透明度
4)修改圖例

之前用兩個系列創建了一個時間軸圖表,讓我們繼續定制這個可視化。

在進行任何其他修改之前,將title()函數附加到表達式的末尾,以添加具有有意義名稱的標題,這將使不熟悉的用戶更容易理解可視化目的。對於這個示例,將title('CPU usage')添加到原始系列中,在Timelion 查詢欄中使用以下表達式:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour'), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage') 

為了進一步區分過去一小時系列,你將把圖表類型更改為區域圖表,為了做到這一點,你需要使用.lines()函數來定制折線圖,你將設置fill和width參數,分別設置折線圖的填充和折線寬度。在本例中,你將通過添加.lines(fill=1,width=0.5)將填充級別設置為1,邊框寬度設置為0.5,在Timelion查詢欄中使用以下表達式:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage') 

讓我們給這些系列塗上顏色,使當前的小時系列比過去一個小時系列流行一點,color()函數可用於更改任何系列的顏色,並接受標准顏色名稱、十六進制值或分組系列的顏色模式。對於這個示例,你將在過去一個小時使用.color(gray),而在當前小時使用.color(#1E90FF),在Timelion查詢欄中輸入以下表達式進行調整:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5).color(gray), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage').color(#1E90FF) 

最后但並非最不重要,調整圖例,使其占用盡可能小的空間,你可以使用.legend()函數來設置圖例的位置和樣式。在本例中,通過將.legend(columns=2, position=nw)兩列追加到原始系列,將圖例放置在可視化的西北位置,使用以下表達式進行調整:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5).color(gray), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage').color(#1E90FF).legend(columns=2, position=nw) 

保存下來,再創建一個新的。


使用數學函數

在前兩部分中,已經學習了如何創建和樣式化Timelion可視化,本節將探索Timelion提供的數學函數。你將繼續使用Metricbeat數據為入站和出站網絡流量創建新的Timelion可視化,首先,需要在工作表中添加一個新的Timelion可視化。

在頂部菜單中,單擊Add添加第二個可視化,當添加到工作表中時,你會注意到查詢欄已經被替換為默認的.es(*)表達式,這是因為查詢與你選擇的Timelion工作表上的可視化相關聯。

開始跟蹤入站/出站網絡流量,你的第一個表達式將計算system.network.in.bytes的最大值,將下面的表達式輸入到你的Timelion查詢欄:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes) 

在繪制變化率時,監視網絡流量更有價值,derivative()函數就是這樣做的 - 繪制值隨時間的變化,通過在表達式末尾添加.derivative()可以很容易地做到這一點,使用以下表達式來更新你的可視化:

現在是出站流量,你需要為system.network.out.bytes添加類似的計算,由於出站流量將離開你的機器,因此將此指標表示為負數是有意義的,.multiply()函數將系列乘以一個數字,這個數字是系列或系列列表的結果。對於本例,你將使用.multiply(-1)將出站網絡流量轉換為負值,使用以下表達式來更新你的可視化:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes).derivative(), .es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.out.bytes).derivative().multiply(-1) 

為了使這個可視化更容易使用,將這個系列從字節轉換為兆字節,Timelion有一個.divide()函數可以使用,.divide()接受與.multiply()相同的輸入,並將這個系列除以所定義的除數,使用以下表達式來更新你的可視化:

使用上一節中學習的格式化函數.title().label().color().lines().legend(),讓我們稍微整理一下這個可視化,使用以下表達式來更新你的可視化:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes).derivative().divide(1048576).lines(fill=2, width=1).color(green).label("Inbound traffic").title("Network traffic (MB/s)"), .es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.out.bytes).derivative().multiply(-1).divide(1048576).lines(fill=2, width=1).color(blue).label("Outbound traffic").legend(columns=2, position=nw) 

保存,開啟新的,畫圖


使用條件邏輯和跟蹤趨勢

在本節中,你將學習如何使用條件邏輯修改時間序列數據,並使用移動平均值創建趨勢,這有助於隨着時間的推移很容易地發現異常值和模式。

對於本教程,你將繼續使用Metricbeat數據添加另一個監控內存消耗的可視化,首先,使用以下表達式繪制system.memory.actual.used.bytes的最大值。

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes') 

讓我們創建兩個閾值來監視使用的內存數量,在本教程中,警告閾值為234MB,嚴重閾值為235MB,當使用內存的最大數量超過這些閾值中的任何一個時,將相應地對該系列進行着色。

 

如果你的計算機的閾值過高或過低,請相應地進行調整。

要配置這兩個閾值,可以使用Timelion的條件邏輯,在本教程中,你將使用if()將每個點與一個數字進行比較,如果條件的值為true,則調整樣式,如果條件的值為false,則使用默認樣式,Timelion提供了以下六個操作符值進行比較。

操作符 含義
eq 相等
ne 不相等
lt 小於
gt 大於
lte 小於等於
gte 大於等於

由於有兩個閾值,因此對它們進行不同的樣式是有意義的,使用gt操作符將警告閾值用.color('#FFCC11')塗成黃色,將嚴重閾值用.color('red')塗成紅色,在Timelion查詢欄中輸入以下表達式,以應用條件邏輯和閾值樣式:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red') 

現在你已經定義了閾值來輕松地識別異常值,讓我們創建一個新的系列來確定真正的趨勢是什么,Timelion的mvavg()函數允許計算給定窗口上的移動平均值,這對嘈雜的時間序列特別有用,對於本教程,你將使用.mvavg(10)來創建具有10個數據點窗口的移動平均線,使用以下表達式創建最大內存使用量的移動平均值:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').mvavg(10) 

現在你已經有了閾值和移動平均值,讓我們格式化可視化,以便更容易使用,和最后一部分一樣,使用.color().line().title().legend()函數相應地更新可視化:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').label('max memory').title('Memory consumption over time'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11').lines(width=5), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red').lines(width=5), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').mvavg(10).label('mvavg').lines(width=2).color(#5E5E5E).legend(columns=4, position=nw) 

保存


展示至Dashboard

整合,其實最終我們需要的是,網絡,cpu,內存優化后的圖

你已經正式利用了Timelion的功能來創建時間序列可視化,本教程的最后一步是向儀表盤添加你新的可視化,下面,本節將向你展示如何從Timelion工作表中保存可視化,並將其添加到現有的儀表盤中。

要將Timelion可視化保存為儀表盤面板,請執行以下步驟。

1)選擇要添加到一個(或多個)儀表盤上的可視化視圖。
2)點擊頂部菜單中的Save選項。
3)選擇Save current expression as Kibana dashboard panel
4)命名你的面板並點擊Save以作為儀表盤可視化。

現在你可以將這個儀表盤面板添加到任何你想要的儀表盤上,這個可視化現在將在可視化列表中列出,繼續並按照你創建的其他可視化效果的相同過程進行操作。

創建一個新的儀表盤或打開一個現有的儀表盤,以添加Timelion可視化,就像其他任何可視化一樣。

最終展示


免責聲明!

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



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