原文连接: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-*'
|