在前面兩篇文章中記錄了使用logstash來收集mysql的慢查詢日誌,而後經過kibana以web的方式展現出來,但在生產環境中,需求會更復雜一些,並且經過logstash寫正則,實在是個費時費勁的事。例如在生產環境中會有要求分析某個時間段mysql或者mongodb的慢查詢日誌狀況;還有I/O吞吐量;這個時間段內常常執行的查詢語句,http訪問狀況等信息;而後將分析出來的結果以圖表的形式展示出來。聽起來是否是有點頭暈,有點高大上的感受,其實經過packetbeat,一切將變得簡單高效。本文介紹使用packetbeat,elasticsearch,kibana實現這個需求。mysql
操做系統版本:centos6.6 64bitgit
Elasticsearch版本:elasticsearch-2.1.0.tar.gzgithub
Kibana版本:Kibana 4.2.1web
Packetbeat版本:packetbeat-1.0.0-1.x86_64redis
Topbeat版本:topbeat-1.0.0-x86_64 (topbeat實際上是用來收集操做系統信息的)sql
在前兩篇文章中未介紹若是安裝elasticsearch和kibana,這個其實很簡單,基本下載下來解壓一下,稍微修改一下配置文件便可運行起來,全部就忽略了,若是有問題,能夠自行百度或者bing一下。mongodb
目前packetbeat支持的網絡協議有http,mysql,postgresql,redis,mongodb和thrift。Packetet支持pcap,pf_ring等抓包方式,採用哪一種方式進行抓包,則須要安裝相應的依賴包。json
一:下載並安裝packetbeatcentos
# 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模板網絡
# curl -XPUT 'http://192.168.1.226:9200/_template/packetbeat' -d@/etc/packetbeat/packetbeat.template.json
三:修改packetbeat配置文件
# 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服務
# /etc/init.d/packetbeat start
五:導入packetbeat-dashboards
# 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數據顯示不全,像是被截斷的樣子,排查後發現實際上是模板的問題,刪除模板後從新導入便可.
# 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-*換成對應的索引名):
# curl -XGET 'http://192.168.1.226:9200/packetbeat-*/_search?pretty'
刪除數據(若是你有多個索引,能夠把packetbeat-*換成對應的索引名):
# curl -XDELETE 'http://192.168.1.207:9200/packetbeat-*'