原文連接:http://ylw6006.blog.51cto.com/blog/470441/1722905mysql
在前面兩篇文章中記錄了使用logstash來收集mysql的慢查詢日誌,而後經過kibana以web的方式展現出來,但在生產環境中,需求會更復雜一些,並且經過logstash寫正則,實在是個費時費勁的事。例如在生產環境中會有要求分析某個時間段mysql或者mongodb的慢查詢日誌狀況;還有I/O吞吐量;這個時間段內常常執行的查詢語句,http訪問狀況等信息;而後將分析出來的結果以圖表的形式展示出來。聽起來是否是有點頭暈,有點高大上的感受,其實經過packetbeat,一切將變得簡單高效。本文介紹使用packetbeat,elasticsearch,kibana實現這個需求。git
操做系統版本:centos6.6 64bitgithub
Elasticsearch版本:elasticsearch-2.1.0.tar.gzweb
Kibana版本:Kibana 4.2.1redis
Packetbeat版本:packetbeat-1.0.0-1.x86_64sql
Topbeat版本:topbeat-1.0.0-x86_64 (topbeat實際上是用來收集操做系統信息的)mongodb
在前兩篇文章中未介紹若是安裝elasticsearch和kibana,這個其實很簡單,基本下載下來解壓一下,稍微修改一下配置文件便可運行起來,全部就忽略了,若是有問題,能夠自行百度或者bing一下。json
目前packetbeat支持的網絡協議有http,mysql,postgresql,redis,mongodb和thrift。Packetet支持pcap,pf_ring等抓包方式,採用哪一種方式進行抓包,則須要安裝相應的依賴包。centos
一:下載並安裝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-*'
|