在前面两篇文章中记录了使用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脚本以后,索引会自动建立
![a8eff16ba66d56b056198448ab8f6c33.gif](https://img8.php1.cn/3cdc5/18414/ae9/255a62df50389ab9.gif)
2: 查看客户端的数据推送状况
![76a52e5016cd80f821167ab981a12c72.gif](https://img.php1.cn/3cd4a/1eebe/cd5/7d7ef3f69d479716.webp)
4: 图形展现,点击dashboard-load save dashboards
![0a757745a8b692e503dec14bb2cbb7b9.gif](https://img8.php1.cn/3cdc5/18414/ae9/c5f43425aa53eded.gif)
Mysql状况:
![d771f0acc1e6e35a1533f024d1a10636.gif](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
Mongodb状况
![93a0d3ec49c3d49f3291d0d78137e7b8.gif](https://img8.php1.cn/3cdc5/18414/ae9/3a9e8b30312a7e84.gif)
汇总状况:
![0bcb3e83ab3f3524c887756a21b8bbc8.gif](https://img8.php1.cn/3cdc5/18414/ae9/9da03aef43c8adea.gif)
七:故障排错
1: 在测试过程当中曾经发现mysql里面的most frequent Mysql queries和slowest mysql queries数据显示不全,像是被截断的样子,排查后发现实际上是模板的问题,删除模板后从新导入便可.
![577dd5d3b0c6751d403f1bb6b586e7ea.gif](https://img8.php1.cn/3cdc5/18414/ae9/ca15372b43cd98a7.gif)
![657aa1d61c62257cfc8a1a67a4bdd43c.gif](https://img8.php1.cn/3cdc5/18414/ae9/c06604b8ef7cdce1.gif)
# 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-*'