原文鏈接:http://ylw6006.blog.51cto.com/blog/470441/1722905
在前面兩篇文章中記錄了使用logstash來收集mysql的慢查詢日志,然后通過kibana以web的方式展示出來,但在生產環境中,需求會更復雜一些,而且通過logstash寫正則,實在是個費時費勁的事。例如在生產環境中會有要求分析某個時間段mysql或者mongodb的慢查詢日志情況;還有I/O吞吐量;這個時間段內經常執行的查詢語句,http訪問情況等信息;然后將分析出來的結果以圖表的形式展現出來。聽起來是不是有點頭暈,有點高大上的感覺,其實通過packetbeat,一切將變得簡單高效。本文介紹使用packetbeat,elasticsearch,kibana實現這個需求。
操作系統版本:centos6.6 64bit
Elasticsearch版本:elasticsearch-2.1.0.tar.gz
Kibana版本:Kibana 4.2.1
Packetbeat版本:packetbeat-1.0.0-1.x86_64
Topbeat版本:topbeat-1.0.0-x86_64 (topbeat其實是用來收集操作系統信息的)
在前兩篇文章中未介紹如果安裝elasticsearch和kibana,這個其實很簡單,基本下載下來解壓一下,稍微修改一下配置文件即可運行起來,所有就忽略了,如果有問題,可以自行百度或者bing一下。
目前packetbeat支持的網絡協議有http,mysql,postgresql,redis,mongodb和thrift。Packetet支持pcap,pf_ring等抓包方式,采用哪種方式進行抓包,則需要安裝相應的依賴包。
一:下載並安裝packetbeat
1
2
3
|
# yum -y install libpcap
# rpm -ivh https://download.elastic.co/beats/packetbeat/packetbeat-1.0.0-x86_64.rpm
# rpm -ivh https://download.elastic.co/beats/topbeat/topbeat-1.0.0-x86_64.rpm
|
二:向elasticsearch導入packetbeat模板
1
2
|
# curl -XPUT
'http://192.168.1.226:9200/_template/packetbeat' -d@/etc/packetbeat/packetbeat.template.json
|
三:修改packetbeat配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# cat /etc/packetbeat/packetbeat.yml --server15
shipper:
name: server15
tags: ["server15"]
interfaces:
device: any
type: pcap
buffer_size_mb: 100
protocols:
mysql:
ports: [3306]
output:
elasticsearch:
host: 192.168.1.207
port: 9200
enabled: true
# cat /etc/packetbeat/packetbeat.yml --server226
shipper:
name: server226
tags: ["server226"]
interfaces:
device: eth0
type: pcap
buffer_size_mb: 100
protocols:
mongodb:
ports: [37017, 38017]
send_request: true # index the request payload
send_response: true # index the response payload
max_docs: 10 # maximum number of documents to index per request/response
max_doc_length: 1024 # maximum document size to index
protocols:
mysql:
ports: [3306]
protocols:
redis:
ports: [6379]
output:
elasticsearch:
enabled: true
host: 192.168.1.207
port: 9200
|
四:啟動packetbeat服務
1
|
# /etc/init.d/packetbeat start
|
五:導入packetbeat-dashboards
1
2
3
|
# git clone https://github.com/elastic/packetbeat-dashboards
# cd packetbeat-dashboards
# sh load.sh -url http://192.168.1.207:9200
|
六:web展示
1: 配置索引,這個在執行完load.sh腳本之后,索引會自動創建
2: 查看客戶端的數據推送情況
3: 查看導入的面板,可視化視圖,點擊setting-objects
4: 圖形展示,點擊dashboard-load save dashboards
Mysql情況:
在有多台mysql服務的情況下,可以根據tags來區分,在搜索框中輸入相應的tag,則只顯示對應的數據
Mongodb情況
匯總情況:
更多數據演示請訪問packetbeat demo網址:http://demo.elastic.co/packetbeat/
七:故障排錯
1: 在測試過程中曾經發現mysql里面的most frequent Mysql queries和slowest mysql queries數據顯示不全,像是被截斷的樣子,排查后發現其實是模板的問題,刪除模板后重新導入即可.
1
2
3
4
5
|
# curl -XDELETE 'http://192.168.1.207:9200/*'
# curl -XPUT
'http://192.168.1.207:9200/_template/packetbeat' -d@/etc/packetbeat/packetbeat.template.json
# cd packetbeat-dashboards
# sh load.sh -url http://192.168.1.207:9200
|
2: elasticsearch數據維護
搜索數據:(如果你有多個索引,可以把packetbeat-*換成對應的索引名):
1
|
# curl -XGET 'http://192.168.1.226:9200/packetbeat-*/_search?pretty'
|
刪除數據(如果你有多個索引,可以把packetbeat-*換成對應的索引名):
1
|
# curl -XDELETE 'http://192.168.1.207:9200/packetbeat-*'
|